Bird
Raised Fist0
Jenkinsdevops~10 mins

Failing fast principle in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Failing fast principle
Start Pipeline
↓
Run Step 1
↓
Step 1 Success?
No→Fail Fast: Stop Pipeline
Yes↓
Run Step 2
↓
Step 2 Success?
No→Fail Fast: Stop Pipeline
Yes↓
Continue Steps or Finish
The pipeline runs steps one by one. If any step fails, it stops immediately to save time and resources.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'exit 1' // simulate failure
      }
    }
    stage('Test') {
      steps {
        sh 'echo "Test stage skipped due to Build failure"'
      }
    }
  }
}
This Jenkins pipeline has a Build stage that fails immediately ('exit 1'). The subsequent Test stage is skipped, demonstrating the fail fast principle.
Process Table
StepActionResultPipeline Status
1Start pipelinePipeline startedRunning
2Run Build stageCommand 'exit 1' executedRunning
3Check Build resultFailure detectedFail Fast: Pipeline stopped
4No further stages runPipeline terminated earlyStopped
💡 Build stage failed, so pipeline stopped immediately due to fail fast principle
Status Tracker
VariableStartAfter Step 1After Step 2Final
pipeline_statusNot startedRunningRunningStopped
build_stage_resultNot runFailureNot runFailure
Key Moments - 2 Insights
Why does the pipeline stop immediately after the build stage fails?
Because of the fail fast principle, the pipeline stops at the first failure to avoid wasting time on later steps. See execution_table row 3.
What happens if the build stage succeeds?
If the build stage succeeds, the pipeline continues to the next stages. This is shown by the 'Yes' path in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the pipeline status after step 2?
ARunning
BStopped
CFailed but continuing
DNot started
💡 Hint
Check the 'Pipeline Status' column at step 2.
At which step does the pipeline detect failure and stop?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Result' column where failure is detected and pipeline stops.
If the build command was 'exit 0' instead of 'exit 1', what would happen?
APipeline stops immediately
BPipeline fails later
CPipeline continues to next stages
DPipeline status is unknown
💡 Hint
Refer to the concept_flow where success leads to continuing the pipeline.
Concept Snapshot
Failing Fast Principle in Jenkins Pipeline:
- Pipeline runs stages sequentially.
- If any stage fails, pipeline stops immediately.
- Saves time and resources by not running unnecessary steps.
- Use 'exit 1' in shell to simulate failure.
- Helps catch errors early and fix quickly.
Full Transcript
The failing fast principle means stopping a Jenkins pipeline as soon as a failure happens. The pipeline starts and runs the first stage. If the stage succeeds, it moves on. If it fails, like with 'exit 1' command, the pipeline stops immediately. This saves time by not running later steps that depend on success. The execution table shows the pipeline starting, running the build stage, detecting failure, and stopping. Variables track the pipeline status changing from running to stopped. This principle helps catch problems early and avoid wasting resources.

Practice

(1/5)
1. What does the failing fast principle mean in Jenkins pipelines?
easy
A. Run all steps twice to check for errors
B. Stop the pipeline immediately when an error occurs
C. Ignore errors and continue running all steps
D. Delay error handling until the end of the pipeline

Solution

  1. Step 1: Understand the failing fast principle

    The principle means to stop work quickly when an error happens to save time and resources.
  2. Step 2: Apply to Jenkins pipelines

    In Jenkins, this means the pipeline should stop immediately on error instead of continuing.
  3. Final Answer:

    Stop the pipeline immediately when an error occurs -> Option B
  4. Quick Check:

    Fail fast = stop immediately on error [OK]
Hint: Fail fast means stop immediately on error [OK]
Common Mistakes:
  • Thinking pipeline should continue despite errors
  • Confusing fail fast with retrying steps
  • Assuming errors are handled at the end
2. Which Jenkins pipeline syntax correctly fails fast when a condition is not met?
easy
A. if (!condition) { echo('Fail fast') }
B. if (!condition) { sleep(10) }
C. if (!condition) { retry(3) }
D. if (!condition) { error('Fail fast') }

Solution

  1. Step 1: Identify the Jenkins command to fail fast

    The error() step immediately stops the pipeline with a failure.
  2. Step 2: Check the condition usage

    If the condition is false, calling error() triggers fail fast behavior.
  3. Final Answer:

    if (!condition) { error('Fail fast') } -> Option D
  4. Quick Check:

    Use error() to fail fast [OK]
Hint: Use error() to stop pipeline immediately [OK]
Common Mistakes:
  • Using echo() which only prints message
  • Using retry() which retries steps instead of failing
  • Using sleep() which delays but does not fail
3. What will be the output of this Jenkins pipeline snippet?
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          if (false) {
            error('Fail fast triggered')
          }
          echo('Step completed')
        }
      }
    }
  }
}
medium
A. Pipeline prints 'Step completed' and succeeds
B. Pipeline fails with message 'Fail fast triggered'
C. Pipeline hangs indefinitely
D. Pipeline skips 'Step completed' but succeeds

Solution

  1. Step 1: Analyze the condition in the script block

    The condition is false, so error() is not called.
  2. Step 2: Check what happens next

    The pipeline executes echo('Step completed') and continues successfully.
  3. Final Answer:

    Pipeline prints 'Step completed' and succeeds -> Option A
  4. Quick Check:

    False condition skips error(), so pipeline succeeds [OK]
Hint: False condition means error() not called, pipeline continues [OK]
Common Mistakes:
  • Assuming error() runs even if condition is false
  • Thinking pipeline fails without error() call
  • Confusing echo() with error()
4. Identify the error in this Jenkins pipeline snippet that tries to fail fast:
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        script {
          if (buildStatus == 'FAILURE')
            error('Build failed')
        }
      }
    }
  }
}
medium
A. buildStatus variable is undefined
B. Incorrect use of error() function
C. Missing braces {} around if block
D. Pipeline syntax requires 'steps' outside 'script'

Solution

  1. Step 1: Check variable definitions

    The buildStatus variable is not defined in the pipeline context.
  2. Step 2: Verify standard Jenkins variables

    In Jenkins, use currentBuild.result or define buildStatus from previous steps to check status.
  3. Final Answer:

    buildStatus variable is undefined -> Option A
  4. Quick Check:

    Undefined variables cause runtime errors [OK]
Hint: Check for undefined variables in scripts [OK]
Common Mistakes:
  • Assuming error() usage is wrong
  • Ignoring undefined variables without context
  • Misplacing steps and script blocks
5. You want your Jenkins pipeline to fail fast if any test stage fails, but continue running other stages if tests pass. Which approach best implements this?
hard
A. Use sleep() to delay failure until all tests complete
B. Use try-catch around test stages and call error() in catch
C. Use error() inside each test stage to stop pipeline on failure
D. Use catchError to mark test failures but continue pipeline

Solution

  1. Step 1: Understand fail fast requirement

    Fail fast means stop pipeline immediately on test failure, so no further stages run.
  2. Step 2: Evaluate options

    Using error() inside each test stage stops pipeline immediately on failure, matching fail fast.
  3. Final Answer:

    Use error() inside each test stage to stop pipeline on failure -> Option C
  4. Quick Check:

    Fail fast = error() stops pipeline immediately [OK]
Hint: Use error() in failing stages to stop pipeline fast [OK]
Common Mistakes:
  • Using catchError which allows pipeline to continue
  • Using try-catch which delays failure
  • Using sleep() which only delays, not fails
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