Jenkinsfile 中的 Jenkins 全局环境变量 [英] Jenkins Global environment variables in Jenkinsfile

查看:93
本文介绍了Jenkinsfile 中的 Jenkins 全局环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I invoke Global environment variables in Jenkinsfile?
For example, if I have a variable -

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

如何在 groovy 脚本中使用它?

How do I use it in the groovy script?

我尝试了 ${credentialsId},但没有成功.它只会给出错误:

I tried ${credentialsId}, but it didn't work. It will just give error:

java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, ........

推荐答案

在 Jenkinsfile 中,你有 "使用环境"其中提到:

In a Jenkinsfile, you have the "Working with the Environment" which mentions:

可从 Jenkins Pipeline 中访问的环境变量的完整列表记录在 localhost:8080/pipeline-syntax/globals#env,

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}"
}

另请参阅 "管理环境".

See also "Managing the Environment".

如何将全局变量传递给 Jenkinsfile?
当我说全局变量时——我的意思是在

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

请参阅设置环境变量";

在 Jenkins 流水线中设置环境变量可以通过 withEnv 步骤完成,该步骤允许为给定的流水线脚本块覆盖指定的环境变量,例如:

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(管道脚本)

Jenkinsfile (Pipeline Script)

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

这篇关于Jenkinsfile 中的 Jenkins 全局环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆