Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build job in Jenkins through python script

I want to Trigger an Build Job in Jenkins through python-jenkins package.I used the Below code for test_api job but it is not working.How can i Build through Script..

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()

It returns the Build url path after the build.

like image 762
Chandra Avatar asked Nov 25 '25 17:11

Chandra


1 Answers

I am using build_job method to trigger a job and it works for me.

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

Here is an example of mine:

#!/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})

You can get token from http://my-jenkins-url/user/foo/configure and then click show API Token....
For those who are new to Jenkins Python API, please reference to Jenkins Python API Link.

like image 100
mainframer Avatar answered Nov 28 '25 15:11

mainframer