0
0
Jenkinsdevops~20 mins

Options directive (timeout, retry) in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Options Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What happens when a Jenkins pipeline stage exceeds the timeout set by the options directive?

Consider this Jenkins pipeline snippet:

options {
  timeout(time: 1, unit: 'MINUTES')
}
stage('Example') {
  steps {
    sh 'sleep 90'
  }
}

What is the expected outcome when this pipeline runs?

Jenkins
options {
  timeout(time: 1, unit: 'MINUTES')
}
stage('Example') {
  steps {
    sh 'sleep 90'
  }
}
AThe stage will be aborted after 1 minute with a timeout error.
BThe stage will complete successfully after 90 seconds.
CThe pipeline will retry the stage automatically after timeout.
DThe pipeline will ignore the timeout and continue running indefinitely.
Attempts:
2 left
💡 Hint

Think about what the timeout option does in Jenkins pipelines.

🧠 Conceptual
intermediate
2:00remaining
How does the retry option behave in a Jenkins pipeline?

Given this Jenkins pipeline snippet:

options {
  retry(3)
}
stage('Build') {
  steps {
    sh 'exit 1'
  }
}

What will Jenkins do when the shell command fails?

Jenkins
options {
  retry(3)
}
stage('Build') {
  steps {
    sh 'exit 1'
  }
}
AJenkins will skip the stage if it fails.
BJenkins will run the stage only once and fail immediately.
CJenkins will retry the stage indefinitely until it succeeds.
DJenkins will retry the stage up to 3 times before failing.
Attempts:
2 left
💡 Hint

Consider what the retry option number means.

🔀 Workflow
advanced
2:00remaining
Identify the correct Jenkins pipeline options block to retry a stage 2 times with a 5-minute timeout

Which options block correctly sets a retry count of 2 and a timeout of 5 minutes for a Jenkins pipeline stage?

A
options {
  retry(2)
  timeout(time: 5, unit: 'MINUTES')
}
B
options {
  retry = 2
  timeout = 5
}
C
options {
  timeout(5)
  retry(2)
}
D
options {
  retry(2, timeout: 5)
}
Attempts:
2 left
💡 Hint

Remember the syntax for options directives in Jenkins pipelines.

Troubleshoot
advanced
2:00remaining
Why does the Jenkins pipeline not retry despite using retry(3) option?

Given this Jenkins pipeline snippet:

options {
  retry(3)
}
stage('Test') {
  steps {
    script {
      try {
        sh 'exit 1'
      } catch (e) {
        echo 'Caught error'
      }
    }
  }
}

Why does Jenkins not retry the stage even though retry(3) is set?

Jenkins
options {
  retry(3)
}
stage('Test') {
  steps {
    script {
      try {
        sh 'exit 1'
      } catch (e) {
        echo 'Caught error'
      }
    }
  }
}
ABecause retry only works with timeout, not shell failures.
BBecause the error is caught inside the script block, Jenkins does not see the failure to trigger retry.
CBecause retry must be set inside the stage, not in options.
DBecause the shell command 'exit 1' is invalid syntax.
Attempts:
2 left
💡 Hint

Think about how Jenkins detects failures to retry.

Best Practice
expert
3:00remaining
What is the best way to combine timeout and retry options to handle flaky tests in Jenkins pipelines?

You want to run a test stage that might fail sometimes and could hang. You want to retry it up to 2 times and also stop it if it runs longer than 3 minutes each try.

Which Jenkins pipeline options block achieves this correctly?

A
options {
  retry(2)
  timeout(3)
}
B
options {
  timeout(time: 3, unit: 'MINUTES') {
    retry(2)
  }
}
C
options {
  retry(2)
  timeout(time: 3, unit: 'MINUTES')
}
D
options {
  retry(2, timeout: 3)
}
Attempts:
2 left
💡 Hint

Check the correct syntax for combining options in Jenkins pipelines.