如何在 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/