Trigger Jenkins-job via curl and retrieve concrete url of running job

It’s not that easy as it seems to obtain the conrete build url from Jenkins after invocation via curl. This is because of the scheduling/queuing architecture of jenkins, so here’s a small helper script that triggers the job and waits until it gets executed in order to return the concrete build url.

#!/bin/bash

JENKINS_BASE_URL='http://localhost:8080'
JENKINS_JOBNAME='testjob_A'
JENKINS_BUILD_DELAY_SEC='3'
JENKINS_QUERY_RETRY_SEC='2'
JENKINS_INVOKE_URL="${JENKINS_BASE_URL}/job/${JENKINS_JOBNAME}/build?param=value&delay=${JENKINS_BUILD_DELAY_SEC}sec"


# step 1: invoke the build by posting <job-url>/build 
# will return a response with a "Location" header. This value points to the item in Jenkins' global build queue. 
printf ">>> invoking build: ${JENKINS_INVOKE_URL} \n"
QUEUE_ITEM_URL=$(curl -s -i -X POST "${JENKINS_INVOKE_URL}" | grep Location | cut -d ' ' -f 2 | xargs )
# strip last slash from url for convenience
QUEUE_ITEM_URL=${QUEUE_ITEM_URL::-2}
printf ">>> successfully invoked, url of queued item: ${QUEUE_ITEM_URL} \n"


# step 2: requesting details on queued item via its json api (jq has to be installed)
# will either return a json with or without "/executable" field in json root.
# if it is missing it means, the item is still in queue only and not yet startet
# if it is present it contains the url to the running job.
BUILD_ITEM_URL='null'
while :
do
	printf ">>> requesting details on queued item \n"
	# python -m json.tool is only required if you need pretty printed jsons 🙂
	QUEUE_ITEM_DETAILS=$(curl -s "${QUEUE_ITEM_URL}/api/json" | python -m json.tool )
	BUILD_ITEM_URL=$(echo $QUEUE_ITEM_DETAILS | jq '.executable.url')

	if [ "$BUILD_ITEM_URL" == "null" ]
	then
		printf "job not yet started, retrying ... \n"
		sleep $JENKINS_QUERY_RETRY_SEC
	else 
		#printf "job started \n"
		break
	fi

done
printf ">>> job has been started, build url is: ${BUILD_ITEM_URL} \n"

Example of a queue-item that waits for execution:

{
    "actions": [
        {
            "causes": [
                {
                    "shortDescription": "Gestartet durch Benutzer anonymous",
                    "userId": null,
                    "userName": "anonymous"
                }
            ]
        }
    ],
    "blocked": false,
    "buildable": false,
    "id": 77,
    "inQueueSince": 1483832795680,
    "params": "",
    "stuck": false,
    "task": {
        "color": "blue",
        "name": "testjob_A",
        "url": "http://localhost:8080/job/testjob_A/"
    },
    "timestamp": 1483832805680,
    "url": "queue/item/77/",
    "why": "In Ruhe-Periode. Endet in 9,9 Sekunden"
} 

Example of a queue-item that has already been started:

{
    "actions": [
        {
            "causes": [
                {
                    "shortDescription": "Gestartet durch Benutzer anonymous",
                    "userId": null,
                    "userName": "anonymous"
                }
            ]
        }
    ],
    "blocked": false,
    "buildable": false,
    "cancelled": false,
    "executable": {
        "number": 62,
        "url": "http://localhost:8080/job/testjob_A/62/"
    },
    "id": 77,
    "inQueueSince": 1483832795680,
    "params": "",
    "stuck": false,
    "task": {
        "color": "blue",
        "name": "testjob_A",
        "url": "http://localhost:8080/job/testjob_A/"
    },
    "url": "queue/item/77/",
    "why": null
}