如何在詹金斯(Jenkins)中的嵌套阶段{}块中执行帖子{}部分?
如何获得以下post
部分运行? 这个想法是根据myList
的内容生成并行运行阶段。
stage("Dynamic stages"){
steps{
stageMap = [:]
script {
for (element in myList) {
stageMap[element] = {
stage("Stage 1 of ${element}"){
echo "Stage 1"
}
stage("Stage 2 ${element}"){
echo "Stage 2"
post{
failure{
echo "Stage 2 failed!"
}
}
}
}
}
}
parallel stageMap
}
}
该构建失败,以下错误
Also: java.lang.NoSuchMethodError: No such DSL method 'post' found among steps...
jenkins doc 可以
肯定的是,有很多允许的步骤列表,并且帖子不在其中。 我是否缺少某些内容,或者Doc不清楚此案?
How do I get the following post
section to run?
The idea is to generate parallel running stages as per the contents of myList
.
stage("Dynamic stages"){
steps{
stageMap = [:]
script {
for (element in myList) {
stageMap[element] = {
stage("Stage 1 of ${element}"){
echo "Stage 1"
}
stage("Stage 2 ${element}"){
echo "Stage 2"
post{
failure{
echo "Stage 2 failed!"
}
}
}
}
}
}
parallel stageMap
}
}
The build fails with the following error
Also: java.lang.NoSuchMethodError: No such DSL method 'post' found among steps...
The Jenkins doc however says it is allowed
Sure enough there is a big list of allowed steps and post is not among those.
Am I missing something or is the doc not clear about this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
post {}
仅在声明管道中可用,但不在声明管道的脚本部分中。您可以使用尝试{} catch {}
。还有另一个错误:您正在闭合内使用循环变量
元素
,这无法正常工作。闭合捕获元素
变量,但是当闭合运行时,其值将始终是循环的最后值。为了解决此问题,我已将循环变量分配给新的本地变量curelement
,这将是每次迭代的新实例,因此它会按预期捕获。post{}
is only available in declarative pipeline, but not within a scripted section of a declarative pipeline. You can usetry{} catch{}
instead.There is another error: You are using loop variable
element
within a closure, which doesn't work as expected. The closure captures theelement
variable, but when the closure runs, its value will always be the last value of the loop. To fix this, I have assigned the loop variable to new local variablecurElement
, which will be a new instance for every iteration, so it gets captured as expected.