Skip to content

Commit abc25e2

Browse files
authored
Merge pull request #219 from gabito1451/feature/cloud-run-CI/CD
feat: ci: add GitHub Actions workflows for Cloud Run deployments
2 parents 780e8a4 + 40b4d45 commit abc25e2

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

.github/workflows/deploy.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 }}"

frontend-deploy.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Deploy Frontend to Google Cloud Run
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
types: [closed]
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-frontend
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+
# Pass the backend URL as a build arg if necessary
52+
docker build \
53+
--build-arg NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }} \
54+
-t $IMAGE_TAG .
55+
56+
docker push $IMAGE_TAG
57+
echo "image=$IMAGE_TAG" >> $GITHUB_OUTPUT
58+
- name: Deploy to Cloud Run
59+
id: deploy
60+
uses: google-github-actions/deploy-cloudrun@v2
61+
with:
62+
service: ${{ env.SERVICE_NAME }}
63+
region: ${{ env.REGION }}
64+
image: ${{ steps.docker-build-push.outputs.image }}
65+
# Port usually exposed by frontend containers (e.g., Next.js uses 3000)
66+
port: 3000
67+
flags: "--allow-unauthenticated"
68+
env_vars: |
69+
NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }}
70+
- name: Show Cloud Run URL
71+
run: |
72+
echo "Deployed successfully to: ${{ steps.deploy.outputs.url }}"

0 commit comments

Comments
 (0)