Jenkins

Set and Use Jenkins Timeout

When working in Jenkins, you may encounter some jobs that take a long time to run, leading to intensive resource usage and performance impact on the server.

However, Jenkins allows us to set a build timeout to specify how long a build should be allowed to run before it is automatically terminated. In this case, a build timeout is useful when you have long-running builds that may get stuck or freeze for some reason.

In this tutorial, we will expore how to configure a build timeout in Jenkins.

Example Pipeline

To best illustrate when and how to use the timeout option in Jenkins, let us take a sample pipeline as shown below:

pipeline {
    agent any
    stages {
        stage('Input') {
            steps {
                script {
                    def userInput = input message: 'Please enter a value:', parameters: [string(defaultValue: 'default value', description: 'Enter a value', name: 'inputValue')]
                    echo "User input: ${userInput}"
                }
            }
        }
    }
}

In this case, the pipeline has a single stage called “Input“, which contains a single step that prompts the user for input.

The “input” step displays a message to the user and waits for the user to enter a value. The value that the user enters is stored in the “userInput” variable. Finally, we print the input message to the console using the echo command.

Once configured, build the pipeline and see what happens.

If we do not provide input in the pipeline above, the pipeline will wait indefinitely for you to enter a value. This is because the “input” step is blocking, which means that the pipeline will not continue to the next step until the user provides input.

To overcome this, we can setup a timeout value that will automatically terminate the build after a given duration.

The example script shows how we can use the Jenkins timeout feature to end the build after 10 seconds.

pipeline {
    agent any
    stages {
        stage('Input') {
            steps {
                timeout(time: 10, unit: 'SECONDS') {
                    script {
                        def userInput = input message: 'Please enter a value:', parameters: [string(defaultValue: 'default value', description: 'Enter a value', name: 'inputValue')]
                        echo "User input: ${userInput}"
                    }
                }
            }
        }
    }

}

The “timeout” step will allow the “input” step to run for 10 seconds before timing out. If the user does not provide input within the time specified, the terminate the build or move to the next step.

Jenkins will force the build to terminate even if there are nested actions after the timeout has elapsed.

Conclusion

In this article, we learned about the Jenkins timeout feature and how to use it to specify the maximum amount of time for a build to run. We saw how to set a build timeout using the “timeout” command in a build script.

Overall, the Jenkins timeout feature is a valuable tool for preventing long-running builds from getting stuck or freezing and ensuring that builds do not consume excessive resources. This can help ensure that your Jenkins instance runs smoothly and efficiently.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list