Is it possible to set the value of an environmental variable on an agent/node from a pipeline?

拥有回忆 提交于 2021-02-11 15:55:56

问题


I'm trying to pass the value for an environmental variable that set on the node under "Node Properties"

Example: I have GIT_BRANCH environmental variable set on the node the value is passed to that variable is ${BRANCH_Z}. ${BRANCH_Z} value comes from a pipeline script like so

pipeline {  
 agent none;
 parameters {
   string(defaultValue: "master", description: 'Branch to build', name: 'BRANCH_Z')
}
environment {
   GIT_BRANCH = "${params.BRANCH_Z}"
}

But when I run this I get back params.BRANCH_Z as the value not master.


回答1:


I do not believe that you can set dynamic values for environment variables within the environment block during pipeline declaration. You can, however, do this for default values in the parameters block during pipeline declaration.

If you want to set a dynamic value for an environment variable, you will likely need to set it within the env map inside the pipeline during execution, such as:

stages {
  stage('Foo') {
    steps {
      env.GIT_BRANCH = params.BRANCH_Z
    }
  }
}

I also believe that one or more plugins may automatically assign that specific environment with a value, so overwriting it is safer anyway.



来源:https://stackoverflow.com/questions/62880159/is-it-possible-to-set-the-value-of-an-environmental-variable-on-an-agent-node-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!