, Mark Joseph Colibao, home

CI/CD for a .NET API on Azure Container Apps with GitHub Actions

A complete workflow that tests a .NET API, pushes its image and deploys it to Azure Container Apps, with every setting it needs in Azure and GitHub.

Written 3 min read

CI builds and tests a .NET API, then CD pushes its image to Azure Container Registry and deploys it to Azure Container Apps. It signs in with OIDC, so GitHub holds no password (how OIDC works).

What one run does
  • Pull request → ci only
  • Push to main, or Run workflow on main → ci, then deploy
  1. ci

    1. checkout
    2. setup-dotnet 10.0.x
    3. dotnet restore
    4. dotnet build
    5. dotnet test
  2. deployneeds: ci

    environment: production

    1. checkout
    2. azure/login with OIDC (id-token: write)
    3. docker build and push, tagged with the commit SHA
    4. az containerapp update --image

Each deploy makes a new revision, so you can see which commit runs.

Copy: marks a value you'll need in GitHub.

1. Pick a trigger

Pull requests always run CI only. Keep push to deploy every merge to main; remove it to deploy only when you press Run workflow.

YAML
on:
  pull_request:
    branches: [main]
  push:
    branches: [main] # automatic: every merge to main deploys
  workflow_dispatch: # manual: the Run workflow button on the Actions tab

The Run workflow button appears once the workflow is on the default branch.

2. Create the container registry

Run after az login, or in Cloud Shell (>_ in the portal). Later tabs reuse these names.

bash
RG=<resource-group>; LOCATION=<location>; ACR=<registry-name>
CAE=<containerapps-environment>; APP=<container-app>

az group create --name $RG --location $LOCATION
az acr create --name $ACR --resource-group $RG --sku Basic

ACR_ID=$(az acr show --name $ACR --query id -o tsv)
LOGIN_SERVER=$(az acr show --name $ACR --query loginServer -o tsv)
echo "$LOGIN_SERVER"   # copy for GitHub

3. Create the Container App

It starts on Microsoft's sample image, because the registry is still empty.

bash
az extension add --name containerapp --upgrade
az provider register --namespace Microsoft.App
az provider register --namespace Microsoft.OperationalInsights

az containerapp env create --name $CAE --resource-group $RG --location $LOCATION
az containerapp create --name $APP --resource-group $RG --environment $CAE \
  --image mcr.microsoft.com/k8se/quickstart:latest --target-port 80 --ingress external

4. Let the app pull with its own identity

The app gets AcrPull on the registry, so it needs no registry password. Ingress moves to 8080, where .NET 8+ images listen. The sample image stops answering until your first deploy; that's expected.

bash
az containerapp registry set --name $APP --resource-group $RG \
  --identity system --server "$LOGIN_SERVER"   # also turns on the app's identity

APP_PRINCIPAL=$(az containerapp show -n $APP -g $RG --query identity.principalId -o tsv)
az role assignment create --assignee-object-id "$APP_PRINCIPAL" \
  --assignee-principal-type ServicePrincipal --role AcrPull --scope "$ACR_ID"

# Identity pulls need ARM tokens. If this says "disabled",
# run it again with "update" and --status enabled
az acr config authentication-as-arm show -r $ACR
az containerapp ingress update --name $APP --resource-group $RG --target-port 8080

5. Create the deploy identity

An app registration that trusts GitHub's tokens for the production environment. A job with an environment sends the environment in its token, not the branch, so the trust names the environment.

Save as credential.json:

JSON
{
  "name": "github-production",
  "issuer": "https://token.actions.githubusercontent.com",
  "subject": "repo:<owner>@<owner-id>/<repo>@<repo-id>:environment:production",
  "audiences": ["api://AzureADTokenExchange"]
}
bash
DEPLOY_APP_ID=$(az ad app create --display-name github-deploy-$APP --query appId -o tsv)
az ad sp create --id "$DEPLOY_APP_ID"
az ad app federated-credential create --id "$DEPLOY_APP_ID" --parameters credential.json

echo "client: $DEPLOY_APP_ID"   # copy these three for GitHub
az account show --query "{tenant: tenantId, subscription: id}" -o tsv

Older repositories send names in the subject, not IDs. The OIDC note shows how to tell.

6. Give it two roles

AcrPush on the registry and Contributor on the app only. Nothing on the resource group or subscription.

bash
DEPLOY_SP=$(az ad sp show --id "$DEPLOY_APP_ID" --query id -o tsv)
APP_RES_ID=$(az containerapp show -n $APP -g $RG --query id -o tsv)

az role assignment create --assignee-object-id "$DEPLOY_SP" \
  --assignee-principal-type ServicePrincipal --role AcrPush --scope "$ACR_ID"
az role assignment create --assignee-object-id "$DEPLOY_SP" \
  --assignee-principal-type ServicePrincipal --role Contributor --scope "$APP_RES_ID"

In the registry's ABAC mode, use Container Registry Repository Writer and Reader instead of AcrPush and AcrPull.

7. Set up GitHub

A production environment that only main can deploy to, with these values. The IDs aren't passwords, but Microsoft's guide keeps them in secrets.

NameKindValue
AZURE_CLIENT_IDsecretClient ID
AZURE_TENANT_IDsecretTenant ID
AZURE_SUBSCRIPTION_IDsecretSubscription ID
ACR_NAMEvariableRegistry name
ACR_LOGIN_SERVERvariableRegistry login server
CONTAINER_APPvariableApp name
RESOURCE_GROUPvariableResource group
bash
REPO=<owner>/<repo>

gh api -X PUT "repos/$REPO/environments/production" --input - <<'EOF'
{ "deployment_branch_policy": { "protected_branches": false, "custom_branch_policies": true } }
EOF
gh api -X POST "repos/$REPO/environments/production/deployment-branch-policies" \
  -f name=main -f type=branch

gh secret set AZURE_CLIENT_ID --env production   # prompts for the value
gh secret set AZURE_TENANT_ID --env production
gh secret set AZURE_SUBSCRIPTION_ID --env production

gh variable set ACR_NAME --env production --body "<registry-name>"
gh variable set ACR_LOGIN_SERVER --env production --body "<login-server>"
gh variable set CONTAINER_APP --env production --body "<container-app>"
gh variable set RESOURCE_GROUP --env production --body "<resource-group>"

GitHub Free, private repository? Environments aren't available there. Put the values under Settings → Secrets and variables → Actions, remove environment: production from the workflow, trust Entity type: Branch main in step 5, and protect main.

8. Add the Dockerfile

Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish src/MyApi/MyApi.csproj -c Release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /app .
USER app
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApi.dll"]

Use your own project path and DLL name. USER app runs it as the non-root user in .NET 8+ images.

9. Add the workflow

Save as .github/workflows/ci-cd.yml. ci runs on every trigger; deploy runs only on main, after ci passes.

YAML
name: ci-cd

on:
  pull_request:
    branches: [main]
  push:
    branches: [main] # remove for manual-only deploys
  workflow_dispatch:

permissions:
  contents: read

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@<full-length-commit-sha> # v7
      - uses: actions/setup-dotnet@<full-length-commit-sha> # v6
        with:
          dotnet-version: '10.0.x'
      - run: dotnet restore
      - run: dotnet build --no-restore --configuration Release
      - run: dotnet test --no-build --configuration Release

  deploy:
    needs: ci
    if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: production
    concurrency:
      group: production
      cancel-in-progress: false # let a running deploy finish
    permissions:
      contents: read
      id-token: write # only this job may ask for an OIDC token
    env:
      IMAGE: ${{ vars.ACR_LOGIN_SERVER }}/my-api:${{ github.sha }}
    steps:
      - uses: actions/checkout@<full-length-commit-sha> # v7

      - uses: azure/login@<full-length-commit-sha> # v3
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

      - name: Build and push the image
        run: |
          az acr login --name "${{ vars.ACR_NAME }}"
          docker build --tag "$IMAGE" .
          docker push "$IMAGE"

      - name: Deploy
        run: |
          az containerapp update --name "${{ vars.CONTAINER_APP }}" \
            --resource-group "${{ vars.RESOURCE_GROUP }}" --image "$IMAGE"

Pin each action to a full commit SHA from its releases page, as in the OIDC note. The image's commit SHA is the tag, not latest, so each deploy makes a new revision and you can see which commit runs.

10. Run it

  1. Open a pull request with the Dockerfile and workflow. Only ci runs.
  2. Merge it. deploy starts on its own (or press Run workflow on main).
  3. Open the app's Application Url. Your API should answer.

When it fails

  • AADSTS700213 or another sign-in error: see Fix and maintain GitHub Actions sign-in to Azure.
  • docker push denied: the deploy identity lacks AcrPush.
  • The image won't pull: the app lacks AcrPull, or ARM tokens are off.
  • The app doesn't answer: the target port isn't 8080.

Sources