Skip to content

CI/CD - Deploy to Azure Container Apps #3

CI/CD - Deploy to Azure Container Apps

CI/CD - Deploy to Azure Container Apps #3

Workflow file for this run

# Generated by GLM 5.3 NVFP4
# Builds the Docker images in ACR and redeploys the Azure Container Apps.
#
# Images are built with `docker buildx build --push` rather than
# `az acr build`: ACR Tasks (the service behind `az acr build`) is not
# permitted on Azure for Students subscriptions (TasksOperationsNotAllowed).
#
# Prerequisites (one-time):
# - terraform apply in infra/ (creates ACR, DB, apps with placeholder images,
# the migrate job, and the Cloudflare DNS/certificate setup)
# - Azure login secrets for the runner (repo or organization secrets):
# AZURE_CLIENT_ID = output ci_identity_client_id of infra/prod (the
# client ID of the id-gspotter-ci managed identity)
# AZURE_TENANT_ID = the Entra tenant ID
# AZURE_SUBSCRIPTION_ID = the subscription ID
# The identity federates to this repo's main branch via git.ntnu.no's
# Actions OIDC issuer, so no client secret exists or is needed.
# - The identity's role grants (Contributor on the resource group,
# AcrPush on the registry) are managed by the Terraform config, not
# by hand.
# - The runner needs the Docker CLI (preinstalled on ubuntu-latest).
#
# Rollback: `az containerapp revision list` + `az containerapp revision
# activate` for the previous revision, or re-run this workflow on an old
# commit (image tags are immutable, per-commit SHAs).
name: CI/CD - Deploy to Azure Container Apps
on:
workflow_dispatch:
permissions:
id-token: write # Azure OIDC login
contents: read
env:
RESOURCE_GROUP: rg-gspotter
BACKEND_APP: gspotter-backend
FRONTEND_APP: gspotter-frontend
MIGRATE_JOB: gspotter-migrate
jobs:
deploy:
runs-on: [self-hosted]
timeout-minutes: 45
# Never overlap production deploys; queue instead of cancelling.
concurrency:
group: production-azure
cancel-in-progress: false
steps:
- name: Check out source
uses: actions/checkout@v4
- name: Log in to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
- name: Resolve Azure resource names
run: |
set -euo pipefail
ACR_NAME="$(az acr list -g "$RESOURCE_GROUP" --query '[0].name' -o tsv)"
if [[ -z "$ACR_NAME" || "$ACR_NAME" == "None" ]]; then
echo "No container registry found in $RESOURCE_GROUP. Run terraform apply in infra/ first." >&2
exit 1
fi
echo "ACR_NAME=$ACR_NAME" >> "$GITHUB_ENV"
echo "ACR_SERVER=$ACR_NAME.azurecr.io" >> "$GITHUB_ENV"
- name: Resolve internal backend URL
run: |
set -euo pipefail
# The backend's ingress is internal-only; this is what the frontend
# proxies /graphql to and what SSR fetches use (BACKEND_URL).
BACKEND_FQDN="$(az containerapp show -n "$BACKEND_APP" -g "$RESOURCE_GROUP" \
--query 'properties.configuration.ingress.fqdn' -o tsv)"
echo "GRAPHQL_PROXY_TARGET=http://$BACKEND_FQDN" >> "$GITHUB_ENV"
- name: Log in to the container registry
run: az acr login --name "$ACR_NAME"
- name: Build and push backend image
run: |
docker buildx build \
--platform linux/amd64 \
--provenance=false \
-f apps/backend/Dockerfile \
-t "$ACR_SERVER/gspotter-backend:$GITHUB_SHA" \
--push .
- name: Build and push frontend image
run: |
docker buildx build \
--platform linux/amd64 \
--provenance=false \
-f apps/frontend/Dockerfile \
--build-arg "APP_BASE_PATH=/" \
--build-arg "VITE_GRAPHQL_URL=/graphql" \
--build-arg "GRAPHQL_PROXY_TARGET=$GRAPHQL_PROXY_TARGET" \
-t "$ACR_SERVER/gspotter-frontend:$GITHUB_SHA" \
--push .
- name: Run database migrations
run: |
set -euo pipefail
# `job start --image` is broken: it creates an execution that drops the
# job's command and env (the container runs the image's default CMD and
# crashes without DATABASE_URL). Update the job's image first, then
# start it with no overrides so the full job template applies.
az containerapp job update -n "$MIGRATE_JOB" -g "$RESOURCE_GROUP" \
--image "$ACR_SERVER/gspotter-backend:$GITHUB_SHA"
az containerapp job start -n "$MIGRATE_JOB" -g "$RESOURCE_GROUP"
# Poll the newest execution until it finishes.
for _ in $(seq 1 60); do
STATUS="$(az containerapp job execution list -n "$MIGRATE_JOB" -g "$RESOURCE_GROUP" \
--query 'sort_by(@, &startTime)[-1].status' -o tsv)"
case "$STATUS" in
Succeeded) echo "Migrations applied."; exit 0 ;;
Failed) echo "Migration job failed. See: az containerapp job execution logs" >&2; exit 1 ;;
*) sleep 10 ;;
esac
done
echo "Timed out waiting for the migration job." >&2
exit 1
# Backend first, so the frontend never points at an older GraphQL schema
# than the one the migrations just applied.
- name: Deploy backend
run: |
az containerapp update -n "$BACKEND_APP" -g "$RESOURCE_GROUP" \
--image "$ACR_SERVER/gspotter-backend:$GITHUB_SHA"
- name: Deploy frontend
run: |
az containerapp update -n "$FRONTEND_APP" -g "$RESOURCE_GROUP" \
--image "$ACR_SERVER/gspotter-frontend:$GITHUB_SHA"