如果输入超时,Mark Jenkins能否成功构建? (声明性管道) [英] Mark Jenkins build as success, in case of timeout on input? (declarative pipeline)

查看:117
本文介绍了如果输入超时,Mark Jenkins能否成功构建? (声明性管道)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个声明性的詹金斯管道,如下所示:

I'm creating a declarative Jenkins pipeline, that looks like this:

pipeline {
    agent {
        label 'mylabel'
    }
    stages {
        stage('Install dependencies') {
            milestone()
            steps {
                sh "yarn install"
            }
        }
        stage('Lint files') {
            steps {
                sh "eslint src"
            }
        }
        stage('Create bundle') {
            steps {
                sh "yarn run build:server"
                sh "yarn run build:client"
            }
        }
        stage('Publish') {
            steps {
                timeout(time: 15, unit: 'SECONDS') {
                    input(message: 'Deploy this build to QA?')
                }
                // deployment steps
            }
        }

    }
}

它很好用,但是,如果超时步骤失败(因为我们不想部署此构建,或者没有人按下该按钮,依此类推),则该构建会被标记为已终止"状态.不幸的是,这意味着例如Github将我们的请求请求标记为检查失败".

It works great, however, if the timeout step fails (because we don't want to deploy this build, or nobody is there to push the button, and so on), the build is marked with status "aborted". Unfortunately this means that for example Github marks our pull requests as "checks failing".

有没有一种方法可以用timeout()步骤之前的状态声明该版本?例如.如果在超时步骤之前构建成功,则即使发生超时也应将其标记为成功.

Is there a way to declare the build with the status that it had before the timeout() step? Eg. if the build was a success up until the timeout step, it should be marked as success, even if the timeout happens.

推荐答案

由于您要做的就是让构建中止而不将其标记为失败,因此您只需在代码中添加一个简单的try/catch.

Since all you want is to let the build abort without marking it as failed, you can just add a simple try/catch to your code.

        stage('Publish') {
            steps {
                script {
                    def proceed = true
                    try {
                        timeout(time: 15, unit: 'SECONDS') {
                            input(message: 'Deploy this build to QA?')
                        }
                    } catch (err) {
                        proceed = false
                    }
                    if(proceed) {
                        // deployment steps
                    }
                }
            }
        }

如果用户中止构建或超时,则错误将被抑制,构建仍然成功,并且部署步骤将无法执行.

If a user aborts the build or it times out, the error is suppressed, the build is still a success, and the deployment steps won't execute.

这篇关于如果输入超时,Mark Jenkins能否成功构建? (声明性管道)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆