问题
I have env vars defined in my environment directive at the top of the pipeline:
environment {
var1 = 'sdfsdfdsf'
var2 = 'sssssss'
}
But there are some that I need to dynamically set or override in the stages. But if I use an environment{} directive in a stage the vars won't be accessible to other stages. Initially I thought I could define them all with default values in the top environment directive and overwrite them in the pipeline but this is the behavior I observed:
- Define var in environment block
- Try to overwrite in script{} block like:
script {env.var1 = 'new value'}
- The env is not overwritten
How can I change the envs?
回答1:
You can do it as follow:
- Define var in stage environment { env.var1 = 'value' }
you can access same var in other stages and change value in environment { env.var1 = 'value2' }
pipeline { agent any environment { var1 = 'value' } stages { stage('Initialize') { steps { script { echo ("value : " + env.var1) } } } stage('build') { environment { var1 = 'value2' } steps { script { echo ("value : " + env.var1) } } } } }
来源:https://stackoverflow.com/questions/47684675/define-global-environment-variables-from-inside-a-stage