explicitly by env.ACC 显示定义,明确地定义,更推荐使用这种 带 env. 的方式来定义 implicitly by ACC 隐式定义。
The value of env.ACC cannot be changed once set inside environment {} block, but ACC behaves in the following way: when the variable ACC is not set then the value of env.ACC gets accessed (if exists of course). But when ACC variable gets initialized in any stage, ACC refers to this newly set value in any stage. Consider the following example:
stage("Stage 2") { steps { echo "env.BAR is '${BAR}'" // prints: env.BAR is 'bar' echo "FOO is '${FOO}'" // prints: FOO is 'initial FOO env value' echo "env.FOO is '${env.FOO}'" // prints: env.FOO is 'initial FOO env value' script { FOO = "test2" env.BAR = "bar2" } } }
stage("Stage 3") { steps { echo "FOO is '${FOO}'" // prints: FOO is 'test2' echo "env.FOO is '${env.FOO}'" // prints: env.FOO is 'initial FOO env value' echo "env.BAR is '${BAR}'" // prints: env.BAR is 'bar2'
script { FOO = "test3" }
echo "FOO is '${FOO}'" // prints: FOO is 'test3' } } } }
And as you can see in the above example, the only exception to the rule is if the environment variable gets initialized outside the environment {} block. For instance, env.BAR in this example was initialized in Stage 1, but the value of env.BAR could be changed in Stage 2 and Stage 3 sees changed value.
There is one way to override the environment variable defined in the environment {} block - you can use withEnv() block that will allow you to override the existing env variable. It won't change the value of the environment defined, but it will override it inside the withEnv() block. Take a look at the following example: