在詹金斯管道中的给定阶段超时之后,继续执行后续阶段

时间:2020-03-16 09:54:26

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli

我正在构建一个声明性的Jenkins管道,在该管道中,我希望某个阶段超时,以使后续阶段继续正常运行。我确定在此用例中,各个阶段之间没有相互依赖关系。

pipeline {
    agent any
    stages {
            stage('Build-1') {
                options {
                    timeout(time: 1, unit: 'HOURS') 
                }
                steps {
                    echo 'Hello World 1'
                }
            }
            stage('Build-2') {
                steps {
                    echo 'Hello World 2'
                }
        }
    }
}

在上面的示例中,阶段Build-1超时后,管道中止并显示以下消息: Sending interrupt signal to process Cancelling nested steps due to timeout

此处,阶段Build-2未执行。是否有一种方法可以使阶段Build-1处于超时状态,但管道仍可以正常运行阶段Build-2

我指的是以下文档:https://jenkins.io/doc/book/pipeline/syntax/#options

1 个答案:

答案 0 :(得分:1)

这可能有效:

pipeline {
    agent any
    stages {
            stage('Build-1') {
                options {
                    timeout(time: 1, unit: 'HOURS') 
                }
                steps {
                    script {
                        try {
                             echo 'Hello World 1'
                        } catch (error) {
                             println "Error happened, continuing"
                        } 
                    }
                }
            }

您可以进一步检查error是否由于超时或其他原因而发生。