1. 程式人生 > >jenkins(搭建在linux下的) python

jenkins(搭建在linux下的) python

python實現執行jenkins工程:

import jenkins
import time
import sys



def run():
	server = jenkins.Jenkins("http://10.7.0.20:9000", username='zhangyazhong', password='123456')
	print(server.get_whoami())
	jobs = server.get_jobs()
	jobs_count = server.jobs_count()
	print(jobs_count)
	print(jobs)
	# server.create_job('empty', jenkins.EMPTY_CONFIG_XML)

	print(jobs[0]['name'])

	last_build_info = server.get_job_info(jobs[0]['name'])
	print(last_build_info)

	print("*********************")
	print(jobs[4]['name'])

	test_info = server.get_job_info(jobs[4]['name'])
	print(test_info)
	param = {'delay':'0sec'}
	enable_result = server.enable_job(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'])
	print(enable_result)
	# my_job = server.get_job_config(jobs[0]['name'])
	# number = server.build_job( last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'] ) #執行第一個任務, parameters=param
	number = server.build_job(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'])
	print(number)
	running_job = server.get_running_builds()


	print( running_job)
	while True:

	    time.sleep(1)
	    print ('check running job...')
	    if len(server.get_running_builds()) == 0:
	        break
	    else:
	        time.sleep(20)

	# last_build_number = server.get_job_info(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'])['lastCompletedBuild']['number']

	lastbuild_number = server.get_job_info(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'])['lastBuild']['number']
	build_info = server.get_build_info(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'], lastbuild_number)
	test_result = server.get_build_info(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'],lastbuild_number)['building']
	try:
		print("判斷是否job存在:")
		ll = server.assert_job_exists(last_build_info['name'] + '/'+last_build_info['jobs'][0]['name'], exception_message='job %s does not exist.......')
		print(ll)
	except jenkins.JenkinsException as f:
		print (f)




	print('++++++++++++++++++')
	print(test_result)
	print('++++++++++++++++++++++++++++++++')
	build_result = build_info['result']
	print ('Build result is ' + build_result)
	if build_result == 'SUCCESS':
	    sys.exit(0)
	else:
	    sys.exit(-1)


if __name__ == '__main__':
	run()