头图

When using jenkins for automatic compilation and construction, some tasks may have been blocked due to network or other reasons, causing the project to be unable to perform compilation and construction again. We want to terminate certain tasks when their execution time exceeds a certain value.

Method one (general)

The "build timeout plugin" plugin of jenkins can help us accomplish this task. I am using jenkins-2.7.1, the plug-in is already installed by default, if it is not installed by default, you can search and install it in the plug-in management.

请添加图片描述

Method 2 (Pipeline)

Jenkins Pipeline provides a lot of steps (steps), which can be combined and nested with each other to easily solve problems such as repeating the steps until successful (retry) and exiting (timeout) if a step takes too long to execute.

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

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

The "Deploy" stage (stage) repeatedly executes the flakey-deploy.sh script 3 times, and then waits for the health-check.sh script to execute for a maximum of 3 minutes. If the health-check.sh script is not completed within 3 minutes, Pipeline will mark it as failed in the "Deploy" phase.

Embedded type

Embedded types of steps, such as timeout and retry can contain other steps, including timeout and retry.

We can also combine these steps. For example, if we want to retry the deployment task 5 times, but the total time spent cannot exceed 3 minutes.

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                timeout(time: 3, unit: 'MINUTES') {
                    retry(5) {
                        sh './flakey-deploy.sh'
                    }
                }
            }
        }
    }
}

Series of articles

jenkins automated configuration
Jenkins's tty problem
jenkins reverse proxy configuration
jenkins's java11 issue
Jenkins private key format error problem
jenkins webhooks configuration
jenkins set task timeout⏱ (multiple methods: pictures and texts 🖼)

refer to


墨抒颖
74 声望11 粉丝

msy.plus