0
0
Jenkinsdevops~30 mins

Options directive (timeout, retry) in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Jenkins Options Directive with Timeout and Retry
📖 Scenario: You are setting up a Jenkins pipeline to build a simple project. Sometimes the build step can take too long or fail temporarily. To handle this, you want to add controls to stop the build if it takes too long and retry the build if it fails.
🎯 Goal: Build a Jenkins pipeline script that uses the options directive to set a timeout of 5 minutes and retry the build step 2 times if it fails.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Add an options directive with timeout set to 5 minutes
Add retry directive to retry the build step 2 times
Include a simple sh step that simulates a build command
Print a message when the build starts
💡 Why This Matters
🌍 Real World
In real projects, builds can hang or fail temporarily. Using timeout and retry helps keep pipelines reliable and efficient.
💼 Career
DevOps engineers often configure Jenkins pipelines with options like timeout and retry to improve automation robustness.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Create a Jenkins pipeline script with a pipeline block and a stages section containing one stage named Build. Inside the Build stage, add a steps block with a shell command echo "Starting build".
Jenkins
Need a hint?

Start with pipeline {} and add stages with one stage('Build'). Use sh 'echo "Starting build"' inside steps.

2
Add the options directive with timeout
Inside the pipeline block, add an options directive that sets a timeout of 5 minutes using timeout(time: 5, unit: 'MINUTES').
Jenkins
Need a hint?

Use options { timeout(time: 5, unit: 'MINUTES') } inside the pipeline block but outside stages.

3
Add retry logic to the build step
Inside the steps block of the Build stage, wrap the sh command with a retry(2) block to retry the build step 2 times if it fails.
Jenkins
Need a hint?

Wrap the sh command inside retry(2) { ... } to retry twice on failure.

4
Print a message to confirm pipeline setup
Add a post section inside the pipeline block with an always block that prints echo 'Pipeline finished' after the pipeline runs.
Jenkins
Need a hint?

Use post { always { echo 'Pipeline finished' } } inside the pipeline block to print after the run.