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.
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).
- Pull request → ci only
- Push to main, or Run workflow on main → ci, then deploy
ci- checkout
- setup-dotnet 10.0.x
- dotnet restore
- dotnet build
- dotnet test
deployneeds: cienvironment: production
- checkout
- azure/login with OIDC (id-token: write)
- docker build and push, tagged with the commit SHA
- 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.
on:
pull_request:
branches: [main]
push:
branches: [main] # automatic: every merge to main deploys
workflow_dispatch: # manual: the Run workflow button on the Actions tabThe 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.
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 GitHubCreate a resource → Container Registry → Create:
- Resource group: Create new. Use it for everything here.
- Registry name: 5 to 50 lowercase letters and numbers, unique in Azure.
- Pricing plan: Basic.
- Role assignment permissions mode: RBAC Registry Permissions.
Review + create → Create → Go to resource.
Copy: the registry name and the Login server.
3. Create the Container App
It starts on Microsoft's sample image, because the registry is still empty.
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 externalSearch Container Apps → Create → Container App:
- Resource group: the one from step 2.
- Deployment source: Container image.
- Container Apps environment: keep the suggested new one.
- Container tab: Use quickstart image.
- Ingress: enabled, Accepting traffic from anywhere.
Review + create → Create. Copy: the app name.
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.
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- App → Identity → System assigned → Status: On → Save.
- Registry → Access control (IAM) → Add role assignment → AcrPull → Members: Managed identity, your app → Review + assign.
- App → Ingress → Target port:
8080→ Save. - Linking an empty registry has no portal option, so run this once in Cloud Shell (
>_, Bash):
az containerapp registry set --name <container-app> --resource-group <resource-group> \
--identity system --server <login-server>
# 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 <registry-name>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:
{
"name": "github-production",
"issuer": "https://token.actions.githubusercontent.com",
"subject": "repo:<owner>@<owner-id>/<repo>@<repo-id>:environment:production",
"audiences": ["api://AzureADTokenExchange"]
}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 tsvIn the Entra admin center:
- App registrations → New registration → a name → Single tenant only → Register.
- Copy: the Application (client) ID and Directory (tenant) ID.
- Certificates & secrets → Federated credentials → Add credential: scenario GitHub Actions deploying Azure resources, your organization and repository, Entity type: Environment,
production, a name → Add. - Copy: the Subscription ID (portal → Subscriptions).
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.
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"Do this twice, from each resource's own Access control (IAM) → Add role assignment:
- Registry: AcrPush (under Job function roles).
- App: Contributor (under Privileged administrator roles).
Members: User, group, or service principal → your app registration → Review + assign.
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.
| Name | Kind | Value |
|---|---|---|
AZURE_CLIENT_ID | secret | Client ID |
AZURE_TENANT_ID | secret | Tenant ID |
AZURE_SUBSCRIPTION_ID | secret | Subscription ID |
ACR_NAME | variable | Registry name |
ACR_LOGIN_SERVER | variable | Registry login server |
CONTAINER_APP | variable | App name |
RESOURCE_GROUP | variable | Resource group |
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>"Repository Settings → Environments → New environment → production:
- Deployment branches and tags: Selected branches and tags → add a Branch rule for
main. - Required reviewers (optional): one approval lets a deploy run.
- Environment secrets and Environment variables: add each row of the table.
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
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.
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
- Open a pull request with the Dockerfile and workflow. Only
ciruns. - Merge it.
deploystarts on its own (or press Run workflow onmain). - Open the app's Application Url. Your API should answer.
When it fails
AADSTS700213or another sign-in error: see Fix and maintain GitHub Actions sign-in to Azure.docker pushdenied: the deploy identity lacksAcrPush.- 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
- Pull images with a managed identity (Microsoft Learn)
- Quickstart: container app with the CLI (Microsoft Learn)
- az containerapp (Azure CLI reference)
- Create a container registry in the portal (Microsoft Learn)
- Registry roles and ABAC mode (Microsoft Learn)
- Connect GitHub Actions to Azure with OIDC (Microsoft Learn)
- .NET 8 container changes: port 8080, app user (Microsoft Learn)
- Manage deployment environments (GitHub Docs)