# Deploy and Smoke Test

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

> Add a deploy job, check the live app with a smoke test on its health page, retry for slow starts, and stop a broken release at staging before it reaches production.

## Deploy and Smoke Test

"Deploy succeeded" only means the files were copied.

It does **not** mean the site works.

In this lesson you:

- add a deploy job to staging.
- check the app is alive with a smoke test.
- give a slow app time to start.
- stop a broken release before production.

**Simple lesson:** after every deploy, ask the app if it is alive.

### A Real-World Example

Think about a new electric fan in your classroom.

- The electrician fits it on the ceiling. **Job done?**
- Not yet. You switch it on.
- If it spins, good. If it sparks, you switch off and call him back.
- You check each fan before the next room gets one.

Now map it:

- **Fitting the fan** = deploy.
- **Switching it on** = smoke test.
- **Next room** = production.

**Simple lesson:** fit it, switch it on, then move on.

## Deploying to Staging

You learned the deploy in Deploy for Real. Quick recap of the staging job:

1. **Wait** for the job that pushes the image.
2. **Sign in** to Azure (GitHub needs `azure/login`. Azure tasks sign in by themselves).
3. **Deploy** the image tag to `shop-web-staging`.

Build it once more from memory. It is the base for every check in this lesson.

**Simple lesson:** wait for the image, sign in, deploy. In that order.

## Smoke Test

**Smoke test** = a tiny, fast check that the app is up and answering.

- Most apps have a **health page**, like `/health`.
- It answers `200 OK` when the app is fine.
- `curl --fail` turns any other answer into a red step.

| What a health page often checks | Why |
|---|---|
| The app started | The basics |
| It can reach the database | Most pages need it |
| The version number | Proves the new build is live |

**Real tools vs this lab:** in this lab, `curl` talks to a simulated staging site. In a real pipeline, it calls your real staging address over the internet.

**Simple lesson:** a smoke test turns "copied" into "working".

## Give the App Time to Start

- A new version can take a few seconds to wake up.
- The first check may get `503 Service Unavailable`.
- That is not a broken app. It is a sleepy one.

Fix: retry a few times before giving up.

- `--retry 5` = try up to 5 more times.
- `--retry-delay 3` = wait 3 seconds between tries.
- `--retry-all-errors` = also retry on 503.

Do not retry forever. A broken app will never answer 200. Five tries is enough to tell "slow" from "broken".

**Simple lesson:** retry a little for slow starts. A real failure still fails.

## Stop Before Production

The whole point: a broken release should stop at staging.

- Production must **need** the staging job.
- If staging's smoke test fails, production is skipped.
- Real users never see the broken version.

Smoke test production too. Staging and production are never exactly the same. A wrong production setting only shows up in production.

**Simple lesson:** each environment proves itself before the next one gets the release.

## Quick Check

## Cheat Sheet

GitHub Actions:

```yaml
  deploy-staging:
    needs: build-image
    runs-on: ubuntu-latest
    environment: staging
    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-staging
          images: ghcr.io/shop/shop-web:${{ github.sha }}
      - run: curl --fail --retry 5 --retry-delay 3 --retry-all-errors https://shop-web-staging.azurewebsites.net/health
```

Azure Pipelines:

```yaml
  - deployment: deploy_staging
    dependsOn: build_image
    environment: staging
    strategy:
      runOnce:
        deploy:
          steps:
            - task: AzureWebAppContainer@1
              inputs:
                azureSubscription: shop-staging-oidc
                appName: shop-web-staging
                containers: ghcr.io/shop/shop-web:$(Build.SourceVersion)
            - script: curl --fail --retry 5 --retry-delay 3 --retry-all-errors https://shop-web-staging.azurewebsites.net/health
```

1. In your repo, add the smoke step after the staging deploy, with your own app URL.
2. Push to main and wait for the run.
3. Now break the health page: React, rename `public/health`. .NET, change `/health` to `/healthz` in `Program.cs`.
4. Push again, then undo it.

**What you should see:** the first run is green. The second run is red at the smoke step with `curl: (22) ... 404`, and production does not deploy.

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| No smoke test | Deploy green = site up | Ask the app itself | A dead site looks green |
| `curl` without `--fail` | curl fails on errors | Without it, 503 still exits 0 | Broken releases pass |
| Retrying forever | It will come up | Cap retries | A broken release blocks the pipeline for hours |
| Production does not need staging | They are separate | Production waits for staging | Broken code reaches users |
| Deploy before sign-in (GitHub) | The deploy signs in | `azure/login` first | "No credentials found" |

## What to Remember

- Deploy = wait for the image, sign in, deploy the tag.
- Smoke test = `curl --fail` on the health page.
- Retry a few times for slow starts, not forever.
- Production needs staging. Smoke test both.

## Exercises

Next: **Release Strategies**
