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+ - name : Run tests
69+ working-directory : apps/backend
70+ run : npm test
71+
72+ - name : Build and push Docker image
73+ run : |
74+ docker build \
75+ -f docker/backend.Dockerfile \
76+ -t asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }} \
77+ .
78+ docker push asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }}
79+
80+ - name : Get GKE credentials
81+ uses : google-github-actions/get-gke-credentials@v2
82+ with :
83+ cluster_name : devcard-cluster
84+ location : asia-south1
85+
86+ - name : Run Prisma migrations
87+ run : |
88+ cat <<EOF | kubectl apply -f -
89+ apiVersion: batch/v1
90+ kind: Job
91+ metadata:
92+ name: prisma-migrate-${{ steps.tag.outputs.sha }}
93+ namespace: uat
94+ spec:
95+ ttlSecondsAfterFinished: 300
96+ template:
97+ spec:
98+ restartPolicy: Never
99+ containers:
100+ - name: migrate
101+ image: asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }}
102+ command: ["npx", "prisma", "migrate", "deploy"]
103+ env:
104+ - name: DATABASE_URL
105+ valueFrom:
106+ secretKeyRef:
107+ name: devcard-secret
108+ key: database-url
109+ EOF
110+ kubectl wait --for=condition=complete \
111+ job/prisma-migrate-${{ steps.tag.outputs.sha }} \
112+ -n uat --timeout=120s
113+
114+ - name : Update image tag in kustomize
115+ run : |
116+ cd infra/k8s/overlays/uat
117+ kustomize edit set image IMAGE_TAG_PLACEHOLDER=asia-south1-docker.pkg.dev/devcard-prod/devcard/backend:${{ steps.tag.outputs.sha }}
118+
119+ - name : Commit and push image tag to infra repo
120+ run : |
121+ cd infra
122+ git config user.name "github-actions[bot]"
123+ git config user.email "github-actions[bot]@users.noreply.github.com"
124+ git add k8s/overlays/uat/kustomization.yaml
125+ git commit -m "chore: update uat backend image to ${{ steps.tag.outputs.sha }}"
126+ git push
127+
128+ - name : Deploy to UAT
129+ run : kubectl apply -k infra/k8s/overlays/uat
130+
131+ - name : Wait for rollout
132+ run : |
133+ kubectl rollout status deployment/backend \
134+ -n uat --timeout=5m
0 commit comments