How do you roll back a failed deployment in CI/CD?

When a deployment fails (due to bugs, misconfigurations, or performance issues), rolling back to a previous stable version is crucial to minimize downtime.
1. Common Rollback Strategies in CI/CD :
1. Manual Rollback :
* Developers manually revert to the previous stable release.
* Useful for smaller teams with low deployment frequency.

Example Command (Git) :
git revert <commit-id>
git push origin main?


2. Automated Rollback :
* CI/CD tools detect failures and automatically roll back to the last stable version.
* Requires automated monitoring and rollback triggers.
* Example tools : Kubernetes, AWS CodeDeploy, ArgoCD.

3. Versioned Deployment Rollback :
* Store previous deployments as versioned artifacts.
* If the latest deployment fails, redeploy the last working version.

Example (Docker) :
docker pull my-app:v1.0
docker run -d my-app:v1.0?


4. Blue-Green Deployment Rollback :
* Two identical environments (Blue - current stable, Green - new version).
* If the Green deployment fails, simply route traffic back to Blue.
* Used in Kubernetes & cloud environments.

5. Canary Deployment Rollback :
* New version is released gradually to a subset of users.
* If issues are detected, rollback is triggered before full release.
* Example : Kubernetes Canary Deployment.


2. How to Implement Rollback in CI/CD :
Example : Rollback in Kubernetes
* If a new deployment fails, rollback using:
kubectl rollout undo deployment my-app?

Example : Rollback in AWS CodeDeploy
* If a deployment fails, use:
aws deploy rollback-deployment --deployment-id <deployment-id>?

Example : Rollback in GitHub Actions
on: workflow_run
jobs:
  rollback:
    if: failure()
    runs-on: ubuntu-latest
    steps:
      - name: Deploy previous version
        run: |
          kubectl rollout undo deployment my-app?

3. Best Practices for Rollback in CI/CD :
* Use Automated Testing – Catch issues before deployment.
* Monitor Deployments – Use tools like Prometheus, Datadog for error detection.
* Always Keep Stable Versions – Never overwrite the last working build.
* Implement Feature Flags – Turn off new features without a full rollback.
* Use Immutable Infrastructure – Always deploy a new version instead of modifying existing servers.