在 shell 中访问生成的并行管道内的 Groovy 变量
Access a Groovy variable inside a generated parallel pipeline in a shell
我无法访问 Jenkins 管道中 shell 步骤中的 groovy 变量。
我阅读了 and 但无法按预期工作。
这是我的管道:
// 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 ==> good
但是这个是错误的[呼应dus011] + hwjs=hw-dus012.js应该是hw-dus011.js,而不是 hw-dus012.js。我认为这是因为它是一个环境变量,第二个 // 阶段覆盖第一个 ==> 坏
板名其他打印错误
所以我尝试使用 '''++' 语法。听起来不错,因为这次板名匹配 [echoing dus011] dus011 abcd 和 [echoing dus012] dus012 abcd 但随后出现 Java 异常: (
我的错误在哪里?如何解决?
感谢您的帮助
我相信这可能会发生,因为在并行块中,您正在通过 env.board = tmpBoard
修改全局 env
变量。基于 write/read 访问权限,这些块之间存在竞争条件。
我认为最好的方法是在块中使用 withEnv
来隔离设置环境的上下文。
而不是
env.board = tmpBoard
你可以
withEnv(["board=$tmpBoard"]) {
// block of code needing board as environment variable
}
我无法访问 Jenkins 管道中 shell 步骤中的 groovy 变量。
我阅读了
这是我的管道:
// 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 ==> good
但是这个是错误的[呼应dus011] + hwjs=hw-dus012.js应该是hw-dus011.js,而不是 hw-dus012.js。我认为这是因为它是一个环境变量,第二个 // 阶段覆盖第一个 ==> 坏
板名其他打印错误
所以我尝试使用 '''++' 语法。听起来不错,因为这次板名匹配 [echoing dus011] dus011 abcd 和 [echoing dus012] dus012 abcd 但随后出现 Java 异常: (
我的错误在哪里?如何解决?
感谢您的帮助
我相信这可能会发生,因为在并行块中,您正在通过 env.board = tmpBoard
修改全局 env
变量。基于 write/read 访问权限,这些块之间存在竞争条件。
我认为最好的方法是在块中使用 withEnv
来隔离设置环境的上下文。
而不是
env.board = tmpBoard
你可以
withEnv(["board=$tmpBoard"]) {
// block of code needing board as environment variable
}