如何将环境变量传递给 Jenkins 管道工具部分

How can I pass environment variables to Jenkins Pipeline Tools section

在Jenkins > Global Tool Configuration > JDK installation > 我添加了JDK7,它的名字是oracle-7u80;同样在Maven安装下,我添加了Maven 3.5 install并命名为mvn.

现在我在管道脚本中使用以上两个安装:

    pipeline {
    agent any
    tools {
        maven 'mvn'
        jdk 'oracle-7u80'
    }
    stages {
        stage('Example') {
            steps {
                
                }
            }
        }
    }

我不想在管道的“工具”部分中对 jdk 和 Maven 值进行硬编码。我想通过环境变量或属性传递这些值,以便我可以在外部管理它们。

有没有办法使用环境变量将定义给 Maven 和 jdk 的值(mvn 或 oracle-7u80)传递给工具?

就像我需要在 Steps/Script 部分中注入一个值一样,在 Jenkins 管道中,我可以在环境变量中全局定义或使用 Jenkins 项目 配置 一般的 勾选为 运行 准备环境 勾选保留 Jenkins 环境变量 我可以使用属性文件定义在属性内容中提供环境变量。

我的意图是得到这样的格式:

pipeline {
    agent any
    tools {
        maven '${MVN_VERSION}'
        jdk '${ORACLE_VERSION'}
    }
    stages {
        stage('Example') {
            steps {
            
                }
            }
        }
    }

流水线项目通常与 Jenkinsfile来自 SCM 的流水线脚本一起使用 流水线 定义 下拉列表)将源代码版本及其构建配置相互绑定以实现可重现的构建。

在构建之前从外部注入构建工具版本与这个想法相矛盾。

我也不确定这在概念上是否可行,因为来自外部的(环境)变量值设置在 stages ... script 中,这是一个与 tools 完全不同的声明分支。但是,嘿,它被称为 声明性 管道,不是强制性的,所以顺序应该无关紧要......理论上。我试试看。

一般情况下,将外部值传递给内部变量见Pipeline: Nodes and Processes, sh: Shell Script and also the answer to the question

Maven版本注入试试

pipeline {
    agent any
    
    tools {
        maven "${MVN_VERSION}"
    }
    
    stages {
        stage('Try: Maven version injected') {
            steps {
                script {
                    env.MVN_VERSION = sh script: 'echo "Maven 3.8.1"', returnStdout: true
                }
                echo "${MVN_VERSION}"                
            }
        }
    }
}

符合预期:

[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: MVN_VERSION for class: groovy.lang.Binding
...

我想到的另一个想法是通过 Choice parameter 使这个项目参数化为两个参数(例如 MVN_GLOBAL_TOOL_NAMEJDK_GLOBAL_TOOL_NAME),例如,这有效:

pipeline {
    agent any
    
    tools {
        maven "${MVN_GLOBAL_TOOL_NAME}" // coming from parameterized project's build parameter
    }
    
    stages {
        stage('Maven tool as build parameter') {
            steps {
                echo "MVN_GLOBAL_TOOL_NAME=${MVN_GLOBAL_TOOL_NAME}"                
            }
        }
    }
}

控制台输出

[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Maven version as build parameter)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
MVN_GLOBAL_TOOL_NAME=Maven 3.8.1
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

另见 ${JENKINS_URL}/job/${JOB_NAME}/api/:

Perform a build

If the build has parameters, post to this URL [Link note: ${JENKINS_URL}/job/${JOB_NAME}/buildWithParameters] and provide the parameters as form data.

另请参阅:${JENKINS_URL}/env-vars.html/