-
Notifications
You must be signed in to change notification settings - Fork 0
422 lines (376 loc) · 15.6 KB
/
reusable-docker.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
name: Docker
on:
workflow_call:
inputs:
registry:
description: Docker registry
type: string
default: ghcr.io
platforms:
description: Docker platforms
type: string
default: linux/amd64,linux/arm64
targets:
description: Docker targets
required: true
type: string
is-merged:
description: Is merged
type: boolean
default: ${{ github.event.pull_request.merged == true }}
is-release:
description: Whether to release
type: boolean
default: true
pr-head-sha:
description: Pull request head SHA
type: string
default: ${{ github.event.pull_request.head.sha }}
version:
description: Next custom version (Not included prefix)
type: string
default: ""
outputs:
version:
description: Next version
value: ${{ jobs.calc-version.outputs.version }}
tag:
description: Next tag
value: ${{ jobs.calc-version.outputs.tag }}
changelog:
description: Changelog
value: ${{ jobs.calc-version.outputs.changelog }}
secrets:
DOCKER_USERNAME:
description: Docker registry password
required: true
DOCKER_PASSWORD:
description: Docker registry username
required: true
jobs:
# バージョンを計算する
calc-version:
name: Calculate next version
runs-on: ubuntu-latest
outputs:
# バージョン
version: ${{ steps.tag-version.outputs.new_version }}
# バージョンタグ
tag: ${{ steps.tag-version.outputs.new_tag }}
# リリースノート
changelog: ${{ steps.tag-version.outputs.changelog }}
# プラットフォーム(アーキテクチャ)のマトリクス
platform-matrix: ${{ steps.platform-matrix.outputs.matrix }}
# latestタグへのマージが必要かどうか (複数のプラットフォームがある時は必要)
need-merge: ${{ steps.platform-matrix.outputs.need-merge }}
# ビルド対象情報のマトリクス
targets-matrix: ${{ steps.targets-matrix.outputs.matrix }}
# パッケージのアップロードが必要かどうか
need-upload: ${{ steps.need-upload.outputs.need-upload }}
steps:
- name: 📥 Check out the repo
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
# マージされていない時には github.event.pull_request.head.sha を使い、マージされた時にはgithub.base_refを使う
ref: ${{ inputs.is-merged == true && github.base_ref || inputs.pr-head-sha }}
fetch-depth: 0
- name: 🏷️ Bump version and push tag
id: tag-version
uses: mathieudutour/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: "minor"
custom_release_rules: "feat:minor:✨ Features,fix:patch:🐛 Fixes,docs:patch:📰 Docs,chore:patch:🎨 Chore,pref:patch:🎈 Performance improvements,refactor:patch:🧹 Refactoring,build:patch:🔍 Build,ci:patch:🔍 CI,revert:patch:⏪ Revert,style:patch:🧹 Style,test:patch:👀 Test,release:major:📦 Release"
dry_run: ${{ inputs.is-merged == true && 'false' || 'true' }}
custom_tag: ${{ inputs.version }}
- name: 📊 Set platform matrix
id: platform-matrix
run: |
# shellcheck disable=SC2312
echo "matrix=[$(echo "${{ inputs.platforms }}" | sed -r -e 's/([^,]+)/\"\1\"/g' -e 's/([^,]+),/\1,/g')]" >> $GITHUB_OUTPUT
# ,があるときはneed-merge=true
echo "need-merge=$(echo "${{ inputs.platforms }}" | grep -q ',' && echo true || echo false)" >> $GITHUB_OUTPUT
- name: 🎯 Set targets matrix
id: targets-matrix
uses: actions/github-script@v7
with:
script: |
core.setOutput('matrix', process.env.TARGETS)
env:
TARGETS: ${{ inputs.targets }}
- name: 📦 Check if need package upload
id: need-upload
run: |
# package.json がない場合は false
if [ ! -f package.json ]; then
echo "need-upload=false" >> $GITHUB_OUTPUT
exit 0
fi
# "ncc build" が package.json にある場合は true、ない場合は false
echo "need-upload=$(grep -q 'ncc build' package.json && echo true || echo false)" >> $GITHUB_OUTPUT
# Dockerイメージをビルドする
build:
name: Docker build (${{ matrix.target.packageName }}, ${{ matrix.platform }})
runs-on: ubuntu-latest
needs: calc-version
strategy:
fail-fast: false
matrix:
# ビルドするプラットフォーム
platform: ${{ fromJSON(needs.calc-version.outputs.platform-matrix) }}
# ビルドするDockerfile
target: ${{ fromJSON(needs.calc-version.outputs.targets-matrix) }}
steps:
- name: 🗑️ Remove platform prefix
id: platform
run: |
echo "shortPlatform=$(echo "${{ matrix.platform }}" | sed -r -e 's/^[^\/]+\///')" >> $GITHUB_OUTPUT
echo "shortHyphenPlatform=$(echo "${{ matrix.platform }}" | sed -r -e 's/^[^\/]+\///' -e 's/\//-/g')" >> $GITHUB_OUTPUT
- name: 🚀 Create GitHub deployment
uses: chrnorm/[email protected]
if: inputs.is-merged == true
id: deployment
with:
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ needs.calc-version.outputs.tag }}
environment: "${{ matrix.target.packageName }}:${{ steps.platform.outputs.shortPlatform }}"
environment-url: https://github.com/${{ github.repository }}/pkgs/container/${{ matrix.target.packageName }}
- name: 🔄 Update deployment status (in_progress)
uses: chrnorm/[email protected]
if: inputs.is-merged == true
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: in_progress
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
- name: 📥 Check out the repo
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
# マージされていない時には github.event.pull_request.head.sha を使い、マージされた時にはgithub.base_refを使う
ref: ${{ inputs.is-merged == true && github.base_ref || inputs.pr-head-sha }}
- name: 🛠️ Set up QEMU
uses: docker/setup-qemu-action@v3
- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true
- name: 🔑 Login to ${{ inputs.registry }}
if: inputs.is-merged == true
uses: docker/[email protected]
with:
registry: ${{ inputs.registry }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: 📝 package.json update version
run: |
find . -type d -name node_modules -prune -o -type f -name package.json -exec sed -r -i "1,/version/s/\"version\": \".+\"/\"version\": \"${{ needs.calc-version.outputs.version }}\"/" {} \;
git diff
- name: 🏷️ Set tag suffix value
id: tag
run: |
if [ "${{ needs.calc-version.outputs.need-merge }}" = "true" ]; then
echo "suffix=-${{ steps.platform.outputs.shortHyphenPlatform }}" >> $GITHUB_OUTPUT
else
echo "suffix=" >> $GITHUB_OUTPUT
fi
- name: 📦 Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: ${{ inputs.registry }}/${{ matrix.target.imageName }}
tags: |
# need-mergeがtrueの時はshortHyphenPlatformを含める (latest-arm64)
# need-mergeがfalseの時はshortHyphenPlatformを含めない (latest)
type=raw,value=${{ needs.calc-version.outputs.tag }}${{ steps.tag.outputs.suffix }}
type=raw,value=latest${{ steps.tag.outputs.suffix }}
- name: 🏗️ Build and push Docker image
uses: docker/[email protected]
with:
context: ${{ matrix.target.context }}
file: ${{ matrix.target.file }}
push: ${{ inputs.is-merged == true }}
platforms: ${{ matrix.platform }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ matrix.target.packageName }}-${{ steps.platform.outputs.shortHyphenPlatform }}
cache-to: type=gha,mode=max,scope=${{ matrix.target.packageName }}-${{ steps.platform.outputs.shortHyphenPlatform }}
provenance: false
- name: 🔄 Update deployment status (success)
uses: chrnorm/[email protected]
if: ${{ success() && inputs.is-merged == true }}
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: success
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
- name: 🔄 Update deployment status (failure)
if: ${{ failure() && inputs.is-merged == true }}
uses: chrnorm/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
state: failure
deployment-id: ${{ steps.deployment.outputs.deployment_id }}
finished-build:
name: Check finished Docker CI
runs-on: ubuntu-latest
if: always()
needs:
- calc-version
- build
permissions:
actions: read
steps:
- name: Check build failure
run: |
echo "Build status: ${{ needs.build.result }}"
if [ "${{ needs.build.result }}" != "success" ]; then
echo "Build failed"
exit 1
fi
# 各プラットフォームのlatestからlatestタグにマージしてイメージを作成
merge-latest:
name: Merge latest (${{ matrix.target.packageName }})
runs-on: ubuntu-latest
if: inputs.is-merged == true && inputs.is-release == true
needs:
- calc-version
- build
strategy:
fail-fast: false
matrix:
# ビルドするDockerfile
target: ${{ fromJSON(needs.calc-version.outputs.targets-matrix) }}
steps:
- name: 🔑 Login to ${{ inputs.registry }}
if: inputs.is-merged == true
uses: docker/[email protected]
with:
registry: ${{ inputs.registry }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: 📝 Create extra-images list
id: extra-images
run: |
echo "images=$(echo "${{ inputs.platforms }}" | sed -r 's#([a-z]+)\/([a-z0-9]+)#${{ inputs.registry }}\/${{ matrix.target.imageName }}:latest-\2#g')" >> $GITHUB_OUTPUT
- name: 📝 Create and push manifest images (latest)
uses: Noelware/[email protected]
# プラットフォームがひとつの場合はマージ処理を行わない
if: ${{ needs.calc-version.outputs.need-merge == 'true' }}
with:
images: ${{ steps.extra-images.outputs.images }}
inputs: ${{ inputs.registry }}/${{ matrix.target.imageName }}:latest
push: true
- name: 📝 Create and push manifest images (version)
uses: Noelware/[email protected]
# プラットフォームがひとつの場合はマージ処理を行わない
if: ${{ needs.calc-version.outputs.need-merge == 'true' }}
with:
images: ${{ steps.extra-images.outputs.images }}
inputs: ${{ inputs.registry }}/${{ matrix.target.imageName }}:${{ needs.calc-version.outputs.tag }}
push: true
release:
name: Release ${{ needs.calc-version.outputs.tag }}
runs-on: ubuntu-latest
if: inputs.is-merged == true && inputs.is-release == true
needs:
- calc-version
- build
- merge-latest
outputs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- name: 📦 Create Release
id: create_release
uses: ncipollo/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag: ${{ needs.calc-version.outputs.tag }}
name: ${{ needs.calc-version.outputs.tag }}
body: ${{ needs.calc-version.outputs.changelog }}
draft: false
prerelease: false
upload-package:
name: Upload package ${{ needs.calc-version.outputs.tag }}
runs-on: ubuntu-latest
if: inputs.is-merged == true && inputs.is-release == true && needs.calc-version.outputs.need-upload == 'true'
needs:
- calc-version
- build
- merge-latest
- release
steps:
- name: 📥 Check out the repo
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
# マージされていない時には github.event.pull_request.head.sha を使い、マージされた時にはgithub.base_refを使う
ref: ${{ inputs.is-merged == true && github.base_ref || inputs.pr-head-sha }}
- name: 📂 Check exists .node-version
id: check-node-version-file
run: |
if [ ! -f ".node-version" ]; then
echo "exists=false" >> $GITHUB_OUTPUT
else
echo "exists=true" >> $GITHUB_OUTPUT
fi
- name: 🏗️ Setup node env
uses: actions/setup-node@v4
if: ${{ steps.check-node-version-file.outputs.exists == 'true' }}
with:
node-version-file: .node-version
- name: 🏗️ Setup node env
if: ${{ steps.check-node-version-file.outputs.exists == 'false' }}
uses: actions/setup-node@v4
with:
node-version: 18
- name: 📝 Check package manager
id: check-package-manager
run: |
if [ -f "yarn.lock" ]; then
echo "package-manager=yarn" >> "$GITHUB_OUTPUT"
elif [ -f "pnpm-lock.yaml" ]; then
echo "package-manager=pnpm" >> "$GITHUB_OUTPUT"
else
echo "package-manager=npm" >> "$GITHUB_OUTPUT"
fi
- name: Setup pnpm
uses: pnpm/action-setup@v4
if: ${{ steps.check-package-manager.outputs.package-manager == 'pnpm' }}
with:
run_install: false
- name: 📦 Install dependencies
run: |
if [ "${{ steps.check-package-manager.outputs.package-manager }}" == "yarn" ]; then
yarn install --frozen-lockfile
elif [ "${{ steps.check-package-manager.outputs.package-manager }}" == "pnpm" ]; then
pnpm install --frozen-lockfile --prefer-frozen-lockfile
else
npm install
fi
- name: 🎁 Run package
run: |
if [ "${{ steps.check-package-manager.outputs.package-manager }}" == "yarn" ]; then
yarn package
elif [ "${{ steps.check-package-manager.outputs.package-manager }}" == "pnpm" ]; then
pnpm run package
else
npm run package
fi
- name: 📦 Create zip
id: create-zip
run: |
REPO="${GITHUB_REPOSITORY#"${GITHUB_REPOSITORY_OWNER}/"}"
ZIP_FILENAME=${REPO}_${{ needs.calc-version.outputs.tag }}.zip
ZIP_PATH=$(pwd)/output/${ZIP_FILENAME}
cd output
zip -r ${ZIP_PATH} ./*
echo "zip-path=${ZIP_PATH}" >> $GITHUB_OUTPUT
echo "zip-filename=${ZIP_FILENAME}" >> $GITHUB_OUTPUT
- name: 📂 ls
run: |
pwd
ls -la
- name: 📤 Upload Release Asset
uses: shogo82148/[email protected]
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ${{ steps.create-zip.outputs.zip-path }}
asset_name: ${{ steps.create-zip.outputs.zip-filename }}