在shell中生成的并行管道中访问Groovy变量

时间:2018-04-03 01:43:56

标签: shell jenkins groovy jenkins-pipeline jenkins-groovy

我一直坚持在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

我们可以看到锁定命令具有正确的板:一次 dus011 ,另一次 dus012 ==>好

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

电路板名称的其他打印错误。

所以我尝试了'' ++'句法。听起来不错,因为这个时间板名称匹配 [回显dus011] dus011 abcd [回显dus012] dus012 abcd 但是接着是Java异常:(

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

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

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

我认为最好的方法是使用块内的withEnv来隔离设置环境的上下文。

而不是

env.board = tmpBoard

你可以做到

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