21

Is there a way to obtain the status of the Jenkins job in a variable during a Post-Build shell script?

I want to print out the message Build Status is $BUILD_URL :: $BUILD_STATUS, where $BUILD_STATUS is the status of the current completed build (e.g. ABORTED, SUCCESS, or FAILURE).

alex
  • 6,818
  • 9
  • 52
  • 103
Jose
  • 1,333
  • 5
  • 20
  • 38

6 Answers6

12

I know this question is quite dated, but I am able to use the currentBuild variable, e.g.:

   post {
     always {
       emailext body: "See ${BUILD_URL}", recipientProviders: [requestor()], subject: "Jenkins: ${JOB_NAME}: Build status is ${currentBuild.currentResult}"
     }
   }
mattgately
  • 932
  • 8
  • 22
8

If you can invoke a python script as a post-build step, you can try something like this:

import os, sys, json, codecs, urllib2

def main():
    url = "http://localhost:8080/job/jobName/lastBuild/api/json"
    try:
        fRead = urllib2.urlopen(url, None, 30); # 30 second timeout
    except:
        raise
    jsonResponse = json.loads(fRead.read());
    fRead.close();
    jobStatus = jsonResponse["result"]

main();

I have tested the url on my Jenkins and it works, but I haven't tested the script itself, so be wary. Obviously, substitute the port number and jobName as appropriate.

user3352495
  • 374
  • 3
  • 11
  • Thanks a lot for the response. But I keep getting the below error. urllib2.HTTPError: HTTP Error 403: Forbidden Do I need to use a login and password to access the Jenkins url ? Any idea how I can overcome the above error. – Jose Mar 08 '14 at 18:41
  • 1
    You can try using an API token from Jenkins: https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients This article explains how to get and use the token. Then modify the url in the script as necessary. – user3352495 Mar 08 '14 at 20:24
7

In my case, I had to include the API TOKEN here is what worked for me:

BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi/json | jq -r '.result')

which for me was:

BUILD_STATUS=$(curl --user robert:valueofmysecrettoken --silent $BUILD_URLapi/json | jq -r '.result')
Robert Misior
  • 106
  • 1
  • 3
  • Just using python to parse the JSON instead of jq: curl --silent --user USER:TOKEN $BUILD_URLapi/json | python -c 'import sys, json; print json.load(sys.stdin)["result"]' – burkestar Mar 13 '18 at 08:46
6

Same as the answer from user3352495 but don't use any python dependencies.

I'm using jenkins own API to get the build status while the job is running, which works like a charm. Be aware that i'm using JQ To parse the json response.

To get this to work simply add a shell script and execute the following command: BUILD_STATUS=$(curl --silent ${BUILD_URL}api/json | jq -r '.result')

Execute shell step

Which results in the following:

Job console

MrSpock
  • 1,470
  • 16
  • 10
  • 1
    This is nice. You can append the URL with `?tree=result` to get something like `{"_class":"hudson.model.FreeStyleBuild","result":"SUCCESS"}` - I'm then just checking for the substring `SUCCESS` to determine the final status (as much as I like `jq` I don't feel like installing it on all the build slaves) – Max Chuquimia May 07 '18 at 06:11
3

On Jenkins 2.263.2, I was able to get the job status using the following variable.

${BUILD_STATUS}

Used it as a variable in a post-build plugin.

Abhinav Thakur
  • 383
  • 2
  • 7
1

To add to the answer since it took me way too long to find a decent solution for this.

You can use the url to check on ANY job from ANY pipeline. If the curl is nagging you about the self signed SSL cert make sure to add the -k option to skip the check.

Also don't forget that if you are running the jenkins server behind a security group, you'll need to allow access to the port you are running your jenkins from.

this was my solution running from a shell step in a pipeline

BUILD_STATUS=$(curl -k --user USER:apiToken --silent https://your.jenkins.url/job/yourJobName/lastBuild/api/json | jq -r '.result')
STATUS="SUCCESS"

            if [ $BUILD_STATUS == $STATUS ];
            then
                echo 'big yay'
                exit 0
            else
                exit 1
            fi
fedezubo
  • 11
  • 1