Jenkins EnvInject 插件 + 管道作业

Jenkins EnvInject plugin + Pipeline job

我想在我的管道作业中使用 EnvInject 插件。因此,我可以设置复选框 "Prepare an environment for the run",但没有像自由式作业那样的操作 "Inject environment variables"。 我在 "Properties Content" 块中声明了我的变量:

如何使用 EnvInject 在管道作业中注​​入环境变量?

如果您在 "Properties Content" 块中声明了以下变量:

param1=value1
param2=value2

然后您可以在此处将它们放入管道中,因此:

//do something
def par1 = env.param1
def par2 = env.param2

Environment Injector 插件在 Jenkins 管道中的使用有限。我建议使用内置的 'environment' Jenkins pipeline feature instead. Here a complete declarative Jenkins pipeline 脚本示例:

pipeline {
    agent any

    // Inject environment variable for whole pipeline script
    environment {
        param1 = 'value1'
    }

    stages {
        stage('Some stage in my pipeline') {
            // Inject environment variable only for this step
            environment {
                param2 = 'value2'
            }

            steps {
                // Use inherited environment variable
                echo "My 'PATH'   environment variable: '${env.PATH}'"
                // Use injected environment variables
                echo "My 'param1' environment variable: '${env.param1}'"
                echo "My 'param2' environment variable: '${env.param2}'"
                
                // Use injected environment variables in a Groovy script block
                script {
                    def value = env.param1
                    println "par1 value: '${value}'"
                    value = env.param2
                    println "par2 value: '${value}'"
                }
            }
        }
    }
}

pipeline暂不支持,请参考下ticket,还有一些不错的替代方式:

https://issues.jenkins-ci.org/browse/JENKINS-42614