jenkins学习之Jenkins流水线中怎么使用全局变量
问题背景
例如我们在 stage1中 利用 script 代码块 设置赋值了一个 变量,然后想在 stage2中使用。应该怎么操作呢?
https://stackoverflow.com/questions/53541489/updating-environment-global-variable-in-jenkins-pipeline-from-the-stage-level/53541813
https://www.youtube.com/watch?v=KwQDxwZRZiE
你不能重写在environment{}块中定义的环境变量。不过,有一个技巧你可能想用。可以参考ACC环境变量
有两种方式:
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:
pipeline {
agent any
environment {
FOO = "initial FOO env value"
}