-
Notifications
You must be signed in to change notification settings - Fork 25
315 lines (284 loc) · 10.3 KB
/
build.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
name: Build and Release
on:
push:
tags:
- '*'
workflow_dispatch:
inputs:
publish:
description: 'Create Production Release'
required: true
type: boolean
deploy:
description: 'Deploy to K8S'
default: 'Skip'
required: true
type: choice
options:
- Skip
- Staging Only
- Staging + Prod
sandbox:
description: 'Deploy to Sandbox'
default: false
required: true
type: boolean
sandboxNoDbRefresh:
description: 'Sandbox Disable Daily DB Refresh'
default: false
required: true
type: boolean
jobs:
# -----------------------------------------------------------------
# PREPARE
# -----------------------------------------------------------------
prepare:
name: Prepare Release
runs-on: ubuntu-latest
outputs:
should_deploy: ${{ steps.buildvars.outputs.should_deploy }}
pkg_version: ${{ steps.buildvars.outputs.pkg_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
fetch-tags: false
- name: Get Next Version
if: ${{ github.event.inputs.publish == 'true' }}
id: semver
uses: ietf-tools/semver-action@v1
with:
token: ${{ github.token }}
branch: main
- name: Set Next Version Env Var
if: ${{ github.event.inputs.publish == 'true' }}
run: |
echo "NEXT_VERSION=$nextStrict" >> "$GITHUB_ENV"
- name: Create Draft Release
uses: ncipollo/release-action@v1
if: ${{ github.event.inputs.publish == 'true' }}
with:
prerelease: true
draft: false
commit: ${{ github.sha }}
tag: ${{ env.NEXT_VERSION }}
name: v${{ env.NEXT_VERSION }}
body: '*pending*'
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set Build Variables
id: buildvars
run: |
if [[ $NEXT_VERSION ]]; then
echo "Using AUTO SEMVER mode: $NEXT_VERSION"
echo "should_deploy=true" >> "$GITHUB_OUTPUT"
echo "pkg_version=$NEXT_VERSION" >> "$GITHUB_OUTPUT"
echo "::notice::Release created using branch $GITHUB_REF_NAME"
elif [[ "$GITHUB_REF" =~ ^refs/tags/* ]]; then
echo "Using TAG mode: $GITHUB_REF_NAME"
echo "should_deploy=true" >> "$GITHUB_OUTPUT"
echo "pkg_version=$GITHUB_REF_NAME" >> "$GITHUB_OUTPUT"
echo "::notice::Release created using tag $GITHUB_REF_NAME"
else
echo "Using TEST mode: v2.0.0-dev.$GITHUB_RUN_NUMBER"
echo "should_deploy=false" >> "$GITHUB_OUTPUT"
echo "pkg_version=2.0.0-dev.$GITHUB_RUN_NUMBER" >> "$GITHUB_OUTPUT"
echo "::notice::Non-production build created using branch $GITHUB_REF_NAME"
fi
# -----------------------------------------------------------------
# RELEASE
# -----------------------------------------------------------------
release:
name: Make Release
if: ${{ !failure() && !cancelled() }}
needs: [prepare]
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
env:
SHOULD_DEPLOY: ${{needs.prepare.outputs.should_deploy}}
PKG_VERSION: ${{needs.prepare.outputs.pkg_version}}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
fetch-tags: false
- name: Setup Node.js
uses: actions/[email protected]
with:
node-version: 16.x
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Make Release Build
env:
DEBIAN_FRONTEND: noninteractive
run: |
echo "Building assets..."
cd frontend
npm ci
npm run build
cd ..
echo "Setting version"
echo "PKG_VERSION: $PKG_VERSION"
echo "GITHUB_SHA: $GITHUB_SHA"
echo "GITHUB_REF_NAME: $GITHUB_REF_NAME"
sed -i -r -e "s|^__version__ += '.*'$|__version__ = '$PKG_VERSION'|" backend/mlarchive/__init__.py
sed -i -r -e "s|^__release_hash__ += '.*'$|__release_hash__ = '$GITHUB_SHA'|" backend/mlarchive/__init__.py
sed -i -r -e "s|^__release_branch__ += '.*'$|__release_branch__ = '$GITHUB_REF_NAME'|" backend/mlarchive/__init__.py
echo "Build release tarball..."
mkdir -p /home/runner/work/release
tar -czf /home/runner/work/release/release.tar.gz -X dev/deploy/exclude-patterns.txt .
- name: Collect + Push Statics
env:
DEBIAN_FRONTEND: noninteractive
AWS_ACCESS_KEY_ID: ${{ secrets.CF_R2_STATIC_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_R2_STATIC_KEY_SECRET }}
AWS_DEFAULT_REGION: auto
AWS_ENDPOINT_URL: ${{ secrets.CF_R2_ENDPOINT }}
run: |
echo "Collecting statics..."
docker run --rm --name collectstatics -v $(pwd):/workspace ghcr.io/ietf-tools/mailarchive-app-base:latest sh build/app/collectstatics.sh
echo "Pushing statics..."
cd static
aws s3 sync . s3://static/mailarchive/$PKG_VERSION --only-show-errors
- name: Augment dockerignore for docker image build
env:
DEBIAN_FRONTEND: noninteractive
run: |
cat >> .dockerignore <<EOL
.devcontainer
.github
.vscode
helm
charts
playwright
svn-history
docker-compose.yml
EOL
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build Release Docker Image
uses: docker/build-push-action@v5
with:
context: .
file: build/app/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/ietf-tools/mailarchive:${{ env.PKG_VERSION }}
- name: Update CHANGELOG
id: changelog
uses: Requarks/changelog-action@v1
if: ${{ env.SHOULD_DEPLOY == 'true' }}
with:
token: ${{ github.token }}
tag: ${{ env.PKG_VERSION }}
writeToFile: false
- name: Create Release
uses: ncipollo/[email protected]
if: ${{ env.SHOULD_DEPLOY == 'true' }}
with:
allowUpdates: true
draft: false
tag: ${{ env.PKG_VERSION }}
name: v${{ env.PKG_VERSION }}
body: ${{ steps.changelog.outputs.changes }}
artifacts: "/home/runner/work/release/release.tar.gz"
token: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
name: release-${{ env.PKG_VERSION }}
path: /home/runner/work/release/release.tar.gz
# -----------------------------------------------------------------
# STAGING
# -----------------------------------------------------------------
staging:
name: Deploy to Staging
if: ${{ !failure() && !cancelled() && (github.event.inputs.deploy == 'Staging Only' || github.event.inputs.deploy == 'Staging + Prod') }}
needs: [prepare, release]
runs-on: ubuntu-latest
environment:
name: staging
env:
PKG_VERSION: ${{needs.prepare.outputs.pkg_version}}
steps:
- name: Deploy to staging
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: deploy.yml
repo: ietf-tools/infra-k8s
ref: main
token: ${{ secrets.GH_INFRA_K8S_TOKEN }}
inputs: '{ "environment":"${{ secrets.GHA_K8S_CLUSTER }}", "app":"mailarchive", "appVersion":"${{ env.PKG_VERSION }}", "remoteRef":"${{ github.sha }}" }'
wait-for-completion: true
wait-for-completion-timeout: 10m
display-workflow-run-url: false
# -----------------------------------------------------------------
# PROD
# -----------------------------------------------------------------
prod:
name: Deploy to Production
if: ${{ !failure() && !cancelled() && github.event.inputs.deploy == 'Staging + Prod' }}
needs: [staging]
runs-on: ubuntu-latest
environment:
name: production
env:
PKG_VERSION: ${{needs.prepare.outputs.pkg_version}}
steps:
- name: Deploy to production
uses: the-actions-org/workflow-dispatch@v4
with:
workflow: deploy.yml
repo: ietf-tools/infra-k8s
ref: main
token: ${{ secrets.GH_INFRA_K8S_TOKEN }}
inputs: '{ "environment":"${{ secrets.GHA_K8S_CLUSTER }}", "app":"mailarchive", "appVersion":"${{ env.PKG_VERSION }}", "remoteRef":"${{ github.sha }}" }'
wait-for-completion: true
wait-for-completion-timeout: 10m
display-workflow-run-url: false
# -----------------------------------------------------------------
# SANDBOX
# -----------------------------------------------------------------
sandbox:
name: Deploy to Sandbox
if: ${{ !failure() && !cancelled() && github.event.inputs.sandbox == 'true' }}
needs: [prepare, release]
runs-on: [self-hosted, dev-server]
environment:
name: sandbox
env:
PKG_VERSION: ${{needs.prepare.outputs.pkg_version}}
steps:
- uses: actions/checkout@v4
- name: Download a Release Artifact
uses: actions/[email protected]
with:
name: release-${{ env.PKG_VERSION }}
- name: Deploy to containers
env:
DEBIAN_FRONTEND: noninteractive
run: |
# echo "Reset production flags in settings.py..."
# sed -i -r -e 's/^DEBUG *= *.*$/DEBUG = True/' -e "s/^SERVER_MODE *= *.*\$/SERVER_MODE = 'development'/" backend/mlarchive/settings/base.py
echo "Install Deploy to Container CLI dependencies..."
cd dev/deploy-to-container
npm ci
cd ../..
echo "Start Deploy..."
node ./dev/deploy-to-container/cli.js --branch ${{ github.ref_name }} --domain dev.ietf.org --appversion ${{ env.PKG_VERSION }} --commit ${{ github.sha }} --ghrunid ${{ github.run_id }} --nodbrefresh ${{ github.event.inputs.sandboxNoDbRefresh }}
- name: Cleanup old docker resources
env:
DEBIAN_FRONTEND: noninteractive
run: |
docker image prune -a -f