Jenkins: 使用catchError()中止构建。

huangapple go评论51阅读模式
英文:

Jenkins: Aborting a build with a catchError()

问题

在我的Jenkins脚本管道中,我执行了一个带有一些任务的阶段,然后发送Slack消息。我希望在前一个阶段通过或失败时发送这些Slack消息,为此我使用了`catchError()`,它的确按照我需要的方式工作。但是当我想中止构建时出现了问题。

如果我中止构建,我希望完全退出构建,不发送Slack消息,但是我的`catchError()`捕获到了这个中止,然后继续管道并发送消息。

```groovy
stage('带有catchError的第一个阶段') {
    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: false) {
        // 在这里执行一些操作,如果有任何失败,继续构建
    }
}

stage('发送Slack消息的第二个阶段') {
    // 基于第一个阶段产生的数据,以及任务是成功还是失败,发送一些Slack消息
}
英文:

In my Jenkins scripted pipeline, I am executing 1 stage with some tasks and then sending Slack messages. I want to send these Slack messages if the previous stage passes or fails and to do this I am using a catchError() and it works exactly as I need to. My problem arises when I want to abort the build.

If I abort the build, I want to exit the build entirely and not send the Slack messages, but my catchError() is catching this abort and then continuing the pipeline and sending the messages.

stage('first stage with catchError') {
    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: false) {
        // Do some stuff here and continue the build if anything fails
    }
}

stage('second stage that send slack messages) {
    // Send some Slack messages based on the data produced in the first stage and based on if the tasks were a success or they failed
}

I thought I could use the catchInterruptions option in the catchError() to abort the entire build (here is a link for the docs for this function), but that clearly hasn't resulted in the behaviour I want. What shall I do instead?

答案1

得分: 0

I just needed to set the catchInterruptions option to true (which is the default value).

    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: true) {
        // Do some stuff here and continue the build if anything fails
    }

OR (because true is the default value)

    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
        // Do some stuff here and continue the build if anything fails
    }
英文:

I was just being stupid and misunderstanding the documentation. I just needed to set the catchInterruptions option to true (which is the default value).

    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: true) {
        // Do some stuff here and continue the build if anything fails
    }

OR (because true is the default value)

    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
        // Do some stuff here and continue the build if anything fails
    }

答案2

得分: 0

这对我有效,通过将catchInterruptions设置为false

catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: false) {
    // 在此处执行一些操作,如果发生错误,继续构建
}

现在,当我们中止构建时,可以通过一次点击中止所有阶段,而以前,我们需要逐个中止每个阶段。

英文:

This worked for me by setting catchInterruptions to false:

catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', catchInterruptions: false) {
    // Do some stuff here and continue the build if anything fails
}

Now, all stages are aborted with one click when we abort a build. Where previously, we would have to abort each stage individually.

huangapple
  • 本文由 发表于 2023年5月18日 01:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定