Skip to content

CI/CD - Deploy to Azure Container Apps #8

CI/CD - Deploy to Azure Container Apps

CI/CD - Deploy to Azure Container Apps #8

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 self-hosted runner needs Docker Engine and the buildx plugin
# (`docker buildx version` must work). The workflow checks this before
# logging in to the registry. Use Docker 28.x: 29.x has an always-on
# BuildKit session bug that can hang builds with "only one connection
# allowed".
#
# 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: Check Docker prerequisites
run: |
set -euo pipefail
command -v docker > /dev/null || {
echo "::error::docker is not installed on the runner"
exit 1
}
docker info > /dev/null || {
echo "::error::the Docker daemon is not reachable by the runner user"
exit 1
}
docker buildx version || {
echo "::error::the Docker buildx plugin is not installed on the runner"
exit 1
}
- 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: |
set -euo pipefail
# `az acr login` shells out to the Docker CLI and fails with
# DOCKER_COMMAND_ERROR if it cannot find it. Ask ACR for a token
# instead and hand it to Docker directly; buildx still needs the
# registry login for `--push`.
ACR_TOKEN="$(az acr login --name "$ACR_NAME" --expose-token \
--query accessToken -o tsv)"
printf '%s' "$ACR_TOKEN" | docker login "$ACR_SERVER" \
--username 00000000-0000-0000-0000-000000000000 \
--password-stdin
- 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"
EXECUTION="$(az containerapp job start -n "$MIGRATE_JOB" -g "$RESOURCE_GROUP" \
--query name -o tsv)"
echo "Migration execution: $EXECUTION"
# Poll that exact execution until it finishes. (Do not use
# `job execution list` + sort_by(&startTime): those fields are nested
# under `properties`, so a top-level sort key is null and JMESPath
# rejects it.)
for _ in $(seq 1 60); do
STATUS="$(az containerapp job execution show -n "$MIGRATE_JOB" -g "$RESOURCE_GROUP" \
--job-execution-name "$EXECUTION" \
--query properties.status -o tsv 2>/dev/null || true)"
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"
# The runner shares a small disk with the production VM. Images have
# already been pushed to ACR, so drop the local copies and keep only a
# small build cache for the next deploy.
- name: Clean up Docker build cache
if: always()
run: |
docker image prune -af || true
docker builder prune -f --max-used-space 1GB || true