Getting a job's build status as post-build variable

烂漫一生 提交于 2019-12-12 08:27:29

问题


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).


回答1:


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')



回答2:


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.




回答3:


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')

Which results in the following:



来源:https://stackoverflow.com/questions/22264431/jenkins-job-build-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!