gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:55:38 34 4
gpt4 key购买 nike

我坚持在 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 ==> good

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

板名其他打印错误。

所以我尝试使用 '''++' 语法。听起来不错,因为这次板名称匹配 [echoing dus011] dus011 abcd[echoing dus012] dus012 abcd 但随后出现 Java 异常 :(

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

谢谢你的帮助

最佳答案

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

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

代替

env.board = tmpBoard

你可以做

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

关于shell - 在 shell 中访问生成的并行管道内的 Groovy 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49621025/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com