Jenkinsfile 中的 Jenkins 全局环境变量

标签 jenkins groovy jenkins-pipeline

如何在 Jenkinsfile 中调用全局环境变量?
例如,如果我有一个变量 -

 name:credentialsId 
 value:xxxx-xxxx-xxxxx-xxxxxxxxx

我如何在 groovy 脚本中使用它?

我试过 ${credentialsId} ,但它没有用。它只会给出错误:
java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, ........

最佳答案

在 Jenkinsfile 中,您有“Working with the Environment ”,其中提到:

The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env,


语法是 ${env.xxx}如:
node {
    echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
}
另见“Managing the Environment”。

How can I pass the Global variables to the Jenkinsfile?
When I say Global variables - I mean in

Jenkins -> Manage Jenkins -> Configure System -> Global properties -> Environment variables
见“Setting environment variables

Setting an environment variable within a Jenkins Pipeline can be done with the withEnv step, which allows overriding specified environment variables for a given block of Pipeline Script, for example:

Jenkinsfile (Pipeline Script)

node {
    /* .. snip .. */
    withEnv(["NAME=value"]) {
        ... your job
    }
}

关于Jenkinsfile 中的 Jenkins 全局环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235165/

相关文章:

grails - 使用 GORM 获取一对多加载对象的 ID,无需再次访问数据库

shell - Groovy 如何组合多个命令?

grails - Intellij中的AST浏览器

Jenkins:在 shell 中访问 bitbucket 有效负载

linux - 如何在 Jenkins 管道脚本中使用 source 命令

html - 使用文件 ://does not work in Chrome 链接到网络位置

node.js - Jenkins 管道 : select nodejs version (+ python version)

Android Gradle keyAlias 变量存储在单独的文件中

git - 如何通过 shell 命令行向组授予对 Bitbucket 存储库的写访问权限

Jenkins groovy 管道 - 检索构建作业的构建号