如何在 jenkins 管道中的触发器部分使用 env 变量?

How to use env variable inside triggers section in jenkins pipeline?

正在读取节点标签和 triggerConfigURL 的属性文件,节点标签有效,但我无法从环境中读取和设置 triggerConfigURL。

def propFile = "hello/world.txt" //This is present in workspace, and it works.
pipeline {
    environment {
        nodeProp = readProperties file: "${propFile}"
        nodeLabel = "$nodeProp.NODE_LABEL"
        dtcPath = "$nodeProp.DTC"
    }
    agent { label env.nodeLabel } // this works!! sets NODE_LABEL value from the properties file.
    triggers {
         gerrit dynamicTriggerConfiguration: 'true',
                triggerConfigURL: env.dtcPath, // THIS DON'T WORK, tried "${env.dtcPath}" and few other notations too.
                serverName: 'my-gerrit-server',
                triggerOnEvents: [commentAddedContains('^fooBar$')]
    }
    stages {
        stage('Print Env') {
            steps {
                script {
                    sh 'env' // This prints "dtcPath=https://path/of/the/dtc/file", so the dtcPath env is set.
                }
            }
        }

运行作业后,配置如下:

envtriggers 子句中 Jenkins 运行 一个接一个,看起来你已经通过实验证明 triggers 运行第一和 env 第二。它看起来也像在 env 之后的 agent 运行s。

虽然我不知道为什么程序员会做出这个特定的决定,但我认为你遇到了一个先有鸡还是先有蛋的问题,你想使用文件定义管道但只能读取定义管道后的文件 运行ning。

话虽如此,以下方法可能有效:

def propFile = "hello/world.txt"
def nodeProp = null

node {
    nodeProp = readProperties file: propFile
}

pipeline {
    environment {
        nodeLabel = nodeProp.NODE_LABEL
        dtcPath = nodeProp.DTC
    }
    agent { label env.nodeLabel } 
    triggers {
         gerrit dynamicTriggerConfiguration: 'true',
                triggerConfigURL: nodeProp.DTC, 
//etc.