歡迎您光臨本站 註冊首頁

使用python-Jenkins批量創建及修改jobs操作

←手機掃碼閱讀     zhang3221994 @ 2020-05-13 , reply:0

1.使用jobsName.ini文件保存要創建job的名字
jobs1
jobs2
jobs3
2.使用Jenkins創建job時自動生成的config.xml文件為模板進行批量創建jobs或修改jobs,一般生成的job會在你安裝的Jenkins目錄下找到
3.腳本實現
import jenkins jobsOperation = input("請選擇是新建jobs還是修改jobs:
a.創建jobs
b.修改jobs
") # 連接遠程Jenkins server=jenkins.Jenkins("http://127.0.0.1:8080/", username="admin", password="admin") # 讀取及修改的配置模板 pathConfigxml = open("config.xml",encoding='utf-8').read() # 讀取要創建的jobs名稱 def readJobsName(): jobnames = open('jobsName.ini',encoding="utf-8") return jobnames pass # 讀取要修改配置的jobs名稱 def readChangeJobsName(): changeJobsName = open('changeJobsName.ini', encoding='utf-8') return changeJobsName pass # 創建新的jobs def createNewJobs(): jobsname = readJobsName() for jobName in jobsname: server.create_job(jobName.replace("
",""),pathConfigxml) pass # 批量修改已有的jobs def changeJobs(): changeJobsName = readChangeJobsName() for changeJobName in changeJobsName: server.reconfig_job(changeJobName.replace("
",""), pathConfigxml) pass if "a"==jobsOperation: createNewJobs() elif "b"==jobsOperation: changeJobs() else:print("未選擇操作,退出任務!")
補充知識:python寫一個功能:禁用jenkins中某一個job任務
import requests #python寫一個功能:禁用jenkins某一個任務(job) print(requests.get('http://localhost:8080/jenkins/job/Test_version/').text) url = 'http://localhost:8080/jenkins/job/Test_version/disable' re = requests.post(url, data={}, auth=('wyq', 'wyq')) print(re.status_code) print(re.headers) print(re.reason)
執行成功並在jenkins下查看Test_version任務已被成功禁用:


[zhang3221994 ] 使用python-Jenkins批量創建及修改jobs操作已經有114次圍觀

http://coctec.com/docs/python/shhow-post-234244.html