如何向 Jenkins 管道添加超时步骤

How to add a timeout step to Jenkins Pipeline

当您使用自由风格的项目时,您可以设置在 20 分钟后如果未结束构建将中止。 Jenkins 多分支管道项目如何做到这一点?

您可以使用 timeout 步骤:

timeout(20) {
  node {
    sh 'foo'
  }
}

如果您需要 TimeUnit 不同于 MINUTES,您可以提供 unit 参数:

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

编辑 2018 年 8 月: 如今更常见的 declarative pipelines (easily recognized by the top-level pipeline construct), timeouts can also be specified using options 在不同级别(每个整体管道或每个阶段):

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

不过,如果您想对声明性管道中的单个步骤应用超时,可以如上所述使用它。

一个Declarative Pipeline it is adviced to use the timeout step in the options-section.

Executes the code inside the block with a determined time out limit. If the time limit is reached, an exception (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) is thrown, which leads in aborting the build (unless it is caught and processed somehow). Unit is optional but defaults to minutes.

timeout-step有3个parameters你可以配置:

  • 时间(必需,整数)

    • 超时时间,如果没有指定单位,以分钟为单位
  • activity(可选,布尔值)

    • 此块的日志中没有 activity 而不是绝对持续时间后超时。
  • 单位(可选,值:纳秒、微秒、毫秒、秒、分钟、小时、天)

    • 时间的单位,默认为分钟

示例:

timeout(time: 10) // would lead to a timeout of 10 minutes (MINUTES is default value)
timeout(time: 10, unit: 'SECONDS') // a 10 seconds timeout
timeout(time: 10, activity: false, unit: 'MILLISECONDS')

official Jenkins documentation 有一个很好的超时使用示例:

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

在声明性管道中,您可以使用:

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

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}

对于声明式 管道(整个作业超时):

pipeline {
    options {
        timeout(time: 3, unit: 'HOURS')
    }
    
    agent {
        label 'agent_name'
    }
        
    stages {
        stage('Stage_name') {
            steps {
                // ...
            }
        }
    }
    
    // ...
}

对于脚本管道(整个作业超时):

def call() {
    node('agent_name') {
        timeout(time: 3, unit: 'HOURS') {
            stage('Stage_name') {
                // ...
            }
        }
    }    
}

超时阶段内(针对特定操作):

声明式管道

pipeline {
    agent {
        label 'agent_name'
    }

    stages {
        stage('Stage_name') {
            steps {
                timeout(time: 3, unit: 'HOURS') {
                    sh ' ... '
                    echo '...'
                    // ...
                }

                // ...
            }
        }
    }
}

脚本化管道

def call() {
    node('agent_name') {
        stage('Stage_name') {
            timeout(time: 3, unit: 'HOURS') {
                sh '...'
                echo '...'
                // ...
            }

            // ...
        }
    }    
}