在Jenkins管道中给定阶段超时后继续执行后续阶段

问题描述 投票:0回答:1

我正在构建一个声明式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

jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli
1个回答
0
投票

这可能有效:

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是由于超时还是其他原因发生的。

© www.soinside.com 2019 - 2024. All rights reserved.