# Deploy for Real

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

> Set up Azure App Service once, sign in without passwords, deploy the pushed image, fix wrong app names, and send production only on a published release.

## Deploy for Real

So far the image sits in the registry. Nobody can use it yet.

In this lesson you put it on a real server:

- **Azure App Service**, a place that runs your container on the web.
- Each environment is its own app: `shop-web-staging` and `shop-web-production`.
- The pipeline signs in with OIDC (from the last lesson) and tells the app which image to run.

You will:

- do the one-time Azure setup.
- deploy the pushed image.
- fix the two most common deploy errors.
- send production only when someone publishes a release.

**Simple lesson:** a deploy tells a server "run this exact image now".

### A Real-World Example

Think about a school notice board.

- The office prints one notice (the image).
- A helper with a key (the sign-in) opens the glass case.
- The helper pins the notice on the right board: junior block or senior block (the app name).
- Senior block gets it only after the principal signs it (the release).

Now map it:

- **Key** = OIDC sign-in.
- **Board name** = App Service name.
- **Principal's signature** = a published release.

**Simple lesson:** right key, right board, right notice, and production only after sign-off.

## One-Time Azure Setup

You do these once, by hand or with a script. The pipeline does not do them.

- Create a **resource group**: a folder for your Azure things.
- Create an **App Service plan**: the machine size you pay for.
- Create one **web app** per environment.
- Set up the OIDC trust from Environments and Secrets: app registration, role, federated credential.

The commands, using the Azure CLI:

```bash
az group create --name shop-rg --location centralindia
az appservice plan create --name shop-plan --resource-group shop-rg --is-linux --sku B1
az webapp create --name shop-web-staging --resource-group shop-rg --plan shop-plan --container-image-name nginx:1.27-alpine
az webapp create --name shop-web-production --resource-group shop-rg --plan shop-plan --container-image-name nginx:1.27-alpine
az webapp config appsettings set --name shop-web-staging --resource-group shop-rg --settings WEBSITES_PORT=8080
```

- App names must be unique in all of Azure. Add your name, like `shop-web-staging-ravi`.
- `WEBSITES_PORT=8080` tells Azure which port your container listens on.
- The full OIDC steps are in the starter repo README.

**Real tools vs this lab:** the lab pretends the apps and sign-in already exist. In your own account, App Service B1 costs money each hour. Delete the resource group when you finish: `az group delete --name shop-rg`.

**Simple lesson:** create the apps and the trust once. The pipeline reuses them every run.

## Deploy the Image

The deploy step names two things: **which app** and **which image**.

| Platform | Deploy step | Image input |
|---|---|---|
| GitHub Actions | `azure/webapps-deploy@v3` after `azure/login` | `images: ghcr.io/shop/shop-web:${{ github.sha }}` |
| Azure Pipelines | `AzureWebAppContainer@1` (signs in by itself) | `containers: ghcr.io/shop/shop-web:$(Build.SourceVersion)` |

**Simple lesson:** deploy the image tag you just pushed. Never a folder, never `latest`.

## Fix It: Deploy Before the Image Exists

- Jobs start together unless you say otherwise.
- If the deploy does not wait for `build-image`, the image is not in the registry yet.

**Simple lesson:** the deploy needs the job that pushes the image.

## Fix It: Wrong App Name

- One letter wrong, and Azure cannot find the app.
- The error says the app does not exist, or the resource was not found.

**Simple lesson:** copy app names from the Azure portal. Do not type them from memory.

## Fix It: Deploy Without Sign-In

On GitHub the deploy step does not sign in by itself. `azure/login` must come first.

**Simple lesson:** sign in first, then deploy, in the same job.

## Production on a Published Release

Common team rule:

- Every push to main goes to **staging**.
- **Production** goes live only when someone publishes a release, like `v1.4.42`.

| Platform | Trigger | Run production only for releases |
|---|---|---|
| GitHub Actions | `release: types: [published]` | `if: github.event_name == 'release'` |
| Azure Pipelines | `trigger: tags: include: ['v*']` | `condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')` |

**Branch flows:** most teams use **trunk-based** work: short branches, merge to main often, release from main with a tag. Long-lived `develop` and `release` branches (GitFlow) make more merges and slower releases. Start with trunk-based.

A release points at a commit that is already on main. So the image with that SHA tag already exists and was tested on staging. Production gets the same image.

**Simple lesson:** main goes to staging. A published release goes to production.

1. Do the one-time setup above with your own app names.
2. Replace the app names and `ghcr.io/shop/shop-web` in your workflow with yours.
3. Make the shop-web package public (Package settings → Change visibility).
4. Push to main and wait for deploy-staging.
5. Open `https://<your-staging-app>.azurewebsites.net/health`.

**What you should see:** a green run, and the health page answering from your own app (`ok` for React, `{"status":"healthy"}` for .NET).

## Quick Check

## Cheat Sheet

GitHub Actions:

```yaml
on:
  push:
    branches: [main]
  release:
    types: [published]

jobs:
  deploy-staging:
    needs: build-image
    if: github.event_name == 'push'
    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 }}

  deploy-production:
    needs: build-image
    if: github.event_name == 'release'
    runs-on: ubuntu-latest
    environment: production
    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:${{ github.sha }}
```

Azure Pipelines:

```yaml
trigger:
  branches:
    include: [main]
  tags:
    include: ['v*']

jobs:
  - 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)

  - deployment: deploy_production
    dependsOn: build_image
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
    environment: production
    strategy:
      runOnce:
        deploy:
          steps:
            - task: AzureWebAppContainer@1
              inputs:
                azureSubscription: shop-production-oidc
                appName: shop-web-production
                containers: ghcr.io/shop/shop-web:$(Build.SourceVersion)
```

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Deploying `latest` | It is the newest image | Deploy the commit SHA tag | You cannot tell what is live |
| No needs on the deploy | Jobs run top to bottom | Jobs start together | "Image not found" |
| Typing the app name | I remember it | Copy it from the portal | "App does not exist" |
| No `azure/login` (GitHub) | The deploy signs in | It uses the job's sign-in | "No credentials found" |
| Private image, no registry access | Azure can see my packages | It cannot, unless you allow it | The app shows an error page |
| Leaving the apps running | They are free | B1 costs money each hour | A surprise bill |

## What to Remember

- One-time setup: resource group, plan, one app per environment, OIDC trust.
- The deploy names the app and the exact image tag.
- Deploy needs the job that pushes the image. GitHub needs azure/login first.
- Main goes to staging. A published release goes to production.

## Exercises

Next: **Deploy and Smoke Test**
