1+ name : UAT Deploy
2+
3+ on :
4+ push :
5+ branches : [main]
6+
7+ jobs :
8+ detect-changes :
9+ runs-on : ubuntu-latest
10+ outputs :
11+ backend : ${{ steps.changes.outputs.backend }}
12+ steps :
13+ - uses : actions/checkout@v4
14+
15+ - uses : dorny/paths-filter@v3
16+ id : changes
17+ with :
18+ filters : |
19+ backend:
20+ - 'apps/backend/**'
21+
22+ backend-deploy :
23+ needs : detect-changes
24+ if : needs.detect-changes.outputs.backend == 'true'
25+ runs-on : ubuntu-latest
26+ permissions :
27+ contents : read
28+ id-token : write
29+
30+ steps :
31+ - name : Checkout app repo
32+ uses : actions/checkout@v4
33+
34+ - name : Checkout infra repo
35+ uses : actions/checkout@v4
36+ with :
37+ repository : Dev-Card/devcard-infra
38+ path : infra
39+ token : ${{ secrets.INFRA_REPO_TOKEN }}
40+
41+ - name : Authenticate to GCP
42+ uses : google-github-actions/auth@v2
43+ with :
44+ workload_identity_provider : ${{ secrets.WIF_PROVIDER }}
45+ service_account : ${{ secrets.WIF_SERVICE_ACCOUNT }}
46+
47+ - name : Setup gcloud
48+ uses : google-github-actions/setup-gcloud@v2
49+
50+ - name : Configure Docker for Artifact Registry
51+ run : gcloud auth configure-docker asia-south1-docker.pkg.dev
52+
53+ - name : Set image tag
54+ id : tag
55+ run : echo "sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
56+
57+ - name : Setup Node.js
58+ uses : actions/setup-node@v4
59+ with :
60+ node-version : 20
61+ cache : npm
62+ cache-dependency-path : apps/backend/package-lock.json
63+
64+ - name : Install dependencies
65+ working-directory : apps/backend
66+ run : npm ci
67+
68+ # TODO: Once tests are fixed, uncomment the following lines
69+ # - name: Run tests
70+ # working-directory: apps/backend
71+ # run: npm test
72+
73+ - name : Build and push Docker image
74+ run : |
75+ docker build \
76+ -f docker/backend.Dockerfile \
77+ -t asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }} \
78+ .
79+ docker push asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }}
80+
81+ - name : Get GKE credentials
82+ uses : google-github-actions/get-gke-credentials@v2
83+ with :
84+ cluster_name : devcard-cluster
85+ location : asia-south1
86+
87+ - name : Run Prisma migrations
88+ run : |
89+ cat <<EOF | kubectl apply -f -
90+ apiVersion: batch/v1
91+ kind: Job
92+ metadata:
93+ name: prisma-migrate-${{ steps.tag.outputs.sha }}
94+ namespace: uat
95+ spec:
96+ ttlSecondsAfterFinished: 300
97+ template:
98+ spec:
99+ restartPolicy: Never
100+ containers:
101+ - name: migrate
102+ image: asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }}
103+ command: ["npx", "prisma", "migrate", "deploy"]
104+ env:
105+ - name: DATABASE_URL
106+ valueFrom:
107+ secretKeyRef:
108+ name: devcard-secret
109+ key: database-url
110+ EOF
111+ kubectl wait --for=condition=complete \
112+ job/prisma-migrate-${{ steps.tag.outputs.sha }} \
113+ -n uat --timeout=120s
114+
115+ - name : Update image tag in kustomize
116+ run : |
117+ cd infra/k8s/overlays/uat
118+ kustomize edit set image IMAGE_TAG_PLACEHOLDER=asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }}
119+
120+ - name : Commit and push image tag to infra repo
121+ run : |
122+ cd infra
123+ git config user.name "github-actions[bot]"
124+ git config user.email "github-actions[bot]@users.noreply.github.com"
125+ git add k8s/overlays/uat/kustomization.yaml
126+ git commit -m "chore: update uat backend image to ${{ steps.tag.outputs.sha }}"
127+ git push
128+
129+ - name : Deploy to UAT
130+ run : kubectl apply -k infra/k8s/overlays/uat
131+
132+ - name : Wait for rollout
133+ run : |
134+ kubectl rollout status deployment/backend \
135+ -n uat --timeout=5m
0 commit comments