Bird
Raised Fist0
Jenkinsdevops~10 mins

Rollback strategies in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Rollback strategies
Detect failure in deployment
↓
Decide rollback needed?
No→Continue 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.

Practice

(1/5)
1. What is the main purpose of a rollback strategy in Jenkins deployments?
easy
A. To automatically increase server resources during deployment
B. To speed up the deployment process by skipping tests
C. To revert to a previous stable version when a deployment fails
D. To permanently delete old versions of the application

Solution

  1. Step 1: Understand rollback purpose

    Rollback strategies are designed to undo changes when something goes wrong.
  2. Step 2: Identify rollback action in Jenkins

    In Jenkins, rollback means returning to a previous stable version to fix deployment issues.
  3. Final Answer:

    To revert to a previous stable version when a deployment fails -> Option C
  4. Quick Check:

    Rollback = revert to stable version [OK]
Hint: Rollback means going back to a safe version after failure [OK]
Common Mistakes:
  • Confusing rollback with speeding up deployment
  • Thinking rollback deletes old versions
  • Assuming rollback increases server resources
2. Which Jenkins pipeline syntax correctly implements a simple rollback using try-catch?
easy
A. try: deploy() except: rollback()
B. try { deploy() } catch { rollback() }
C. try deploy() catch rollback()
D. try { deploy() } catch (Exception e) { rollback() }

Solution

  1. Step 1: Recall Jenkins pipeline syntax

    Jenkins uses Groovy syntax where catch requires an exception parameter.
  2. Step 2: Identify correct try-catch structure

    try { deploy() } catch (Exception e) { rollback() } correctly uses catch (Exception e) to catch errors and run rollback.
  3. Final Answer:

    try { deploy() } catch (Exception e) { rollback() } -> Option D
  4. Quick Check:

    Jenkins try-catch needs exception parameter [OK]
Hint: Jenkins catch needs exception variable in parentheses [OK]
Common Mistakes:
  • Omitting exception parameter in catch block
  • Using Python-style try-except syntax
  • Missing braces around try or catch blocks
3. Given this Jenkins pipeline snippet, what will be the output if deploy() fails?
try {
  deploy()
  echo 'Deployment succeeded'
} catch (Exception e) {
  rollback()
  echo 'Rollback done'
}
medium
A. Rollback done
B. Deployment succeeded
C. No output
D. Deployment succeeded and Rollback done

Solution

  1. Step 1: Analyze try block behavior on failure

    If deploy() fails, the code inside try after deploy() is skipped.
  2. Step 2: Understand catch block execution

    The catch block runs rollback() and then prints 'Rollback done'.
  3. Final Answer:

    Rollback done -> Option A
  4. Quick Check:

    Failure triggers catch block output [OK]
Hint: Failed deploy skips try echo, runs catch echo [OK]
Common Mistakes:
  • Assuming 'Deployment succeeded' prints on failure
  • Thinking both messages print together
  • Ignoring catch block execution on error
4. Identify the error in this Jenkins pipeline rollback snippet:
try {
  deploy()
} catch {
  rollback()
}
medium
A. Missing exception parameter in catch block
B. deploy() should be inside catch block
C. rollback() should be outside try-catch
D. try block cannot be used without finally

Solution

  1. Step 1: Check catch block syntax in Jenkins

    Jenkins requires catch to have an exception parameter like catch (Exception e).
  2. Step 2: Confirm other options are incorrect

    deploy() belongs in try, rollback() in catch is correct, finally is optional.
  3. Final Answer:

    Missing exception parameter in catch block -> Option A
  4. Quick Check:

    Catch needs exception parameter [OK]
Hint: Catch block must include exception variable [OK]
Common Mistakes:
  • Leaving catch parentheses empty
  • Moving deploy() into catch block
  • Thinking finally is mandatory
5. You want to automate rollback in Jenkins only if deployment fails and the failure is due to a test error. Which approach best achieves this?
hard
A. Always run rollback after deployment regardless of success
B. Use try-catch and check exception message for test failure before rollback
C. Manually trigger rollback after checking logs
D. Skip rollback and notify team on any failure

Solution

  1. Step 1: Understand conditional rollback need

    Rollback should run only if failure is due to test errors, not all failures.
  2. Step 2: Use try-catch with exception check

    In Jenkins, catch block can inspect exception details to decide if rollback is needed.
  3. Step 3: Evaluate other options

    Always running rollback wastes time; manual rollback is slow; skipping rollback misses fixes.
  4. Final Answer:

    Use try-catch and check exception message for test failure before rollback -> Option B
  5. Quick Check:

    Conditional rollback uses exception checks [OK]
Hint: Check error type in catch before rollback [OK]
Common Mistakes:
  • Running rollback unconditionally wastes resources
  • Relying on manual rollback delays fixes
  • Ignoring failure cause in rollback decision
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