-
Notifications
You must be signed in to change notification settings - Fork 0
128 lines (109 loc) · 4.32 KB
/
Copy pathdeploy.yml
File metadata and controls
128 lines (109 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
name: Deploy to GCP
on:
push:
branches: [main]
pull_request:
branches: [main]
env:
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
GCP_REGION: asia-southeast2
REGISTRY: asia-southeast2-docker.pkg.dev
jobs:
# ─────────────────────────────────────────
# Terraform Plan (PR) / Apply (main)
# ─────────────────────────────────────────
terraform:
name: Terraform
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # Untuk Workload Identity Federation
steps:
- uses: actions/checkout@v4
- name: Auth to GCP
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
service_account: ${{ secrets.WIF_SERVICE_ACCOUNT }}
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.7.0"
- name: Terraform Init
working-directory: terraform
run: terraform init
- name: Terraform Format Check
working-directory: terraform
run: terraform fmt -check
- name: Terraform Validate
working-directory: terraform
run: terraform validate
- name: Terraform Plan
working-directory: terraform
run: |
terraform plan \
-var="project_id=${{ env.GCP_PROJECT_ID }}" \
-var="region=${{ env.GCP_REGION }}"
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
working-directory: terraform
run: |
terraform apply -auto-approve \
-var="project_id=${{ env.GCP_PROJECT_ID }}" \
-var="region=${{ env.GCP_REGION }}"
# ─────────────────────────────────────────
# Build & Push Docker Images
# ─────────────────────────────────────────
build:
name: Build & Push Images
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
permissions:
contents: read
id-token: write
strategy:
matrix:
service: [api-gateway, worker, scheduler]
steps:
- uses: actions/checkout@v4
- name: Auth to GCP
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
service_account: ${{ secrets.WIF_SERVICE_ACCOUNT }}
- name: Configure Docker for Artifact Registry
run: gcloud auth configure-docker ${{ env.REGISTRY }} --quiet
- name: Build & Push ${{ matrix.service }}
run: |
IMAGE="${{ env.REGISTRY }}/${{ env.GCP_PROJECT_ID }}/app-repo/${{ matrix.service }}:${{ github.sha }}"
docker build -t $IMAGE services/${{ matrix.service }}
docker push $IMAGE
# Tag as latest
docker tag $IMAGE "${{ env.REGISTRY }}/${{ env.GCP_PROJECT_ID }}/app-repo/${{ matrix.service }}:latest"
docker push "${{ env.REGISTRY }}/${{ env.GCP_PROJECT_ID }}/app-repo/${{ matrix.service }}:latest"
# ─────────────────────────────────────────
# Deploy ke Cloud Run
# ─────────────────────────────────────────
deploy:
name: Deploy to Cloud Run
runs-on: ubuntu-latest
needs: [terraform, build]
if: github.ref == 'refs/heads/main'
permissions:
contents: read
id-token: write
strategy:
matrix:
service: [api-gateway, worker, scheduler]
steps:
- name: Auth to GCP
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.WIF_PROVIDER }}
service_account: ${{ secrets.WIF_SERVICE_ACCOUNT }}
- name: Deploy ${{ matrix.service }} to Cloud Run
uses: google-github-actions/deploy-cloudrun@v2
with:
service: ${{ matrix.service }}
region: ${{ env.GCP_REGION }}
image: ${{ env.REGISTRY }}/${{ env.GCP_PROJECT_ID }}/app-repo/${{ matrix.service }}:${{ github.sha }}