问题
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