利用 Jenkins API 批量停用对应view内的任务

安装并导入库

安装:pip install python-jenkins

1
import python-jenkins

连接到Jenkins

1
2
3
server = jenkins.Jenkins('https://jenkins.domain.com/',username='username', password='password',timeout =5)
user = server.get_whoami()
print(user)
  1. 获取整个view下的所有任务
1
jobs = server.get_jobs(view_name="my view")
  1. 停用任务
1
2
3
4
5
6
7
for job in jobs:
job_info = server.get_job_info(job['name'])
if job_info['disabled']:
print(f"{job['name']} is already disabled")
else:
print(f"now disable {job['name']}...")
server.disable_job(job['name'])