在脚本管道中,如何获取Jenkins构建参数

时间:2018-04-04 15:22:45

标签: jenkins groovy jenkins-pipeline

我的系统中有一组远程节点,每个节点都有一组已定义的标签。我需要一个Jenkins脚本,可以打印出在所有具有匹配标签的远程节点上运行的Build的参数。

我可以在远程节点上获取Executor对象,但我不知道如何访问当前在该远程节点上运行的Build参数。

import jenkins.model.Jenkins

def GetRemoteBuildParameters(host){

    // find all remote nodes with matching label(s)
    def label = Jenkins.instance.getLabel(host)
    def nodes = label.getNodes()

    // our label should match with >= 1 node, but check to be sure
    assert nodes.size() > 0

    // TODO: print all parameters of build that is running on each node
    nodes.each{ node ->

        // only care about online nodes
        if (node.toComputer().isOnline()){

            // a node may have > 1 Executor
            node.toComputer().getExecutors().each{ e ->

                // at this point, 'e' is an Executor
                // but I don't know how to access the 'Build' running on 'e'
                println e.getDisplayName()
                println e.getCurrentExecutable()

                // I think the following line gets me access to a Job,
                // not a Build
                println e.getCurrentExecutable().getParent()
            }
        }
    }
}
GetRemoteBuildParameters("foo")

在上面的例子中,如果节点A和B都有标签'foo',我想在A和B上打印出Builds的所有参数。注意,现在我假设A和B都在运行Build;我稍后会处理这个问题。

2 个答案:

答案 0 :(得分:0)

您可以使用环境变量BUILD_NUMBER。要打印当前版本号,您可以像这样使用

echo "${env.BUILD_NUMBER}"

答案 1 :(得分:0)

相关问题