Bird
Raised Fist0
Jenkinsdevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the timeout option in a Jenkins pipeline?
easy
A. To stop a build if it runs longer than a specified time
B. To retry a failed build step multiple times
C. To schedule a build at a specific time
D. To increase the build's priority in the queue

Solution

  1. Step 1: Understand the role of timeout

    The timeout option is used to limit how long a build or step can run.
  2. Step 2: Identify the effect of timeout

    If the build runs longer than the set time, Jenkins stops it to save resources.
  3. Final Answer:

    To stop a build if it runs longer than a specified time -> Option A
  4. Quick Check:

    Timeout = stop long builds [OK]
Hint: Timeout stops builds running too long [OK]
Common Mistakes:
  • Confusing timeout with retry
  • Thinking timeout schedules builds
  • Assuming timeout increases priority
2. Which of the following is the correct syntax to set a retry of 3 times in a Jenkins pipeline options block?
easy
A. options { retry(3); }
B. options { retry 3 }
C. options { retry(3) {} }
D. options { retry(3) }

Solution

  1. Step 1: Recall Jenkins options syntax

    The correct syntax uses retry(3) inside the options block without extra braces.
  2. Step 2: Identify the valid option

    options { retry(3) } matches the correct syntax exactly: options { retry(3) }.
  3. Final Answer:

    options { retry(3) } -> Option D
  4. Quick Check:

    Retry syntax = retry(3) inside options [OK]
Hint: Use retry(3) inside options block without extra braces [OK]
Common Mistakes:
  • Adding extra braces after retry(3)
  • Using retry without parentheses
  • Placing retry outside options block
3. Given this Jenkins pipeline snippet:
options {
  timeout(time: 5, unit: 'MINUTES')
  retry(2)
}

stage('Test') {
  steps {
    script {
      def count = 0
      retry(2) {
        count++
        if (count < 3) {
          error('Fail')
        }
      }
      echo "Count is ${count}"
    }
  }
}

What will be the output of the echo command?
medium
A. Count is 1
B. Count is 2
C. Count is 3
D. Build fails before echo

Solution

  1. Step 1: Understand retry behavior in script

    The inner retry(2) block retries the enclosed code up to 2 times on failure.
  2. Step 2: Trace the count increments and errors

    First run: count=1, error thrown (count<3). Retry 1: count=2, error thrown. Retry 2: count=3, no error, exits retry.
  3. Step 3: Determine echo output

    After retries, count is 3, so echo "Count is 3" runs.
  4. Final Answer:

    Count is 3 -> Option C
  5. Quick Check:

    Retry runs until count=3, then echo prints count [OK]
Hint: Count increments on each retry until no error [OK]
Common Mistakes:
  • Assuming retry runs only once
  • Thinking count resets on retry
  • Ignoring retry inside script block
4. You wrote this Jenkins pipeline options block:
options {
  timeout(10)
  retry(3)
}

But the build fails with a syntax error. What is the problem?
medium
A. timeout requires both time and unit parameters
B. retry cannot be used inside options block
C. timeout value must be a string, not a number
D. retry requires a block after the number

Solution

  1. Step 1: Check timeout syntax requirements

    The timeout option requires named parameters: time and unit.
  2. Step 2: Identify missing parameters

    Using timeout(10) alone is invalid; it must be timeout(time: 10, unit: 'MINUTES').
  3. Final Answer:

    timeout requires both time and unit parameters -> Option A
  4. Quick Check:

    Timeout needs time and unit named params [OK]
Hint: Timeout needs time and unit named parameters [OK]
Common Mistakes:
  • Passing only a number to timeout
  • Thinking retry needs a block
  • Confusing timeout value types
5. You want to create a Jenkins pipeline that stops if it runs longer than 2 minutes and retries the entire build up to 2 times if it fails. Which options block correctly implements this?
hard
A. options { timeout(time: 2, unit: 'MINUTES') { retry(2) } }
B. options { timeout(time: 2, unit: 'MINUTES') retry(2) }
C. options { timeout(2) retry(2) }
D. options { retry(2) timeout(2, 'MINUTES') }

Solution

  1. Step 1: Set timeout correctly

    Timeout must use named parameters: time: 2, unit: 'MINUTES'.
  2. Step 2: Add retry correctly

    Retry is a separate option inside the options block: retry(2).
  3. Step 3: Validate options block structure

    options { timeout(time: 2, unit: 'MINUTES') retry(2) } correctly lists both options without nesting or syntax errors.
  4. Final Answer:

    options { timeout(time: 2, unit: 'MINUTES') retry(2) } -> Option B
  5. Quick Check:

    Timeout and retry as separate options inside options block [OK]
Hint: Use named params for timeout and separate retry inside options [OK]
Common Mistakes:
  • Nesting retry inside timeout block
  • Using timeout without named params
  • Wrong parameter order or types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard