Skip to content

Commit c1e5ec0

Browse files
authored
Merge pull request #677 from rajbos/rajbos/azure-aca-terraform-deploy
feat: Azure Container Apps deployment for sharing server (Terraform + workflows)
2 parents 2bc68ed + bc8c3ca commit c1e5ec0

9 files changed

Lines changed: 910 additions & 10 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Cleanup Sharing Server (branch deleted)
2+
3+
# Destroys the test Container Apps environment when a branch is deleted.
4+
# Production (main) is intentionally excluded.
5+
on:
6+
delete:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
cleanup:
13+
name: Destroy test environment
14+
runs-on: ubuntu-latest
15+
# Only run for branch deletions; skip tag deletions and the main branch.
16+
if: github.event.ref_type == 'branch' && github.event.ref != 'main'
17+
environment: testing
18+
permissions:
19+
contents: read
20+
env:
21+
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
22+
ARM_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
23+
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
24+
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
25+
steps:
26+
- name: Harden the runner (Audit all outbound calls)
27+
uses: step-security/harden-runner@8d3c67de8e2fe68ef647c8db1e6a09f647780f40 # v2.19.0
28+
with:
29+
egress-policy: audit
30+
31+
- name: Checkout code
32+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
33+
34+
# Reproduce the exact same slug+hash logic used by the deploy workflow
35+
# so the state key resolves to the same tfstate file.
36+
- name: Compute state key for deleted branch
37+
id: env
38+
run: |
39+
# NOTE: on the `delete` event, github.event.ref is the deleted branch name.
40+
BRANCH="${{ github.event.ref }}"
41+
SLUG=$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-//;s/-$//')
42+
SLUG_TRUNC=$(echo "$SLUG" | cut -c1-13 | sed 's/-$//')
43+
HASH=$(echo -n "$BRANCH" | sha256sum | cut -c1-6)
44+
APP_NAME="sharing-test-${SLUG_TRUNC}${HASH}"
45+
echo "app_name=${APP_NAME}" >> "$GITHUB_OUTPUT"
46+
echo "state_key=sharing-server/test-${SLUG_TRUNC}${HASH}.tfstate" >> "$GITHUB_OUTPUT"
47+
48+
- name: Setup Terraform
49+
uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3
50+
51+
- name: Terraform init
52+
id: init
53+
working-directory: sharing-server/infra
54+
run: |
55+
terraform init -reconfigure \
56+
-backend-config="resource_group_name=${{ vars.TF_STATE_RESOURCE_GROUP }}" \
57+
-backend-config="storage_account_name=${{ vars.TF_STATE_STORAGE_ACCOUNT }}" \
58+
-backend-config="container_name=${{ vars.TF_STATE_CONTAINER }}" \
59+
-backend-config="key=${{ steps.env.outputs.state_key }}"
60+
61+
- name: Destroy resources (if any exist)
62+
working-directory: sharing-server/infra
63+
env:
64+
# Variables are required by Terraform even for destroy; use safe placeholders
65+
# for values that only affect resource creation (image, secrets).
66+
TF_VAR_resource_group_name: ${{ vars.AZURE_RESOURCE_GROUP }}
67+
TF_VAR_location: ${{ vars.AZURE_LOCATION || 'westeurope' }}
68+
TF_VAR_app_name: ${{ steps.env.outputs.app_name }}
69+
TF_VAR_container_image: "ghcr.io/${{ github.repository_owner }}/copilot-sharing-server:latest"
70+
TF_VAR_github_client_id: ${{ secrets.SHARING_GITHUB_CLIENT_ID }}
71+
TF_VAR_github_client_secret: ${{ secrets.SHARING_GITHUB_CLIENT_SECRET }}
72+
TF_VAR_session_secret: ${{ secrets.SHARING_SESSION_SECRET }}
73+
run: |
74+
RESOURCE_COUNT=$(terraform state list 2>/dev/null | wc -l)
75+
if [ "$RESOURCE_COUNT" -gt 0 ]; then
76+
echo "Found ${RESOURCE_COUNT} resource(s) in state — running destroy..."
77+
terraform destroy -auto-approve
78+
echo "✅ Environment destroyed: ${{ steps.env.outputs.app_name }}" >> "$GITHUB_STEP_SUMMARY"
79+
else
80+
echo "No resources found in state for branch '${{ github.event.ref }}' — nothing to destroy."
81+
echo "ℹ️ No resources found for branch \`${{ github.event.ref }}\` — nothing to destroy." >> "$GITHUB_STEP_SUMMARY"
82+
fi

0 commit comments

Comments
 (0)