Skip to content

Commit a486078

Browse files
authored
Create ci-cd.yml
1 parent b649af3 commit a486078

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: CI/CD - FastAPI to Cloud Run
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
ci:
10+
name: Build & Test
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: "3.10"
20+
21+
- name: Cache pip
22+
uses: actions/cache@v4
23+
with:
24+
path: ~/.cache/pip
25+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements*.txt') }}
26+
27+
- name: Install deps
28+
run: |
29+
python -m pip install --upgrade pip
30+
pip install -r requirements.txt
31+
# optional: test deps
32+
pip install pytest pytest-cov
33+
34+
- name: Lint (optional)
35+
run: |
36+
pip install ruff
37+
ruff check .
38+
39+
- name: Run tests
40+
run: pytest -q
41+
42+
cd:
43+
name: Docker Build, Push & Deploy
44+
needs: ci
45+
runs-on: ubuntu-latest
46+
if: github.ref == 'refs/heads/main'
47+
env:
48+
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
49+
REGION: ${{ secrets.GCP_REGION }} # e.g. asia-southeast2
50+
SERVICE_NAME: fastapi-tiktok-dashboard # bebas
51+
IMAGE: asia.gcr.io/${{ secrets.GCP_PROJECT_ID }}/fastapi-tiktok-dashboard:${{ github.sha }}
52+
steps:
53+
- name: Checkout
54+
uses: actions/checkout@v4
55+
56+
- name: Set up gcloud
57+
uses: google-github-actions/setup-gcloud@v2
58+
with:
59+
project_id: ${{ secrets.GCP_PROJECT_ID }}
60+
service_account_key: ${{ secrets.GCP_SA_KEY }} # JSON SA key
61+
export_default_credentials: true
62+
63+
- name: Configure Docker auth
64+
run: gcloud auth configure-docker --quiet
65+
66+
- name: Build Docker image
67+
run: docker build -t "$IMAGE" -f Dockerfile .
68+
69+
- name: Push image
70+
run: docker push "$IMAGE"
71+
72+
- name: Deploy to Cloud Run
73+
run: |
74+
gcloud run deploy $SERVICE_NAME \
75+
--image "$IMAGE" \
76+
--region "$REGION" \
77+
--platform managed \
78+
--allow-unauthenticated \
79+
--port 8001 \
80+
--set-env-vars POSTGRES_HOST=${{ secrets.POSTGRES_HOST }},POSTGRES_DB=${{ secrets.POSTGRES_DB }},POSTGRES_USER=${{ secrets.POSTGRES_USER }},POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }},GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}

0 commit comments

Comments
 (0)