通过python脚本在Jenkins中构建工作

时间:2015-07-30 10:57:53

标签: python-2.7 jenkins

我想通过python-jenkins包在Jenkins中触发一个构建作业。我使用了下面的代码来执行test_api但是它无法正常工作。如何通过脚本构建..

import jenkins
j = jenkins.Jenkins('http://your_url_here', 'username', 'password')
print j.get_jobs()
url=j.build_job_url('test_api', parameters=None, token=None)
print url
last_build_number = j.get_job_info('test_api')['lastCompletedBuild'] ['number']
print "last_build_number",last_build_number
build_info=j.get_build_info('test_api',last_build_number)
if build_info['result']=='SUCCESS':
    print " Build Success "
else:
    print " Build Failed "
    log=j.get_build_console_output('test_api',last_build_number)
    f=open('log_buildFail.txt','w')
    f.write(log)
    f.close()

它在构建之后返回Build url路径。

1 个答案:

答案 0 :(得分:3)

我使用build_job方法来触发工作,它对我有用。

build_job(name, parameters=None, token=None)
    Trigger build job.

    Parameters: 
    name – name of job
    parameters – parameters for job, or None, dict
    token – Jenkins API token

以下是我的一个例子:

#!/usr/bin/python
# coding: utf-8
import jenkins
ci_jenkins_url = "http://my-jenkins-url/"
username = "foo"
token = "fa818f4f90621a4e69de563516098689"
job = "test-job"
j = jenkins.Jenkins(ci_jenkins_url, username=username, password=token)

if __name__ == "__main__":
      j.build_job(job, {'token': token})

您可以从http://my-jenkins-url/user/foo/configure获取令牌,然后点击show API Token... 对于那些刚接触Jenkins Python API的人,  请参考Jenkins Python API Link