如何在詹金斯(Jenkins)中的嵌套阶段{}块中执行帖子{}部分?

发布于 2025-01-28 09:21:07 字数 883 浏览 3 评论 0原文

如何获得以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2025-02-04 09:21:07

post {}仅在声明管道中可用,但不在声明管道的脚本部分中。您可以使用尝试{} catch {}

还有另一个错误:您正在闭合内使用循环变量元素,这无法正常工作。闭合捕获元素变量,但是当闭合运行时,其值将始终是循环的最后值。为了解决此问题,我已将循环变量分配给新的本地变量curelement,这将是每次迭代的新实例,因此它会按预期捕获。

stage("Dynamic stages"){
  steps{
    stageMap = [:]
    script {
      for (element in myList) {
        def curElement = element

        stageMap[element] = {
          stage("Stage 1 of ${curElement}"){
            echo "Stage 1"
          }
          stage("Stage 2 ${curElement}"){
            echo "Stage 2"
            try {
                // some steps that may fail
            }
            catch( Exception e ) {
                echo "Stage 2 failed! Error: $e"
                throw  // Rethrow exception, to let the build fail.
            }
          }
        }
      }
    }
    parallel stageMap
  }
}

post{} is only available in declarative pipeline, but not within a scripted section of a declarative pipeline. You can use try{} catch{} instead.

There is another error: You are using loop variable element within a closure, which doesn't work as expected. The closure captures the element 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 variable curElement, which will be a new instance for every iteration, so it gets captured as expected.

stage("Dynamic stages"){
  steps{
    stageMap = [:]
    script {
      for (element in myList) {
        def curElement = element

        stageMap[element] = {
          stage("Stage 1 of ${curElement}"){
            echo "Stage 1"
          }
          stage("Stage 2 ${curElement}"){
            echo "Stage 2"
            try {
                // some steps that may fail
            }
            catch( Exception e ) {
                echo "Stage 2 failed! Error: $e"
                throw  // Rethrow exception, to let the build fail.
            }
          }
        }
      }
    }
    parallel stageMap
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文