-
Notifications
You must be signed in to change notification settings - Fork 13
289 lines (264 loc) · 10.8 KB
/
deploy.yml
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
name: Build & Deploy
on:
push:
branches:
- main
workflow_dispatch:
inputs:
force:
description: "Force deploy to all services"
default: true
type: boolean
nocache:
description: "Do not rely on existing layer cache"
default: false
type: boolean
env:
REGION: us-central1
PROJECT_ID: cask-382601
GOOGLE_CLIENT_ID: "721909483682-uk3befic1j1krv3drig2puu30v1i4v48.apps.googleusercontent.com"
SENTRY_DSN: "https://[email protected]/4505138086019073"
SENTRY_ORG: "peated"
SENTRY_PROJECT: "peated"
API_SERVER: https://api.peated.com
URL_PREFIX: https://peated.com
FATHOM_SITE_ID: "OGNPFEUC"
jobs:
build:
name: 🚀 Build
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
runs-on: ubuntu-latest
permissions:
contents: "read"
id-token: "write"
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3
- name: "Google auth"
id: "auth"
uses: "google-github-actions/auth@v1"
with:
token_format: access_token
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER_RESOURCE_NAME }}
service_account: ${{ secrets.SERVICE_ACCOUNT }}@${{ env.PROJECT_ID }}.iam.gserviceaccount.com
access_token_lifetime: 300s
# - name: Login to Artifact Registry
# uses: docker/login-action@v1
# with:
# registry: us-central-docker.pkg.dev
# username: oauth2accesstoken
# password: ${{ steps.auth.outputs.access_token }}
- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v1"
with:
project_id: "${{ env.PROJECT_ID }}"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: "Docker auth"
run: |-
gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev
- name: Record Version
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Verify Web
id: verify_web
run: |
if gcloud artifacts docker images describe "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/web:${{ github.sha }}"; then
echo "image_exists=true" >> "$GITHUB_OUTPUT"
else
echo "image_exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Verify API
id: verify_api
run: |
if gcloud artifacts docker images describe "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/api:${{ github.sha }}"; then
echo "image_exists=true" >> "$GITHUB_OUTPUT"
else
echo "image_exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Verify Worker
id: verify_worker
run: |
if gcloud artifacts docker images describe "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/worker:${{ github.sha }}"; then
echo "image_exists=true" >> "$GITHUB_OUTPUT"
else
echo "image_exists=false" >> "$GITHUB_OUTPUT"
fi
# optimize cache by building all stages
- name: Build All Stages
uses: docker/build-push-action@v5
if: steps.verify_web.outputs.image_exists == 'false' || steps.verify_api.outputs.image_exists == 'false' || steps.verify_worker.outputs.image_exists == 'false'
with:
context: .
cache-from: type=gha,scope=prod
cache-to: type=gha,mode=max,scope=prod
no-cache: ${{ inputs.nocache == 'true' }}
build-args: |
VERSION=${{ github.sha }}
API_SERVER=${{ env.API_SERVER }}
URL_PREFIX=${{ env.URL_PREFIX }}
GOOGLE_CLIENT_ID=${{ env.GOOGLE_CLIENT_ID }}
SENTRY_DSN=${{ env.SENTRY_DSN }}
SENTRY_ORG=${{ env.SENTRY_ORG }}
SENTRY_PROJECT=${{ env.SENTRY_PROJECT }}
FATHOM_SITE_ID=${{ env.FATHOM_SITE_ID }}
secrets: |
"SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}"
- name: Build Web
uses: docker/build-push-action@v5
if: steps.verify_web.outputs.image_exists == 'false'
with:
context: .
cache-from: type=gha,scope=prod
no-cache: ${{ inputs.nocache == 'true' }}
push: true
target: web
build-args: |
VERSION=${{ github.sha }}
API_SERVER=${{ env.API_SERVER }}
URL_PREFIX=${{ env.URL_PREFIX }}
GOOGLE_CLIENT_ID=${{ env.GOOGLE_CLIENT_ID }}
SENTRY_DSN=${{ env.SENTRY_DSN }}
SENTRY_ORG=${{ env.SENTRY_ORG }}
SENTRY_PROJECT=${{ env.SENTRY_PROJECT }}
FATHOM_SITE_ID=${{ env.FATHOM_SITE_ID }}
secrets: |
"SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}"
tags: |
${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/web:${{ github.sha }}
- name: Build API
uses: docker/build-push-action@v5
if: steps.verify_api.outputs.image_exists == 'false'
with:
context: .
cache-from: type=gha,scope=prod
no-cache: ${{ inputs.nocache == 'true' }}
push: true
target: api
build-args: |
VERSION=${{ github.sha }}
API_SERVER=${{ env.API_SERVER }}
URL_PREFIX=${{ env.URL_PREFIX }}
GOOGLE_CLIENT_ID=${{ env.GOOGLE_CLIENT_ID }}
SENTRY_DSN=${{ env.SENTRY_DSN }}
SENTRY_ORG=${{ env.SENTRY_ORG }}
SENTRY_PROJECT=${{ env.SENTRY_PROJECT }}
FATHOM_SITE_ID=${{ env.FATHOM_SITE_ID }}
secrets: |
"SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}"
tags: |
${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/api:${{ github.sha }}
- name: Build Worker
uses: docker/build-push-action@v5
if: steps.verify_worker.outputs.image_exists == 'false'
with:
context: .
cache-from: type=gha,scope=prod
no-cache: ${{ inputs.nocache == 'true' }}
push: true
target: worker
build-args: |
VERSION=${{ github.sha }}
API_SERVER={{ env.API_SERVER }}
URL_PREFIX=${{ env.URL_PREFIX }}
GOOGLE_CLIENT_ID=${{ env.GOOGLE_CLIENT_ID }}
SENTRY_DSN=${{ env.SENTRY_DSN }}
SENTRY_ORG=${{ env.SENTRY_ORG }}
SENTRY_PROJECT=${{ env.SENTRY_PROJECT }}
FATHOM_SITE_ID=${{ env.FATHOM_SITE_ID }}
secrets: |
"SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}"
tags: |
${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/worker:${{ github.sha }}
deploy:
name: 🚀 Deploy
concurrency:
group: ${{ github.workflow }}
runs-on: ubuntu-latest
needs: [build]
permissions:
contents: "read"
id-token: "write"
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3
- name: "Google auth"
id: "auth"
uses: "google-github-actions/auth@v1"
with:
token_format: access_token
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER_RESOURCE_NAME }}
service_account: ${{ secrets.SERVICE_ACCOUNT }}@${{ env.PROJECT_ID }}.iam.gserviceaccount.com
access_token_lifetime: 300s
- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v1"
with:
project_id: "${{ env.PROJECT_ID }}"
- name: Get GKE credentials
uses: google-github-actions/get-gke-credentials@v1
with:
cluster_name: default
location: ${{ env.REGION }}
- name: Record Version
id: vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: Get changed files
id: changed-files-yaml
uses: tj-actions/changed-files@v39
with:
files_yaml: |
api:
- apps/server/**
web:
- apps/web/**
worker:
- apps/worker/**
common:
- packages/**
- apps/server**
- Dockerfile
- package.json
migrations:
- apps/**/migrations/*.sql
- packages/**/migrations/*.sql
- name: Run Migrations
if: ${{ inputs.force || steps.changed-files-yaml.outputs.migrations_any_changed == 'true' }}
run: |-
gcloud run jobs update migrate-database \
--region ${{ env.REGION }} \
--image "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/api:${{ github.sha }}"
gcloud run jobs execute migrate-database \
--region ${{ env.REGION }} \
--wait
- name: "Deploy API"
if: ${{ inputs.force || steps.changed-files-yaml.outputs.api_any_changed == 'true' || steps.changed-files-yaml.outputs.common_any_changed == 'true' }}
run: |-
kubectl set image deployment.v1.apps/peated-api peated-api="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/api:${{ github.sha }}"
kubectl rollout status deployment/peated-api --timeout=5m
- name: "Deploy Web"
if: ${{ inputs.force || steps.changed-files-yaml.outputs.web_any_changed == 'true' || steps.changed-files-yaml.outputs.common_any_changed == 'true' }}
run: |-
kubectl set image deployment.v1.apps/peated-web peated-web="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/web:${{ github.sha }}"
kubectl rollout status deployment/peated-web --timeout=5m
- name: "Deploy Worker"
if: ${{ inputs.force || steps.changed-files-yaml.outputs.worker_any_changed == 'true' || steps.changed-files-yaml.outputs.common_any_changed == 'true' }}
run: |-
kubectl set image deployment.v1.apps/peated-worker peated-worker="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/peated/worker:${{ github.sha }}"
kubectl rollout status deployment/peated-worker --timeout=5m
- name: Discord notification (success)
if: success()
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/[email protected]
with:
args: |
"${{ github.actor }} deployed version [${{ steps.vars.outputs.sha_short }}](https://github.com/{{ EVENT_PAYLOAD.repository.full_name }}/commit/${{ github.sha }})."
- name: Discord notification (failure)
if: failure()
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/[email protected]
with:
args: |
"ERROR: Failed to deploy version [${{ steps.vars.outputs.sha_short }}](https://github.com/{{ EVENT_PAYLOAD.repository.full_name }}/commit/${{ github.sha }})."