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 example code to do other things but the API Documentation mentions the Build class. So I thought I would try to instantiate it and then wait for it to complete (hoping that would also trigger the actual build) but I am getting rather cryptic errors much earlier than that. What am I doing wrong?
import jenkinsapi
b = jenkinsapi.build.Build("http://localhost:8080", 1, "test2")
b.block_until_complete()
Gives me:
Traceback (most recent call last):
File "/Users/jonathan/Genetta/Eclipse_Django_workspace/FOO/foo/TriggerBuild.py", line 2, in <module>
b = jenkinsapi.build.Build("http://localhost:8080", 1, "test2")
File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/build.py", line 58, in __init__
JenkinsBase.__init__(self, url)
File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 35, in __init__
self.poll()
File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 59, in poll
data = self._poll(tree=tree)
File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/build.py", line 65, in _poll
return self.get_data(url, params={'depth': self.depth}, tree=tree)
File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/jenkinsbase.py", line 72, in get_data
requester = self.get_jenkins_obj().requester
File "/Users/jonathan/anaconda/lib/python2.7/site-packages/jenkinsapi/build.py", line 371, in get_jenkins_obj
return self.job.get_jenkins_obj()
AttributeError: 'str' object has no attribute 'get_jenkins_obj'
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.
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¶m2=value2"
status = requests.get(job_url)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With