如何在动态生成的 jenkins 阶段使用 post 构建操作

How to use post build action in a dynamically generated jenkins stage

我正在使用 Jenkins,我想向动态生成的阶段添加一个 post-build 操作。

我通过循环遍历我拥有的配置文件来动态生成阶段:

stage('run list'){
  steps {
    script {
      ...
      def stages = stagelist.collectEntries {
        ["${it}": generateStage(it)]
      }
      ...
    }
  }
}

其中 generateStage 定义为:

def generateStage(job){
  return {
    stage("${job}"){
      catchError(){
         ....
      }
      post {
        ...
      }
    }
  }
}

这对我目前的阶段来说效果很好。但是,当我尝试向生成阶段函数添加 post-action 时,出现 NoSuchMethod 错误。

如果我能得到一些帮助,将不胜感激!

当您使用 static parallel steps 时,可以使用 post 指令作为管道语法的一部分,但是当您动态创建阶段时(在 generateStage 中)您正在通过使用 不支持 post 指令 .
的脚本化管道语法来实现 您可以做的是使用 post 部分的脚本管道版本,使用 try catch finally 语法来实现与使用 post 声明性管道中的部分。
尝试类似的东西:

def generateStage(job){
    return {
        stage("${job}"){
            try {
                // Your Execution steps
                ...
                // This is equivalent to the 'success' section in the post directive
            }
            catch(Exception ex) {
                // This is equivalent to the 'Failure' section in the post directive
            }
            finally {
                // This is equivalent to the 'always' section in the post directive
            }
        }
    }
}

它不是百分百相等的,因为 try catch 只会检测异常​​而不会检测直接构建结果更改 - 但它应该适用于大多数步骤