最近要開發1個接口,接收到1個指令后自動觸發自動化測試,雖然也可以通過shell命令做這一步,但因為目前所有構建自動化的的動作都通過jenkins完成,所以想要嘗試能不能用python去控制jenkins構建job。還真有!萬能的python。想起來一句話,有趣的事,python永遠不會缺席!
通過jenkins-python實現在后台操作jenkins構建job,只需要5步,並且前面4步都是簡單的配置,用python代碼實現操作也只有不到20行代碼,很簡單
step1:在jenkins中創建1個測試job
step2:查看Jenkins的User Id,並記錄下來,等下配置python腳本時需要用到
step3:jenkins生成api-token,記錄token至1個文檔中,后面要用到
點擊【保存】
step4:安裝python-jenkins庫
命令行安裝
1 sudo pip install python-jenkins
pythonCharm安裝:
當前使用的python-jenkins版本是1.5.0
接下來通過python腳本嘗試構建一個jenkins項目
代碼示例:
通過這個小示例,感覺用python-jenkins還是可以非常方便在的在后台操作jenkins的。
1 #導入依賴 2 import jenkins 3 4 #定義遠程的jenkins master server 的url,以及port 5 jenkins_server_url = 'http://10.1.71.51:8080/jenkins' 6 7 #定義用戶的Userid 和 api token 8 user_id = 'admin' 9 api_token = '11a7a8151dbde5173fa19b346ad46b5efe' 10 11 #實例化jenkins對象,連接遠程的jenkins master server 12 server = jenkins.Jenkins(jenkins_server_url,username=user_id,password=api_token) 13 #打印一下server查是否連接成功 14 # print(server) #返回一個jenkins對象<jenkins.Jenkins object at 0x10807d190> 15 16 # 構建job名為testJob的job(不帶構建參數) 17 # server.build_job('testJob') 18 19 #查看某個job的構建信息 20 job_info=server.get_job_info('testJob') 21 # print(job_info) 22 23 #獲取job名為testJob的job的最后次構建號 24 lastbuildNumber=server.get_job_info('testJob')['lastBuild']['number'] 25 26 #獲取job名為testJob的job的最后1次構建的執行結果狀態 27 result =server.get_build_info('testJob',lastbuildNumber)['result'] 28 29 #判斷testJob是否還在構建中 30 status = server.get_build_info('testJob',lastbuildNumber)['building'] 31 print(status)
我們可以打印job_info可以一下會看到什么信息:
可以很明顯看出來,get_job_info('job_name')返回的是整個job的構建信息
job_info=server.get_job_info('testJob')
1 { 2 '_class': 'hudson.model.FreeStyleProject', 3 'actions': [ 4 { 5
6 }, 7 { 8
9 }, 10 { 11
12 }, 13 { 14 '_class': 'com.cloudbees.plugins.credentials.ViewCredentialsAction'
15 } 16 ], 17 'description': '', 18 'displayName': 'testJob', 19 'displayNameOrNull': None, 20 'fullDisplayName': 'testJob', 21 'fullName': 'testJob', 22 'name': 'testJob', 23 'url': 'http: //10.1.71.51/jenkins/job/testJob/', 24 'buildable': True, 25 'builds': [ 26 { 27 '_class': 'hudson.model.FreeStyleBuild', 28 'number': 25, 29 'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
30 }, 31 { 32 '_class': 'hudson.model.FreeStyleBuild', 33 'number': 24, 34 'url': 'http: //10.1.71.51/jenkins/job/testJob/24/'
35 }, 36 { 37 '_class': 'hudson.model.FreeStyleBuild', 38 'number': 23, 39 'url': 'http: //10.1.71.51/jenkins/job/testJob/23/'
40 } 41 ], 42 'color': 'blue', 43 'firstBuild': { 44 '_class': 'hudson.model.FreeStyleBuild', 45 'number': 1, 46 'url': 'http: //10.1.71.51/jenkins/job/testJob/1/'
47 }, 48 'healthReport': [ 49 { 50 'description': 'Buildstability: Norecentbuildsfailed.', 51 'iconClassName': 'icon-health-80plus', 52 'iconUrl': 'health-80plus.png', 53 'score': 100
54 } 55 ], 56 'inQueue': True, 57 'keepDependencies': False, 58 'lastBuild': { 59 '_class': 'hudson.model.FreeStyleBuild', 60 'number': 25, 61 'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
62 }, 63 'lastCompletedBuild': { 64 '_class': 'hudson.model.FreeStyleBuild', 65 'number': 25, 66 'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
67 }, 68 'lastFailedBuild': { 69 '_class': 'hudson.model.FreeStyleBuild', 70 'number': 7, 71 'url': 'http: //10.1.71.51/jenkins/job/testJob/7/'
72 }, 73 'lastStableBuild': { 74 '_class': 'hudson.model.FreeStyleBuild', 75 'number': 25, 76 'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
77 }, 78 'lastSuccessfulBuild': { 79 '_class': 'hudson.model.FreeStyleBuild', 80 'number': 25, 81 'url': 'http: //10.1.71.51/jenkins/job/testJob/25/'
82 }, 83 'lastUnstableBuild': None, 84 'lastUnsuccessfulBuild': { 85 '_class': 'hudson.model.FreeStyleBuild', 86 'number': 7, 87 'url': 'http: //10.1.71.51/jenkins/job/testJob/7/'
88 }, 89 'nextBuildNumber': 26, 90 'property': [ 91
92 ], 93 'queueItem': { 94 '_class': 'hudson.model.Queue$WaitingItem', 95 'blocked': False, 96 'buildable': False, 97 'id': 55, 98 'inQueueSince': 1567674112446, 99 'params': '', 100 'stuck': False, 101 'task': { 102 '_class': 'hudson.model.FreeStyleProject', 103 'name': 'testJob', 104 'url': 'http: //10.1.71.51/jenkins/job/testJob/'
105 }, 106 'url': 'queue/item/55/', 107 'why': 'Inthequietperiod.Expiresin4.9秒', 108 'timestamp': 1567674117446
109 }, 110 'concurrentBuild': False, 111 'downstreamProjects': [ 112
113 ], 114 'labelExpression': None, 115 'scm': { 116 '_class': 'hudson.scm.NullSCM'
117 }, 118 'upstreamProjects': [ 119
120 ] 121 }
參考文檔:
Python-Jenkins API使用 —— 在后端代碼中操控Jenkins