python自动化部署jenkins_python+jenkins纯api实现自动化部署

#coding=utf8

import sys

import os

import requests

import json

import time

username = ''

password =

server_ip=''

job_name=''

req = requests.get('http://{}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'.format(server_ip), auth=(username, password))

res=req.text

crumb=res.split(':')[-1].strip()

print crumb

headers={'Jenkins-Crumb':crumb,'Content-Type':'application/json;UTF-8'}

#查询上一次任务,如果在执行,则终止

url='http://{}/job/{}/lastBuild/api/json?pretty=true'.format(server_ip,job_name)

req = requests.post(url,headers=headers,data={}, auth=(username, password))

res=json.loads(req.text)

build_number=res['id']

status=res['building']

if status==True :

# 查询上一次任务,如果在执行,则终止

url = 'http://{}/job/{}/{}/stop/api/json?pretty=true'.format(server_ip, job_name,build_number)

req = requests.post(url, headers=headers, data={}, auth=(username, password))

time.sleep(5)

build_number=int(build_number)+1

#开始创建任务

url='http://{}/job/{}/build'.format(server_ip,job_name)

req = requests.post(url,headers=headers,data={}, auth=(username, password))

print req.text

time.sleep(8)

while 1:

try:

url='http://{}/job/{}/{}/api/json?pretty=true'.format(server_ip,job_name,build_number)

req = requests.get(url,headers=headers, auth=(username, password))

result= json.loads(req.text)['building']

if result==False:

break

except:

pass

url='http://{}/job/{}/lastBuild/api/json?pretty=true'.format(server_ip,job_name)

req = requests.post(url,headers=headers,data={}, auth=(username, password))

res=json.loads(req.text)['result']

if res=='SUCCESS':

print '安装包构建成功'

else:

print '安装包构建失败'

sys.exit(1)

你可能感兴趣的:(python自动化部署jenkins_python+jenkins纯api实现自动化部署)