【问题标题】:How do I define a global variable in a scripted jenkins pipeline如何在脚本化的詹金斯管道中定义全局变量
【发布时间】:2021-06-16 01:58:24
【问题描述】:

我想在 Jenkins 脚本化管道中定义一个全局变量,可以在管道中的任何位置访问该变量。即任何阶段和任何方法。 如果我在管道顶部定义 var,它在 node declration 和 stage 声明中有效,但在被调用方法中无效。 我不想使用 env.XXX 和 withEnv([]) 因为我可能不得不从不同的地方调用这些方法,这意味着有时使用 env 而不是其他的。

这是我用于脚本化管道的简单 JenkinsFile:

def jenkinsNode = 'linux'
def DEBUG = 1

node(jenkinsNode){
  echo ">> node($jenkinsNode)"
  echo "DEBUG = $DEBUG"

  if (DEBUG) {
    echo "DEBUG is On"}
  else {
    echo "DEBUG is Off"
  }

  stage('test-this') {
    if (DEBUG) {
      echo "DEBUG is On"}
    else {
      echo "DEBUG is Off"
    }

    testMethod()
  }
  echo "<< node($jenkinsNode)"
}

def testMethod() {
  echo ">> testMethod()"

  if (DEBUG) {
    echo "DEBUG is On"}
  else {
    echo "DEBUG is Off"
  }

  echo "<< testMethod()"
}

当我运行它时,我得到:

Running on rh6-a01 in /jenkins_home/jenkins-rh6-a01/a98289de/workspace/test/test/test-global
[Pipeline] {
[Pipeline] echo
>> node(linux)
[Pipeline] echo
DEBUG = 1
[Pipeline] echo
DEBUG is On
[Pipeline] stage
[Pipeline] { (test-this)
[Pipeline] echo
DEBUG is Off
[Pipeline] echo
>> testMethod()
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: DEBUG for class: WorkflowScript
[...snip...]

如何编写允许任何方法访问 DEBUG 变量的 Jenkinsfile?

【问题讨论】:

  • 全局变量通常在管道块之外定义。见stackoverflow.com/questions/52063864/…
  • 是的,您提到的是声明式管道。我的查询专门针对脚本化管道。
  • 抱歉你是对的。你只需要删除def。查看完整答案

标签: jenkins jenkins-pipeline pipeline cloudbees


【解决方案1】:

从顶部的声明中删除 def 可以解决此问题。

def jenkinsNode = 'linux'
DEBUG = 1

node(jenkinsNode){
  echo ">> node($jenkinsNode)"
  echo "DEBUG = $DEBUG"

  if (DEBUG) {
 .....

给出输出

>> node(linux)
[Pipeline] echo
DEBUG = 1
[Pipeline] echo
DEBUG is On
[Pipeline] stage
[Pipeline] { (test-this)
[Pipeline] echo
DEBUG is On
[Pipeline] echo
>> testMethod()
[Pipeline] echo
DEBUG is On
[Pipeline] echo
<< testMethod()
[Pipeline] }
[Pipeline] // stage
[Pipeline] echo
<< node(docker)
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

这是因为使用def 将变量绑定到当前范围(方法内容不在其中)。不使用 def 不会绑定允许它在脚本中的任何位置使用的范围。

请注意,Groovy 不会阻止您在其他地方使用带有 def 的变量,这可能会导致意外结果,例如在方法中添加def DEBUG = 0

def testMethod() {
  echo ">> testMethod()"
  def DEBUG = 0

  if (DEBUG) {
    echo "DEBUG is On"}
  else {
    echo "DEBUG is Off"
  }

仍然可以正常运行,但会在该方法中关闭 DEBUG。

【讨论】:

  • 不是一个好习惯。见:stackoverflow.com/questions/50571316/…
  • 公平,使用field 可能更安全。但它认为你需要一个非常复杂的脚本才能成为一个问题。在超过 8 年的詹金斯。由于它提供的简单性,我从来没有遇到过问题。
【解决方案2】:
  1. 您可以将变量作为参数传递
......

    testMethod(DEBUG)
  }
  echo "<< node($jenkinsNode)"
}

def testMethod(DEBUG) {
  echo ">> testMethod()"

  if (DEBUG) {
    echo "DEBUG is On"}
  else {
    echo "DEBUG is Off"
  }

  echo "<< testMethod()"
}
  1. 如果上述解决方案不是您正在寻找的,使用@Field 注释将按照此答案https://stackoverflow.com/a/37425799/10697591 中的说明工作
import groovy.transform.Field

@Field def DEBUG = 1
def jenkinsNode = 'master'


node(jenkinsNode){
  echo ">> node($jenkinsNode)"
  echo "DEBUG = $DEBUG"

  if (DEBUG) {
    echo "DEBUG is On"}
  else {
    echo "DEBUG is Off"
  }

  stage('test-this') {
    if (DEBUG) {
      echo "DEBUG is On"}
    else {
      echo "DEBUG is Off"
    }

    testMethod()
  }
  echo "<< node($jenkinsNode)"
}

def testMethod() {
  echo ">> testMethod()"

  if (DEBUG) {
    echo "DEBUG is On"}
  else {
    echo "DEBUG is Off"
  }

  echo "<< testMethod()"
}

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多