从阶段级别更新 Jenkins 管道中的环境全局变量 - 可能吗? [英] Updating environment global variable in Jenkins pipeline from the stage level - is it possible?

查看:21
本文介绍了从阶段级别更新 Jenkins 管道中的环境全局变量 - 可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些全局变量和一些阶段的 Jenkinsfile.我可以从一个阶段更新全局变量吗?

示例:

pipeline {代理任何环境 {PASSWD = "${sh(returnStdout: true, script: 'python -u do_some_something.py')}"ACC = "你好"}阶段('stage1'){当 { 表达式 { params.UPDATE_JOB == false } }脚步{脚本 {def foo= sh( returnStdout: true, script: 'python -u do_something.py')env.ACC = foo打印 foo打印(密码")打印 env.PASSWD}}}}

是否可以使用 foo 中的值更新 ACC 变量,以便我可以在下一阶段使用 ACC 变量?

解决方案

您不能覆盖在 environment {} 块中定义的环境变量.但是,您可能想要使用一种技巧.可以通过两种方式引用ACC环境变量:

  • 明确地由 env.ACC
  • ACC
  • 隐含

env.ACC 的值一旦设置在 environment {} 块中就不能更改,但是 ACC 的行为方式如下:当变量 ACC 没有设置,然后 env.ACC 的值被访问(如果存在的话).但是当ACC变量在任何阶段被初始化时,ACC在任何阶段都引用这个新设置的值.考虑以下示例:

pipeline {代理任何环境 {FOO = "初始 FOO 环境值"}阶段{阶段(阶段 1"){脚步 {脚本 {echo "FOO is '${FOO}'"//打印:FOO 是 '初始 FOO 环境值'env.BAR = "酒吧"}}}阶段(阶段 2"){脚步 {echo "env.BAR is '${BAR}'"//打印:env.BAR is 'bar'echo "FOO is '${FOO}'"//打印:FOO 是 '初始 FOO 环境值'echo "env.FOO is '${env.FOO}'"//打印:env.FOO is 'initial FOO env value'脚本 {FOO = "test2"env.BAR = "bar2"}}}阶段(阶段 3"){脚步 {echo "FOO is '${FOO}'"//打印:FOO 是 'test2'echo "env.FOO is '${env.FOO}'"//打印:env.FOO is 'initial FOO env value'echo "env.BAR is '${BAR}'"//打印:env.BAR is 'bar2'脚本 {FOO = "test3"}echo "FOO is '${FOO}'"//打印:FOO 是 'test3'}}}}

正如您在上面的示例中看到的,规则的唯一例外是环境变量在 environment {} 块之外初始化.例如,本例中的env.BAR是在Stage 1中初始化的,但是env.BAR的值可以在中改变Stage 2Stage 3 看到了改变的值.

更新 2019-12-18

有一种方法可以覆盖在 environment {} 块中定义的环境变量 - 您可以使用 withEnv() 块来覆盖现有的 env多变的.它不会改变定义的环境的值,但会在 withEnv() 块内覆盖它.看看下面的例子:

pipeline {代理任何阶段{阶段(测试"){环境 {FOO = "酒吧"}脚步 {脚本 {withEnv(["FOO=newbar"]) {echo "FOO = ${env.FOO}"//打印:FOO = newbar}}}}}}

<块引用>

我还鼓励您查看我的 "Jenkins 管道环境变量解释 视频.

I have a Jenkinsfile with some global variables and some stages. can I update the global variable out from a stage?

An example:

pipeline {
  agent any

  environment {                 
    PASSWD = "${sh(returnStdout: true, script: 'python -u do_some_something.py')}"
    ACC = "HI"
  }

  stage('stage1') {
      when { expression { params.UPDATE_JOB == false } }

      steps{
        script {
          def foo= sh(  returnStdout: true, script: 'python -u do_something.py ')
          env.ACC =  foo
          println foo
          print("pw")
          println env.PASSWD
       }
     }  
   }
}

Is it possible to update the ACC variable with the value from foo, so that I can use the ACC Variable in the next stage?

解决方案

You can't override the environment variable defined in the environment {} block. However, there is one trick you might want to use. You can refer to ACC environment variable in two ways:

  • explicitly by env.ACC
  • 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"
    }

    stages {
        stage("Stage 1") {
            steps {
                script {
                    echo "FOO is '${FOO}'" // prints: FOO is 'initial FOO env value'

                    env.BAR = "bar"
                }
            }
        }

        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.

UPDATE 2019-12-18

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:

pipeline {
  agent any 

  stages {
    stage("Test") {
      environment {
        FOO = "bar"
      }

      steps {
        script {
          withEnv(["FOO=newbar"]) {
            echo "FOO = ${env.FOO}" // prints: FOO = newbar
          }
        }
      }
    }
  }
}

I also encourage you to check my "Jenkins Pipeline Environment Variables explained " video.

这篇关于从阶段级别更新 Jenkins 管道中的环境全局变量 - 可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆