Jenkins 发布者 postBuildScripts 不起作用

Jenkins publishers postBuildScripts doesn't work

我有一个 groovy 脚本来在 Jenkins 中设置计划作业。
我想在构建失败时执行一些 shell 脚本。
如果我在通过 groovy 脚本更新作业后创建作业后手动拥有脚本,则它们 运行.
但是 groovy 脚本没有添加它:

job('TestingAnalysis') {

  triggers {
    cron('H 8 28 * *')
  }

  steps {
    shell('some jiberish to create error')
  }
  publishers {
    postBuildScripts {
        steps {
          shell('echo "fff"')
          shell('echo "FFDFDF"')
          }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(true)
    }
    retryBuild {
        rerunIfUnstable()
        retryLimit(3)
        fixedDelay(600)
    }
  }
}

一切正常,除了:

    postBuildScripts {
        steps {
          shell('echo "fff"')
          shell('echo "FFDFDF"')
          }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(true)
    }

这是我的结果:

我尝试了 postBuildSteps,但也出现了错误。

我试过也有错误:

    postBuildScripts {
        steps {
          sh' echo "ggg" '
          }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(true)
    }

看看JENKINS-66189 seems like there is an issue with version 3.0 of the PostBuildScript in which the old syntax (that you are using) is no longer supported. In order to use the new version it in a Job Dsl script you will need to use Dynamic DSL syntax

在您自己的 Jenkins 实例中使用以下 link 以查看正确用法:
YOUR_JENKINS_URL/plugin/job-dsl/api-viewer/index.html#path/freeStyleJob-publishers-postBuildScript.
它将帮助您构建正确的命令。在你的情况下它将是:

job('TestingAnalysis') {
    triggers {
        cron('H 8 28 * *')
    }
    steps {
        shell('some jiberish to create error')
    }
    publishers {
        postBuildScript {
            buildSteps {
                postBuildStep {
                    stopOnFailure(false) // Mandatory setting
                    results(['FAILURE']) // Replaces onlyIfBuildFails(true)
                    buildSteps {
                        shell {
                            command('echo "fff"')
                        }
                        shell {
                            command('echo "FFDFDF"')
                        }
                    }
                }
            }
            markBuildUnstable(false)  // Mandatory setting
        }
    }
}

请注意,您现在只需将相关构建结果列表传递给 results 函数,而不是使用 onlyIfBuildSucceedsonlyIfBuildFails 等函数。 (成功,不稳定,失败,NOT_BUILT,中止)