Issue
I am trying to trigger a Jenkins build and wait until it has completed.
When I start Jenkins via its REST API, I get a resonse with a URL like http://JENKINS_URL:8080/queue/item/36285/
.
When I try to access it, I always the get the 404 error. My test builds are rather short therefore it is possible that they are finished before I exit the debugger.
Question: Is it guaranteed that if a URL like http://JENKINS_URL:8080/queue/item/36285/
results in a 404 response, then the corresponding Jenkins build is finished? That is, can I use this URL to find out whether or not it is finished (as long as the URL returns a status code not equal to 404, the build is running)?
Solution
The /queue/item/
give you items in queue, that waiting to start and 404 means item not in the queue, can be canceled or in execution. Job execution can be in running, finished or canceled statuses.
You can use queueId
(36285
in your example), get job details and get build status for the particular queueId
. In the response for the request below is job details with builds. You can find build you need by queueId
in builds
and get building
(boolean) and result
status:
JENKINS_URL+"/job/"+ artifactToBuild +"/api/json?depth=1'
Response example for JENKINS_URL+"/job/"+ artifactToBuild +"/api/json?depth=1'
:
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"actions": [],
"description": "",
"displayName": "my_job_name",
"displayNameOrNull": null,
"fullDisplayName": "my_job_name",
"fullName": "my_job_name",
"name": "my_job_name",
"url": "http://localhost:8080/job/my_job_name/",
"buildable": true,
"builds": [
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#1",
"duration": 1651,
"estimatedDuration": 1671,
"executor": null,
"fullDisplayName": "my_job_name #1",
"id": "1",
"keepLog": false,
"number": 1,
"queueId": 103,
"result": "SUCCESS",
"timestamp": 1580946018635,
"url": "http://localhost:8080/job/my_job_name/1/",
"changeSets": [],
"culprits": [],
"nextBuild": {
"number": 2,
"url": "http://localhost:8080/job/my_job_name/2/"
},
"previousBuild": null
}
],
"color": "aborted",
"firstBuild": {},
"healthReport": [
{
"description": "Build stability: No recent builds failed.",
"iconClassName": "icon-health-80plus",
"iconUrl": "health-80plus.png",
"score": 100
}
],
"inQueue": false,
"keepDependencies": false,
"lastBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#10",
"duration": 1238044,
"estimatedDuration": 1671,
"executor": null,
"fullDisplayName": "my_job_name #10",
"id": "10",
"keepLog": false,
"number": 10,
"queueId": 587,
"result": "ABORTED",
"timestamp": 1581534463735,
"url": "http://localhost:8080/job/my_job_name/10/",
"changeSets": [],
"culprits": [],
"nextBuild": null,
"previousBuild": {
"number": 9,
"url": "http://localhost:8080/job/my_job_name/9/"
}
},
"lastCompletedBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#10",
"duration": 1238044,
"estimatedDuration": 1671,
"executor": null,
"fullDisplayName": "my_job_name #10",
"id": "10",
"keepLog": false,
"number": 10,
"queueId": 587,
"result": "ABORTED",
"timestamp": 1581534463735,
"url": "http://localhost:8080/job/my_job_name/10/",
"changeSets": [],
"culprits": [],
"nextBuild": null,
"previousBuild": {
"number": 9,
"url": "http://localhost:8080/job/my_job_name/9/"
}
},
"lastFailedBuild": null,
"lastStableBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#2",
"duration": 1671,
"estimatedDuration": 1671,
"executor": null,
"fullDisplayName": "my_job_name #2",
"id": "2",
"keepLog": false,
"number": 2,
"queueId": 105,
"result": "SUCCESS",
"timestamp": 1580946027378,
"url": "http://localhost:8080/job/my_job_name/2/",
"changeSets": [],
"culprits": [],
"nextBuild": {
"number": 3,
"url": "http://localhost:8080/job/my_job_name/3/"
},
"previousBuild": {
"number": 1,
"url": "http://localhost:8080/job/my_job_name/1/"
}
},
"lastSuccessfulBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#2",
"duration": 1671,
"estimatedDuration": 1671,
"executor": null,
"fullDisplayName": "my_job_name #2",
"id": "2",
"keepLog": false,
"number": 2,
"queueId": 105,
"result": "SUCCESS",
"timestamp": 1580946027378,
"url": "http://localhost:8080/job/my_job_name/2/",
"changeSets": [],
"culprits": [],
"nextBuild": {
"number": 3,
"url": "http://localhost:8080/job/my_job_name/3/"
},
"previousBuild": {
"number": 1,
"url": "http://localhost:8080/job/my_job_name/1/"
}
},
"lastUnstableBuild": null,
"lastUnsuccessfulBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"actions": [],
"artifacts": [],
"building": false,
"description": null,
"displayName": "#10",
"duration": 1238044,
"estimatedDuration": 1671,
"executor": null,
"fullDisplayName": "my_job_name #10",
"id": "10",
"keepLog": false,
"number": 10,
"queueId": 587,
"result": "ABORTED",
"timestamp": 1581534463735,
"url": "http://localhost:8080/job/my_job_name/10/",
"changeSets": [],
"culprits": [],
"nextBuild": null,
"previousBuild": {
"number": 9,
"url": "http://localhost:8080/job/my_job_name/9/"
}
},
"nextBuildNumber": 11,
"property": [
{
"_class": "hudson.plugins.jira.JiraProjectProperty"
},
{
"_class": "org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty"
}
],
"queueItem": null,
"concurrentBuild": false,
"resumeBlocked": false
}
Answered By - Sers
Answer Checked By - Robin (JavaFixing Admin)