0
0
Jenkinsdevops~5 mins

Rollback strategies in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes a new software update causes problems. Rollback strategies help you quickly go back to a previous working version to keep your app running smoothly.
When a new deployment causes errors or crashes in your application.
When you want to test a new feature but need a quick way to undo changes if it fails.
When a configuration change breaks your system and you need to restore the last stable state.
When you want to minimize downtime by automatically reverting to a safe version after a failed deployment.
When you want to keep a history of deployments to choose which version to restore.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the application...'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying the application...'
      }
    }
  }
  post {
    failure {
      echo 'Deployment failed! Rolling back to previous stable version...'
      script {
        // Simulate rollback command
        sh 'echo Rollback executed'
      }
    }
  }
}

This Jenkinsfile defines a simple pipeline with build and deploy stages.

The post section runs after the stages finish.

If deployment fails, the failure block triggers a rollback step.

This rollback step runs a shell command to revert to the previous stable version.

Commands
This command updates the Jenkins job configuration with the latest pipeline definition to include rollback steps.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline.yaml
Expected OutputExpected
Job 'my-pipeline' updated successfully
--conf - Specifies the Jenkins configuration file to use
Starts a build of the Jenkins pipeline to test deployment and rollback behavior.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 for job my-pipeline
Shows the console output of build #1 to verify if rollback was triggered on failure.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploying the application... [Pipeline] sh + echo Rollback executed Rollback executed [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline
Key Concept

If a deployment fails, automatically or manually revert to the last stable version to keep your system running smoothly.

Common Mistakes
Not defining a rollback step in the Jenkins pipeline.
Without a rollback step, failed deployments leave the system broken until manually fixed.
Add a post-failure block in the Jenkinsfile to run rollback commands automatically.
Assuming rollback will happen without testing the pipeline.
Rollback steps may have errors or not trigger as expected, causing downtime.
Test the pipeline with intentional failures to confirm rollback works correctly.
Not keeping previous stable versions available for rollback.
Rollback requires a known good version to restore; without it, rollback fails.
Maintain versioned builds or artifacts so rollback can restore a stable state.
Summary
Define rollback steps in Jenkins pipelines using the post-failure block.
Use commands to update and run Jenkins jobs to test rollback behavior.
Always test rollback to ensure quick recovery from failed deployments.