您好,登錄后才能下訂單哦!
這篇文章主要講解了Python-jenkins如何獲取job構建信息,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
官方文檔:
需求:當1個job啟動構建后,獲取它的構建狀態.(成功,失敗,駁回,構建中,正在排隊)
關鍵函數:
獲取job是否在排隊的結果
獲取正在排隊構建的job隊列 即pending狀態中的所有job,如果沒有 pending狀態的job即返回1個空列表
queue_info = server.get_queue_info()
獲取job構建結果
server.get_build_info(name, build_number)[ 'result'] # 構建結束 SUCCESS|FAILURE<class 'str'> ABORTED <class 'str'> 構建中None None <class 'NoneType'>
代碼示例:
def get_build_state(server, name, build_number): ''' :param name: job_name :param build_number: 最后1次構建序號 :param:jenkins_server :return: 最后1次構建狀態 pending,success,false,building ''' build_state = None # 獲取正在排隊構建的job隊列 即pending狀態中的所有job,如果沒有 pending狀態的job即返回1個空列表 queue_info = server.get_queue_info() if queue_info: for queue_job_info in queue_info: if queue_job_info['task']['name'] == name: # msg = 'pending期,排隊構建中' build_state = 'pending' else: build_state = server.get_build_info(name, build_number)[ 'result'] # 構建結束 SUCCESS|FAILURE<class 'str'> ABORTED <class 'str'> 構建中None None <class 'NoneType'> return build_state
通過接口將該功能封裝起來
@build_bp.route('/gotest/api/getBuildState', methods=['POST']) def get_job_build_state(): ''' 獲取job構建狀態 return:build_state Success,Faild,Pending,Building ''' data = request.get_json() job_name = data['job_name'] server = get_jenkins_server() # 獲取job的最后次構建號 lastbuildNumber = server.get_job_info(job_name)['lastBuild']['number'] #獲取構建狀態 build_state = get_build_state(server, job_name, lastbuildNumber) if build_state is None: build_msg = 'Building' elif build_state == 'SUCCESS': build_msg = 'SUCCESS' elif build_state == 'FAILURE': build_msg = 'FAILURE' elif build_state == 'ABORTED': build_msg = 'ABORTED' elif build_state =='pending': build_msg = 'pending' response = {} datas={} response['code'] = 200 response['msg'] = 'OK' datas['build_state'] = build_msg datas['buildNumber'] = lastbuildNumber response['datas'] = datas return response
測試接口:
import requests url='http://127.0.0.1:5000/gotest/api/getBuildState' data={'job_name':'android_official'} r = requests.post(url,json=data) print(r.text)
響應:
{"code":200,"datas":{"buildNumber":1010,"build_state":"SUCCESS"},"msg":"OK"}
看完上述內容,是不是對Python-jenkins如何獲取job構建信息有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。