1+ name : Deploy to Google Cloud Run
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ pull_request :
8+ branches :
9+ - main
10+ types : [closed] # Deploy when PR is merged (which is effectively a push, but checking explicitly if needed. Usually just push to main is enough, let's stick to standard push to main)
11+
12+ env :
13+ PROJECT_ID : ${{ secrets.GCP_PROJECT_ID }}
14+ REGION : ${{ secrets.GCP_REGION }}
15+ WIF_PROVIDER : ${{ secrets.WIF_PROVIDER }}
16+ WIF_SERVICE_ACCOUNT : ${{ secrets.WIF_SERVICE_ACCOUNT }}
17+ ARTIFACT_REGISTRY : ${{ secrets.ARTIFACT_REGISTRY }}
18+ SERVICE_NAME : skillcert-backend
19+
20+ jobs :
21+ build-and-deploy :
22+ # Only run on push to main or PR merged to main
23+ if : github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true)
24+ runs-on : ubuntu-latest
25+
26+ permissions :
27+ contents : " read"
28+ id-token : " write" # Required for Workload Identity Federation
29+
30+ steps :
31+ - name : Checkout repository
32+ uses : actions/checkout@v4
33+
34+ - name : Authenticate to Google Cloud
35+ id : auth
36+ uses : google-github-actions/auth@v2
37+ with :
38+ workload_identity_provider : ${{ env.WIF_PROVIDER }}
39+ service_account : ${{ env.WIF_SERVICE_ACCOUNT }}
40+
41+ - name : Set up Cloud SDK
42+ uses : google-github-actions/setup-gcloud@v2
43+
44+ - name : Configure Docker to use Artifact Registry
45+ run : |
46+ gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev --quiet
47+ - name : Build and Push Docker image
48+ id : docker-build-push
49+ run : |
50+ IMAGE_TAG=${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.ARTIFACT_REGISTRY }}/${{ env.SERVICE_NAME }}:${{ github.sha }}
51+ docker build -t $IMAGE_TAG .
52+ docker push $IMAGE_TAG
53+ echo "image=$IMAGE_TAG" >> $GITHUB_OUTPUT
54+ - name : Deploy to Cloud Run
55+ id : deploy
56+ uses : google-github-actions/deploy-cloudrun@v2
57+ with :
58+ service : ${{ env.SERVICE_NAME }}
59+ region : ${{ env.REGION }}
60+ image : ${{ steps.docker-build-push.outputs.image }}
61+ # The container port is 3000 according to Dockerfile
62+ port : 3000
63+ # Allow unauthenticated access (adjust if the backend should be private)
64+ flags : " --allow-unauthenticated"
65+
66+ - name : Show Cloud Run URL
67+ run : |
68+ echo "Deployed successfully to: ${{ steps.deploy.outputs.url }}"
0 commit comments