我一直坚持在Jenkins管道的Shell步骤中访问Groovy变量。

我读了Access a Groovy variable from within shell step in Jenkins pipelinePass groovy variable to shell script,但是无法按预期工作。

这是我的管道:

// While you can't use Groovy's .collect or similar methods currently, you can
// still transform a list into a set of actual build steps to be executed in
// parallel.

// Our initial list of strings we want to echo in parallel
def tempList = dusboard.split(',').collect{it as String}

def stringsToEcho = ["a", "b", "c", "d"]

// The map we'll store the parallel steps in before executing them.
def stepsForParallel = tempList.collectEntries {
  ["echoing ${it}" : transformIntoStep(it)]
}

// Actually run the steps in parallel - parallel takes a map as an argument,
// hence the above.
parallel stepsForParallel

// Take the string and echo it.
def transformIntoStep(tmpBoard) {
  // We need to wrap what we return in a Groovy closure, or else it's invoked
  // when this method is called, not when we pass it to parallel.
  // To do this, you need to wrap the code below in { }, and either return
// that explicitly, or use { -> } syntax.
  return {
    node('tb-tftp-server') {
      echo tmpBoard
      stage('install') {
        env.board = tmpBoard
        lock(resource: "$tmpBoard") {
          sh '''
                hwjs="hw-\${board}.js"
                echo "1 $hwjs"
                hwjs="hw-\${tmpBoard}.js"
                echo "2 $hwjs"
            '''
        }
        lock(resource: "$tmpBoard") {
          sh '''
                echo ''' + tmpBoard + ' abcd' '''
         '''
        }
      }
    }
  }
}

如果我使用 dus011,dus012 作为输入参数(即变量dusboard = dus011,dus012)执行它,则输出
[echoing dus011] Trying to acquire lock on [dus011]
[echoing dus011] Lock acquired on [dus011]
[Pipeline] [echoing dus011] {
[Pipeline] [echoing dus012] lock
[echoing dus012] Trying to acquire lock on [dus012]
[echoing dus012] Lock acquired on [dus012]
[Pipeline] [echoing dus012] {
[Pipeline] [echoing dus011] sh
[echoing dus011] [lionel_test3] Running shell script
[echoing dus011] + hwjs=hw-dus012.js
[echoing dus011] + echo '1 hw-dus012.js'
[echoing dus011] 1 hw-dus012.js
[echoing dus011] + hwjs=hw-.js
[echoing dus011] + echo '2 hw-.js'
[echoing dus011] 2 hw-.js
[Pipeline] [echoing dus012] sh
[echoing dus012] [lionel_test3@2] Running shell script
[Pipeline] [echoing dus011] }
[echoing dus011] Lock released on resource [dus011]
[Pipeline] [echoing dus011] // lock
[echoing dus012] + hwjs=hw-dus012.js
[echoing dus012] + echo '1 hw-dus012.js'
[echoing dus012] 1 hw-dus012.js
[echoing dus012] + hwjs=hw-.js
[echoing dus012] + echo '2 hw-.js'
[echoing dus012] 2 hw-.js
[Pipeline] [echoing dus011] lock
[echoing dus011] Trying to acquire lock on [dus011]
[echoing dus011] Lock acquired on [dus011]
[Pipeline] [echoing dus011] {
[Pipeline] [echoing dus011] sh
[echoing dus011] [lionel_test3] Running shell script
[Pipeline] [echoing dus012] }
[echoing dus012] Lock released on resource [dus012]
[Pipeline] [echoing dus012] // lock
[echoing dus011] + echo dus011 abcd
[echoing dus011] dus011 abcd
[Pipeline] [echoing dus012] lock
[echoing dus012] Trying to acquire lock on [dus012]
[echoing dus012] Lock acquired on [dus012]
[Pipeline] [echoing dus012] {
[Pipeline] [echoing dus012] sh
[echoing dus012] [lionel_test3@2] Running shell script
[Pipeline] [echoing dus011] }
[echoing dus011] Lock released on resource [dus011]
[Pipeline] [echoing dus011] // lock
[Pipeline] [echoing dus011] }
[echoing dus012] + echo dus012 abcd
[echoing dus012] dus012 abcd
[Pipeline] [echoing dus011] // stage
[Pipeline] [echoing dus011] }
[Pipeline] [echoing dus011] // node
[Pipeline] [echoing dus011] }
[echoing dus011] Failed in branch echoing dus011
[Pipeline] [echoing dus012] }
[echoing dus012] Lock released on resource [dus012]
[Pipeline] [echoing dus012] // lock
[Pipeline] [echoing dus012] }
[Pipeline] [echoing dus012] // stage
[Pipeline] [echoing dus012] }
[Pipeline] [echoing dus012] // node
[Pipeline] [echoing dus012] }
[echoing dus012] Failed in branch echoing dus012
[Pipeline] // parallel
[Pipeline] End of Pipeline
Also:   java.lang.NullPointerException: Cannot get property '
             ' on null object
java.lang.NullPointerException: Cannot get property '
             ' on null object

我们可以看到lock命令的面板正确:一次 dus011 ,另一次 dus012 ==>好

但这是错误的 [echoing dus011] + hwjs = hw-dus012.js ,应该是 hw-dus011.js ,而不是 hw-dus012.js 。我认为这是因为它是一个环境变量,第二个阶段//覆盖了第一个==>不好

董事会名称的其他印刷品有误。

所以我尝试使用'''++'语法。听起来不错,因为这次的董事会名称与 [echoing dus011] dus011 abcd [echoing dus012] dus012 abcd 相匹配,但是与Java异常匹配:(

我的错误在哪里?怎么解决呢?

感谢您的帮助

最佳答案

我相信这可能会发生,因为在并行块中,您正在通过执行env来修改全局env.board = tmpBoard变量。您基于写/读访问在这些块之间存在竞争条件。

我认为执行此操作的最佳方法是改为使用块内的withEnv隔离设置环境的上下文。

代替

env.board = tmpBoard

你可以做

withEnv(["board=$tmpBoard"]) {
    // block of code needing board as environment variable
}

09-11 18:39