2020年2月6日 星期四

[ 常見問題 ] Jenkins Global environment variables in Jenkinsfile

Source From Here
Question
How do I invoke Global environment variables in Jenkinsfile? For example, if I have a variable:
name:credentialsId
value:xxxx-xxxx-xxxxx-xxxxxxxxx

How do I use it in the groovy script?

How-To
In a Jenkinsfile, you have the "Working with the Environment" which mentions:
The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env,

The syntax is ${env.xxx} as in:
  1. node {  
  2.     echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"  
  3. }  
See also "Managing the Environment".
How can I pass the Global variables to the Jenkinsfile?
When I say Global variables - I mean in

Check path as below:
Jenkins -> Manage Jenkins -> Configure System -> Global properties -> Environment variables

See "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)
  1. node {  
  2.     /* .. snip .. */  
  3.     withEnv(["NAME=value"]) {  
  4.         ... your job  
  5.     }  
  6. }  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...