设置超时
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
如果超时后,想要继续执行后续步骤,需要捕获异常。文章来源:https://www.toymoban.com/news/detail-583238.html
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文章来源地址https://www.toymoban.com/news/detail-583238.html
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"
}
}
}
}
到了这里,关于【Jenkins】Pipeline - 设置超时时间的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!