0
0
Jenkinsdevops~5 mins

Failing fast principle in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
The failing fast principle helps catch errors early in a process so you don't waste time on steps that will fail later. In Jenkins, it means stopping a build as soon as a problem is found to save resources and fix issues quickly.
When you want to stop a build immediately if a test fails to save time.
When you want to avoid running deployment steps if the build or tests fail.
When you want quick feedback on code changes to fix errors faster.
When you want to reduce resource use by not running unnecessary steps after failure.
When you want to keep your pipeline clean by not continuing with broken code.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
        sh 'exit 0'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
        sh 'exit 1' // Simulate test failure to fail fast
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying...'
      }
    }
  }
  post {
    failure {
      echo 'Build failed early, stopping pipeline.'
    }
  }
}

This Jenkinsfile defines a simple pipeline with three stages: Build, Test, and Deploy.

The Test stage runs a command that fails (exit 1) to simulate a test failure.

Because of this failure, Jenkins stops the pipeline immediately and does not run the Deploy stage.

The post section with failure prints a message when the pipeline fails early.

Commands
This command updates the Jenkins job configuration with the Jenkinsfile that implements the failing fast principle.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline
Expected OutputExpected
Job 'my-pipeline' updated successfully.
This command starts the Jenkins pipeline build to test the failing fast behavior.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 for job 'my-pipeline'.
This command fetches the console output of the first build to verify that the pipeline stopped after the test failure.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
Building... Testing... [Pipeline] sh + exit 1 [Pipeline] [Pipeline] echo Build failed early, stopping pipeline. Finished: FAILURE
Key Concept

If a step fails, stop the pipeline immediately to save time and resources.

Common Mistakes
Not stopping the pipeline after a failure and letting all stages run.
This wastes time and resources running steps that will fail or cause problems later.
Configure the pipeline to fail fast by letting Jenkins stop on errors automatically.
Ignoring the failure messages in the console output.
You miss the chance to fix problems early and the pipeline continues with broken code.
Always check the console output to catch failures early and act on them.
Summary
The Jenkinsfile shows how to stop a pipeline immediately when a test fails.
Commands update the Jenkins job, start a build, and check the output to confirm fail fast.
Failing fast saves time and resources by not running unnecessary steps after failure.