Closed zking2000 closed 1 month ago
import jenkins
import requests
from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
import ssl
class SSLAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
kwargs['ssl_context'] = context
return super(SSLAdapter, self).init_poolmanager(*args, **kwargs)
# 创建一个自定义的 requests 会话,禁用 SSL 验证
session = requests.Session()
session.mount('https://', SSLAdapter())
session.verify = False
class NoVerifyJenkins(jenkins.Jenkins):
def __init__(self, baseurl, username=None, password=None):
super().__init__(baseurl, username, password)
self.session = session
def jenkins_open(self, req, add_crumb=True):
"""Submit a request to Jenkins and get the response."""
req.headers['Authorization'] = 'Basic ' + self.auth
response = self.session.send(req, verify=False)
if response.status_code == 401:
raise jenkins.JenkinsException('Error in request. Possibly authentication failed [401]: %s' % req.get_full_url())
elif response.status_code == 403:
raise jenkins.JenkinsException('Error in request. Possibly authentication failed [403]: %s' % req.get_full_url())
elif response.status_code != 200:
raise jenkins.JenkinsException('Error in request [%s]: %s' % (response.status_code, req.get_full_url()))
return response.text
# Jenkins服务器的URL、用户名和API令牌
jenkins_url = 'https://your-jenkins-url'
username = 'your-username'
api_token = 'your-api-token'
# 创建 Jenkins 对象,使用禁用 SSL 验证的会话
server = NoVerifyJenkins(jenkins_url, username=username, password=api_token)
# 遍历文件夹中的所有 job
def get_jobs(folder_name=''):
jobs = server.get_jobs(folder_name)
for job in jobs:
job_name = job['name']
job_url = job['url']
print(f"Job Name: {job_name}, Job URL: {job_url}")
# 如果是文件夹类型,递归遍历子文件夹
if job['_class'] == 'com.cloudbees.hudson.plugins.folder.Folder':
get_jobs(f"{folder_name}/{job_name}" if folder_name else job_name)
# 开始遍历根文件夹
get_jobs()
Python
wget http://your-jenkins-url/jnlpJars/jenkins-cli.jar
!/bin/bash
JENKINS_URL="http://your-jenkins-url" JENKINS_USER="your-username" JENKINS_API_TOKEN="your-api-token" JENKINS_CLI="java -jar jenkins-cli.jar -s $JENKINS_URL -auth $JENKINS_USER:$JENKINS_API_TOKEN"
遍历文件夹中的所有job
function get_jobs() { local folder_name="$1" local jobs
}
开始遍历根文件夹
get_jobs