0

在 Jenkins 自由式作业中(在较旧的 1.6x 版本上,不支持 2.x 管道作业),如果构建状态从to恢复(!),我想运行一个 shell 命令(curl -XPOST ...)作为构建后步骤。FAILEDSUCCESS

但是,我所知道的所有用于确定构建状态的插件只能在当前构建状态为FAILEDSUCCESS但不考虑与上次构建相比是否恢复时才能执行某些操作。

有什么方法可以实现这一点,例如使用Groovy Post build插件和一些脚本行?

4

2 回答 2

2

我发现这样的事情是一个很好的方法。你可以建立一些有趣的逻辑,“currentBuild”变量在这里有一些不错的文档:currentBuild variable doc

    script {
       if ( ( currentBuild.resultIsBetterOrEqualTo("SUCCESS") && currentBuild.previousBuild.resultIsWorseOrEqualTo("UNSTABLE") ) || currentBuild.resultIsWorseOrEqualTo("UNSTABLE")) {
         echo "If current build is good, and last build is bad, or current build is bad"
       }
    }
于 2020-10-22T00:27:21.120 回答
1

与此同时,我找到了实现这一目标的方法。它不一定漂亮,我仍然很欣赏替代解决方案:)

首先,需要一个插件,它可以让您在 Post Build 步骤中执行 shell 命令。可能有不同的,我为此使用PostBuildScript 插件

然后,创建一个“执行一组脚本”后期构建步骤,将要执行的步骤设置为Build step并选择Execute shell,对我来说这看起来像这样: Jenkins 后期构建步骤

在那里,我运行以下 shell 脚本行,这些脚本行使用我的 Jenkins 服务器的 REST API 和 Python 单线器(您也可以使用jq或其他东西)来确定当前构建的状态以及最后一个构建的状态完成构建:

statusOfCurrentBuild=$(curl --silent "${BUILD_URL}api/json" | python -c "import sys, json; print json.load(sys.stdin)['result']")
statusOfLastBuild=$(curl --silent "${JOB_URL}/lastCompletedBuild/api/json" | python -c "import sys, json; print json.load(sys.stdin)['result']")

if [ "${statusOfCurrentBuild}" == "SUCCESS" ] && [ "${statusOfLastBuild}" == "FAILURE" ]
then
    echo "Build was fixed"
    # do something interesting here
fi

根据您的 Jenkins 设置,使用 REST API 可能需要身份验证。

于 2018-02-16T10:49:34.433 回答