【Jenkins】Pipeline - 设置超时时间

设置超时

timeout(20) {
	..
}

默认时间单位为MINUTES,如果其他时间单位,则使用unit参数:SECONDS、MINUTES和HOURS

timeout(time: 20, unit: 'SECONDS') {
	..
}

示例

可以在不同级别(每个整体管道或每个阶段)使用options指定超时

任务- 超时时间

pipeline {
  options {
      timeout(time: 1, unit: 'HOURS')
  }
  stages { .. }
  // ..
}

步骤 - 超时

pipeline {
    agent any
    stages {
        stage('Run') {
            steps {
                retry(3) {
                    sh './deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './ren_test.sh'
                }
            }
        }
    }
}

超时后继续执行

在上面的示例中,在阶段超时之后,流水线中止,并显示以下消息:

Sending interrupt signal to process Cancelling nested steps due to timeout

如果超时后,想要继续执行后续步骤,需要捕获异常。

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

超时异常:
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException

pipeline {
    agent any
    options{
        timestamps()
    }
    stages {
        stage("A") {
            options {
                timeout(time: env.timeout, unit: "SECONDS")
                //MINUTES
            }

            steps {
                script {
                    Exception caughtException = null

                    catchError(buildResult: 'SUCCESS', stageResult: 'ABORTED') { 
                        try { 
                            echo "Started stage A"
                            sleep(time: 5, unit: "SECONDS")
                        } catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
                            error "Caught ${e.toString()}" 
                        } catch (Throwable e) {
                            caughtException = e
                        }
                    }

                    if (caughtException) {
                        error caughtException.message
                    }
                }
            }
        }

        stage("B") {
            steps {
                echo "Started stage B"
            }
        }
    }
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值