如何在管道中的下游作业中获取上游作业具有 运行 的节点名称

How can I get the node name where the upstream job has run in the downstream job in pipeline

我有多个带有 'build_run' 标签的节点(例如,node1、node2、node3)。因此,当我 运行 此管道时,我无法确定 'Build' 和 'Run' 作业在同一节点中具有 运行。 'Build' 可能发生在 'node1' 中,而 'Run' 可能发生在 'node3' 中。我希望 Build 和 运行 发生在同一个节点中。但我不想硬编码相同。 我想知道 'Build' 哪个节点已拾取。这样我就可以将它作为节点参数传递给 运行。 我该如何解决这个问题?

stage('Build, run) {
    for(int i=0; i<4; ++i){
        def builds = { 
            stage('Build') {
                build job: 'Build', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: 'build_run']]
            }

            stage('Run') {
               build job: 'Run', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: 'build_run']]
            }
        }
    }
}
parallel builds

我使用了类似下面的东西,它对我有用。 (使用 rawBuild.getEnvironment()['NODE_NAME'] 获取作业具有 运行 的节点。)

    def node_to_use = ""
    stage('Build, run) {
        for(int i=0; i<4; ++i){
            def builds = { 
                stage('Build') {
                    def build_var = build job: 'Build', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: 'build_run']]
                    node_to_use = build_var.rawBuild.getEnvironment()['NODE_NAME']
                }

                stage('Run') {
                   build job: 'Run', parameters: [[$class: 'LabelParameterValue', name: 'TestMachine', label: node_to_use]]
                }
            }
        }
    }
    parallel builds