I have a script to trigger a job on Jenkins remotely using a token. Here is my script:
JENKINS_URL='http://jenkins.myserver.com/jenkins'
JOB_NAME='job/utilities/job/my_job'
JOB_TOKEN='my_token'
curl "${JENKINS_URL}/${JOB_NAME}/buildWithParameters?token=${JOB_TOKEN}"
After I run it, I get following response:
* Hostname was NOT found in DNS cache
* Trying 10.5.187.225...
* Connected to jenkins.myserver.com (10.5.187.225) port 80 (#0)
> GET /jenkins/job/utilities/job/my_job/buildWithParameters?token=my_token HTTP/1.1
> User-Agent: curl/7.37.1
> Host: jenkins.myserver.com
> Accept: */*
>
< HTTP/1.1 201 Created
* Server nginx/1.6.2 is not blacklisted
< Server: nginx/1.6.2
< Date: Tue, 03 Feb 2015 23:40:47 GMT
< Content-Length: 0
< Location: http://jenkins.myserver.com/jenkins/queue/item/91/
< Connection: keep-alive
< Cache-Control: private
< Expires: Wed, 31 Dec 1969 16:00:00 PST
<
* Connection #0 to host jenkins.myserver.com left intact
I noticed that it returns the queue url in the header: http://jenkins.myserver.com/jenkins/queue/item/91. But I don't know how I should use this return url.
1) I am wondering if anyone knows how I can check the status for the job that I just created?
2) Since the above response does not return the job #, I can't really use this api call:
curl http://jenkins.myserver.com/jenkins/job/utilities/job/my_job/8/api/json
to check for status. So where can I get the job name and job number after I get the location url from the above response?
Thanks
When you trigger a job, the job is placed into the queue. The actual build is created only when it starts running and at that point the build gets a build number. If all your executors are busy, it can sometimes take a long time before the build is created and starts running.
The only way to get the build number when triggering a job, is to use the "build" command of the Jenkins CLI. If you use the -w option, the command will not return until the build starts and then it will print "Started build #N"
You do not actually need the java cli.jar, just an ssh client is enough. See https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+SSH
Other than that there is no known solution. You might be able to search through the builds and find a one that was triggered around the time your triggered the job, but that's a lot of work.
I solved this problem using polling of the Jenkins server. When a job is started remotely the return headers has the job queue URL. Using this one can make more API calls to get status.
Steps:
I used python and the Requests module to do this
Output:
JSON from a Jenkins queue once its job has started:
You can use Jenkins API for this. A sample Python script:
Refer http://www.easyaslinux.com/tutorials/devops/how-to-check-build-status-of-jenkins-job-using-python-script/ for detailed explanation
I had similar problem to get state with rest api only.
this was my solution (it is a weak and not stable solution!):