0

I want to launch a build using the Jenkins API and get the build number of the launched build.

However, because of synchronizations considerations, I don't want 2 separate calls (like: launchJobBuild(); getJobLatestBuildNumber()), but instead I am looking for an API call that gets in return the specific buildNumber that was just created (in the reply content for example)

Does such an API call exist?

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
Yotam Eliraz
  • 107
  • 2
  • 8
  • possible duplicate of [Retrieve id of remotely triggered jenkins job](http://stackoverflow.com/questions/24507262/retrieve-id-of-remotely-triggered-jenkins-job) – Christopher Orr May 28 '15 at 08:54

3 Answers3

2

This question has already been asked: Retrieve id of remotely triggered jenkins job

If you're on v1.598 or above, the reponse will contain the build ID in the Location. Check out @morgwai's answer

Community
  • 1
  • 1
Somaiah Kumbera
  • 7,063
  • 4
  • 43
  • 44
0

You can get build no from response headers, it gives queueNo which is buildNo+1.

headerName = location

location →http://:/queue/item/54/ here 54 is queueItemNo. BuildNo would be queueItemNo-1 = 53

You will get this response when triggering job. http://:/job/<job_name>/buildWithParameters?repository=<repo_name>&branch=<branch_name>

For above url to work you would be requiring jenkins-crumb. http://13.232.201.103:8080/crumbIssuer/api/json use basic authentication with postman.

In reponse to this u will get { "_class": "hudson.security.csrf.DefaultCrumbIssuer", "crumb": "61b6dd4325d000f8b76e9d830fcab88e12d38315e4a7a858c70b838cf9f07d20", "crumbRequestField": "Jenkins-Crumb" }

Use Jenkins Crumb as header in point 1. Jenkins-Crumb:61b6dd4325d000f8b76e9d830fcab88e12d38315e4a7a858c70b838cf9f07d20

JobNo = response.getHeaders().get("location").get(0).split("/")[5]-1;

0

Shell script I developed to trigger a job, and get the build number :

JENKINS_EMAIL=<Email>
JENKINS_TOKEN=<API Key>
JENKINS_URL=<Jenkins Server URL>
JENKINS_JOB=<JOB>
        
# Trigger Job and get queue location

location=$(curl -X POST -s -I -u $JENKINS_EMAIL:$JENKINS_TOKEN "${JENKINS_URL}${JENKINS_JOB}/buildWithParameters?pass=ok" | grep location | awk '{ print $NF }')
location2=${location//[$'\t\r\n']}

# Wait till build number is generated

while true ; do
    
    buildnumber=$(curl -X GET -s -u $JENKINS_EMAIL:$JENKINS_TOKEN "${location2}api/json" | jq '.executable.number')

    if [[ $buildnumber != "null" ]]; then
        echo "Build Started. Build number is : "$buildnumber"
        break
    else
        echo "Still in Queue"
        sleep 1
    fi  
done

This is how i trigger a job, and get the build number. The while loop exists because, the build number will not be generated immediately. First the build will be queued. We need to get that queue location from the first api response and then using it get the build number using that while loop.

Modify the parameters used in first API as you require

anandhu
  • 686
  • 2
  • 13
  • 40