0
0
Jenkinsdevops~10 mins

Rollback strategies in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Rollback strategies
Detect failure in deployment
Decide rollback needed?
NoContinue monitoring
Yes
Choose rollback method
Revert to
previous
version
Verify rollback success
Resume normal operations
This flow shows how Jenkins detects deployment failure, decides to rollback, chooses a rollback method, and verifies success before resuming.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Deploy') {
      steps {
        sh 'deploy.sh || exit 1'
      }
    }
    stage('Rollback') {
      when { failure() }
      steps {
        sh 'rollback.sh'
      }
    }
  }
}
A Jenkins pipeline that deploys an app and runs rollback if deployment fails.
Process Table
StepActionConditionResultNext Step
1Run deploy.shN/AFails with errorGo to rollback stage
2Check failure()Deployment failedTrueExecute rollback.sh
3Run rollback.shN/ARollback to previous versionVerify rollback
4Verify rollbackRollback success?SuccessResume normal operations
5EndN/ADeployment stablePipeline complete
💡 Rollback successful, deployment restored to stable version
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
deployment_statusnot startedfailedfailedrolled backstable
rollback_neededfalsefalsetruetruefalse
Key Moments - 2 Insights
Why does the rollback stage run only after deployment fails?
Because the 'when { failure() }' condition in the Jenkins pipeline triggers rollback only if the deploy stage fails, as shown in execution_table row 2.
What happens if rollback.sh also fails?
The pipeline would stop or report failure; this example assumes rollback.sh succeeds (row 3). Handling rollback failure requires additional error steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the deployment_status after step 3?
Afailed
Brolled back
Cstable
Dnot started
💡 Hint
Check variable_tracker column 'After Step 3' for deployment_status
At which step does Jenkins decide to run rollback.sh?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at execution_table row 2 where failure() condition triggers rollback
If deploy.sh succeeds, what happens to the rollback stage?
ARollback runs anyway
BPipeline fails
CRollback stage is skipped
DRollback runs after verification
💡 Hint
Recall 'when { failure() }' means rollback runs only on failure (see key_moments 1)
Concept Snapshot
Jenkins rollback strategies:
- Detect deployment failure
- Use 'when { failure() }' to trigger rollback stage
- Rollback methods: revert, redeploy, or backup
- Verify rollback success before continuing
- Ensures stable deployment state
Full Transcript
This Jenkins rollback strategy pipeline runs a deployment script. If deployment fails, Jenkins detects failure and triggers the rollback stage using the 'when { failure() }' condition. The rollback stage runs a rollback script to restore the previous stable version. After rollback, the pipeline verifies success and resumes normal operations. Variables track deployment status and rollback need. This flow ensures deployments can be safely reverted if errors occur.