Python调用Jenkins接口批准/拒绝Pipeline流程
import jenkins,requests,urllib jenkins_server_url = 'http://jenkins.jk.com/' user_id = 'approver' api_token = '1187e29' # api_token = 'password' job_name = '测试账号管理' build_id = 220 server=jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token) # print(dir(server)) try: #获取job状态 jobinfor = server.get_build_info(job_name,build_id) if jobinfor['building'] == True: #批准/拒绝任务 url = 'http://%s:%s@jenkins.jk.com/job/%s/%s/input/Inputap/abort' % (user_id,api_token,job_name,build_id) #UserInput返回非True和None,表示拒绝 此处Inpputap的ID名称,首字母必须大写 url = 'http://%s:%s@jenkins.jk.com/job/%s/%s/input/Inputap/proceedEmpty' % (user_id,api_token,job_name,build_id) #UserInput返回None,表示批准 # url = 'http://%s:%s@autoit.xin.com/job/%s/%s/wfapi/pendingInputActions' % (user_id,api_token,job_name,build_id) # # url = 'http://%s:%s@autoit.xin.com/job/%s/%s/wfapi/runs' % (user_id,api_token,job_name,build_id) postdata={"inputId":"Inputap","json":'{}',"proceed":"同意"} #input定义: id: 'inputap', ok:"同意" postdata = urllib.parse.urlencode(postdata) #对特殊符号和中文编码为URL字符 url = 'http://%s:%s@jenkins.jk.com/job/%s/%s/wfapi/inputSubmit?%s' % (user_id,api_token,job_name,build_id,postdata) #UserInput返回True,表示批准 print(url) m=requests.post(url) print(m.reason) print(m.ok) except Exception as ee: print(ee)
import jenkins,requests jenkins_server_url = 'http://jenkins.jk.com/' user_id = 'approver' api_token = 'abccd12187' # api_token = 'password' job_name = '测试账号管理' build_id = 206 server=jenkins.Jenkins(jenkins_server_url, username=user_id, password=api_token) # print(dir(server)) try: #获取job状态 jobinfor = server.get_build_info(job_name,build_id) print(jobinfor['result'],jobinfor['building']) if jobinfor['building'] == True: #批准/拒绝任务 url = 'http://%s:%s@jenkins.jk.com/job/%s/%s/input/Inputap/abort' % (user_id,api_token,job_name,build_id) #UserInput返回非True和None 此处Inpputap的ID名称,首字母必须大写 # url = 'http://%s:%s@jenkins.jk.com/job/%s/%s/input/Inputap/proceedEmpty' % (user_id,api_token,job_name,build_id) #UserInput返回None print(url) m=requests.post(url) print(m.reason) print(m.ok) except Exception as ee: print(ee)