Skip to content Skip to sidebar Skip to footer

How To Trigger Jenkins Build Using The Python Package JenkinsAPI?

I have a Jenkins job set up with the name Test2 which I can build from Jenkins web interface. Now I want to trigger that build using JenkinsAPI. I have only been able to find examp

Solution 1:

It's not clear to me why your example isn't working, but I find the JenkinsAPI documentation confusing in general so perhaps I just don't get it.

I've found that to get a particular build directly, you can use the get_build method in the api package. The arguments are in a different order:

import jenkinsapi
b = jenkinsapi.api.get_build("http://localhost:8080", "Test 1", 1)

This is fine for existing builds, started through some other means. But it sounds like you actually want to trigger a build. In that case, get the job through a Jenkins instance and use the invoke method:

import jenkinsapi
jenkins = jenkinsapi.jenkins.Jenkins("http://192.168.99.100:8080")
job = jenkins["Test 1"]
job.invoke(block=True)

In my opinion, there is little benefit to using a confusingly documented interface package (why are there multiple ways to get a build?) when the plain Jenkins REST API can be accessed via the requests package as described by massiou's answer.


Solution 2:

Instead of using jenkinsapi module, you could trigger your job simply request jenkins REST api like follows:

import requests 

# Case job has no parameter
job_url = "http://localhost:8080/job/test2/build"

# Job with parameters
job_url = "http://localhost:8080/job/test2/buildWithParameters?param1=value1&param2=value2"

status = requests.get(job_url)

Post a Comment for "How To Trigger Jenkins Build Using The Python Package JenkinsAPI?"