# Rollback and Recovery

Course: [CI/CD](https://www.devtutorials.ai/learn/cicd) · Section: Continuous Delivery
Source: https://www.devtutorials.ai/learn/cicd/continuous-delivery/rollback-and-recovery

> Catch a production-only bug, roll back by itself with a failure() job, check the rollback worked, measure time to restore, and choose between rollback, fix forward and feature flags.

## Rollback and Recovery

Even with every check, some bugs only show up in production.

When that happens, one thing matters most: **how fast users get a working site back**.

In this lesson you:

- catch a production-only bug with a smoke test.
- make the pipeline roll back by itself.
- make sure it only rolls back when something fails.
- check that the rollback really worked.

**Simple lesson:** you cannot stop every bug. You can make every bug short.

### A Real-World Example

Think about a power cut at school.

- The lights go out in the middle of class.
- Nobody waits for the electrician to find the fault first.
- The **inverter** switches on in seconds. Class goes on.
- The electrician fixes the real fault later, calmly.

Now map it:

- **Power cut** = a bad release in production.
- **Inverter** = automatic rollback to the last good version.
- **Electrician, later** = the proper fix, in a new pull request.

**Simple lesson:** restore first, fix second.

## Why Bugs Reach Production

Staging is close to production, but never the same:

- Different data, like a customer name with an emoji.
- Different settings, like a missing production key.
- Different load, like 10,000 users instead of 3 testers.

So production needs its own smoke test, and a plan for when it fails.

**Simple lesson:** the last check before users is the production smoke test. Plan what happens when it fails.

## Automatic Rollback

**Rollback** = putting the last good version back.

The pipeline can do it by itself:

- A job that runs only **when an earlier job fails**.
- It signs in, runs the rollback, and checks the site again.

| Platform | Run only on failure |
|---|---|
| GitHub Actions | `if: failure()` |
| Azure Pipelines | `condition: failed()` |

The rollback is a normal deploy of an **older image tag**:

- The last image that worked is saved as a variable, `LAST_GOOD_TAG`.
- The rollback job deploys `ghcr.io/shop/shop-web:<LAST_GOOD_TAG>` to production.
- It works because every image has its own SHA tag and stays in the registry.
- After each good release, update `LAST_GOOD_TAG` to that SHA.

| Platform | Read the variable |
|---|---|
| GitHub Actions | `${{ vars.LAST_GOOD_TAG }}` |
| Azure Pipelines | `$(LAST_GOOD_TAG)` |

**Real tools vs this lab:** a real rollback needs the last good artifact or slot to still exist. Do not delete old images from the registry too soon. In this lab, `LAST_GOOD_TAG` is version 1.4.41, and it is always there. App Service **deployment slots** are another way back: swap the old slot in again.

**Simple lesson:** a rollback job with `failure()` turns a long outage into a short one.

### Only on Failure

- A rollback job without a condition runs after every release.
- Then it undoes good releases too.

### Check the Rollback Worked

- A rollback is a deploy too. It can fail.
- Smoke test production again at the end of the rollback job.

### Fix It: The Rollback Must Sign In

**Simple lesson:** treat the rollback like any deploy: sign in, run it, check it.

## Time to Restore

**Time to restore** = from "production is broken" to "production is fine again".

| How you recover | Typical time to restore |
|---|---|
| Someone notices, rebuilds, redeploys | Hours |
| Smoke test fails, a person rolls back | 30 to 60 minutes |
| Smoke test fails, the pipeline rolls back | A few minutes |

The lab shows this number after each rollback.

Practise a rollback on a quiet day. The worst time to learn your rollback is broken is during a real outage.

**Simple lesson:** measure time to restore. Automatic rollback makes it small.

## Roll Back, Fix Forward, or Flip a Flag

Rollback is not the only way out:

- **Roll back:** put the old version back. Best when the whole release is bad.
- **Fix forward:** ship a quick new fix. Best for tiny problems, or when the old code cannot run any more.
- **Feature flag:** turn off just the new feature, without a deploy. Best when the feature was built behind a switch.

Database changes make rollback hard. If the new release changed a table, the old code may not read it. Change the database in small steps that both versions understand.

**Simple lesson:** pick the fastest safe way back for this kind of problem.

## Quick Check

## Cheat Sheet

GitHub Actions:

```yaml
  rollback-production:
    needs: deploy-production
    if: failure()
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
      - uses: azure/webapps-deploy@v3
        with:
          app-name: shop-web-production
          images: ghcr.io/shop/shop-web:${{ vars.LAST_GOOD_TAG }}
      - run: curl --fail --retry 5 --retry-delay 3 --retry-all-errors https://shop-web-production.azurewebsites.net/health
```

Azure Pipelines:

```yaml
  - job: rollback_production
    dependsOn: deploy_production
    condition: failed()
    steps:
      - task: AzureWebAppContainer@1
        inputs:
          azureSubscription: shop-production-oidc
          appName: shop-web-production
          containers: ghcr.io/shop/shop-web:$(LAST_GOOD_TAG)
      - script: curl --fail --retry 5 --retry-delay 3 --retry-all-errors https://shop-web-production.azurewebsites.net/health
```

1. In GitHub, Settings → Secrets and variables → Actions → Variables: add `LAST_GOOD_TAG` with the SHA of your last green production run.
2. Add the rollback job to your workflow.
3. Publish a release where the health page is broken (rename it, as in Deploy and Smoke Test).

**What you should see:** deploy-production is red at the smoke step, rollback-production runs, and the health page works again. Fix the page and release again.

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| No production smoke test | Staging passed, so production is fine | Production can differ | Users find the bug first |
| Rollback job with no condition | It only matters on failure | Add `failure()` or `failed()` | Good releases get undone |
| Rollback without sign-in | It is a simple step | It needs access like a deploy | Production stays broken |
| `LAST_GOOD_TAG` never updated | Set it once | Update it after each good release | You roll back to a very old version |
| Never testing the rollback | It will work when needed | Practise it | It fails during a real outage |
| Rolling back after a database change | Rollback always works | The old code may not read new data | A second outage |

## What to Remember

- Restore first, fix second.
- Smoke test production, then roll back on `failure()`.
- Check the rollback with another smoke test.
- Measure time to restore.
- Roll back, fix forward or flip a flag: pick the fastest safe way.

## Exercises

Next: **After the Deploy**
