jenkins - 当其中一个阶段中止时如何标记构建成功?

标签 jenkins jenkins-pipeline jenkins-groovy

我有一个包含阶段的管道,其中一个阶段间歇性地花费比预期更长的时间,因此使用超时来中止它。但如果阶段中止,构建也会标记为中止。以下是管道的代码:

pipeline {
    agent any

    stages {

        stage('First') {
            options {
                timeout(time: 10, unit: 'SECONDS')
            }
            steps {
                script {

                    catchError(buildResult: 'SUCCESS') {
                        echo "Executing stage I"
                        sleep 12
                    }

                }
            }
        }

        stage('Second') {
            steps {
                script {
                    echo "Executing stage II"
                }
            }
        }
    }
}

即使阶段被标记为Aborted,我仍想将构建标记为Success。你能帮助我如何实现这一目标吗?

最佳答案

我建议对迈克尔的答案进行一项改进(顺便说一句,这是正确的)。您可以使用catchError来标记阶段ABORTED(或UNSTABLE)并标记构建SUCCESS,但您需要用 try-catch block 包装可能超时的代码以控制错误。考虑以下示例:

pipeline {
    agent any

    stages {

        stage('First') {
            options {
                timeout(time: 3, unit: 'SECONDS')
            }
            steps {
                script {
                    catchError(buildResult: 'SUCCESS', stageResult: 'ABORTED') {
                        try {
                            echo "Executing stage I"
                            sleep 4
                        } catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
                            error "Stage interrupted with ${e.toString()}"
                        }
                    }

                }
            }
        }

        stage('Second') {
            steps {
                script {
                    echo "Executing stage II"
                }
            }
        }
    }
}

当您运行此管道时,超时的阶段将标记为ABORTED,但管道会继续,并且如果剩余阶段没有失败,则将其标记为SUCCESS.

enter image description here

这是 UNSTABLE 阶段状态的样子。

enter image description here

Michael 的解决方案也有效,但它产生的结果略有不同 - 超时的阶段被标记为 SUCCESS,这可能不太直观。您需要点击舞台来检查是否超时。

pipeline {
    agent any

    stages {

        stage('First') {
            options {
                timeout(time: 3, unit: 'SECONDS')
            }
            steps {
                script {
                    try {
                        echo "Executing stage I"
                        sleep 4
                    } catch(Exception e) {
                        currentBuild.result = "SUCCESS"
                    }
                }
            }
        }

        stage('Second') {
            steps {
                script {
                    echo "Executing stage II"
                }
            }
        }
    }
}

enter image description here

关于jenkins - 当其中一个阶段中止时如何标记构建成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61223849/

相关文章:

jenkins - 如何使用 Jenkins Pipeline Groovy 脚本检索当前工作区?

gitlab - 当 gitlab webhook 调用时,jenkins 管道中的 git 分支名称是什么

Jenkins 抛出 java.lang.StackOverflowError -- 不仅仅是在 unstash

jenkins - Jenkins 管道脚本中的节流并行步骤

jenkins - Jenkins 管道中触发器指令的条件

Docker-compose 退出代码应该为非零时似乎为零

Jenkins 在找到多个文件时构建

security - 使用 Jenkins 构建在 Docker 容器内运行的 Docker 镜像

Jenkins 文件 : "RejectedAccessException: No such field found" in catch block

regex - Jenkins - 排除的提交消息正则表达式不起作用