获取作业的构建状态作为构建后变量

问题描述 投票:13回答:3

是否有一种方法可以在构建后的Shell脚本中获取变量中Jenkins作业的状态?

我想打印出消息Build Status is $BUILD_URL :: $BUILD_STATUS,其中$BUILD_STATUS是当前已完成构建的状态(例如ABORTEDSUCCESSFAILURE)。

jenkins jenkins-plugins
3个回答
6
投票

就我而言,我必须包含API令牌,这对我有用:

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

对我来说是:

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

5
投票

如果您可以在构建后的步骤中调用python脚本,则可以尝试如下操作:

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();

我已经在我的Jenkins上测试了URL,并且可以正常工作,但是我还没有测试脚本本身,所以要小心。显然,适当地替换端口号和jobName。


5
投票

与user3352495的答案相同,但不使用任何python依赖项。

我正在使用jenkins自己的API来在作业运行时获取构建状态,这就像一种魅力。请注意,我正在使用JQ解析json响应。

要使其正常工作,只需添加一个shell脚本并执行以下命令:BUILD_STATUS=$(curl --silent ${BUILD_URL}api/json | jq -r '.result')

Execute shell step

将得到以下结果:

Job console

© www.soinside.com 2019 - 2024. All rights reserved.