-
Notifications
You must be signed in to change notification settings - Fork 19
536 lines (464 loc) · 17.2 KB
/
build.yml
File metadata and controls
536 lines (464 loc) · 17.2 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
name: Build and Publish
on:
workflow_dispatch:
inputs:
logLevel:
description: "Log level"
required: true
default: "warning"
type: choice
options:
- info
- warning
- debug
build:
description: "Build"
required: false
default: false
type: boolean
update_readme:
description: "Update Readme"
required: false
default: false
type: boolean
push:
branches:
- master
- dev
tags:
- "v*"
paths-ignore:
- "**.md"
- ".github/ISSUE_TEMPLATE/**"
pull_request:
branches:
- master
- dev
paths-ignore:
- "**.md"
- ".github/ISSUE_TEMPLATE/**"
env:
REGISTRY: ${{ vars.REGISTRY != '' && vars.REGISTRY || '' }}
BUILD_AMD64: ${{ vars.BUILD_AMD64 != 'false' && 'true' || 'false' }}
BUILD_ARM64: ${{ vars.BUILD_ARM64 != 'false' && 'true' || 'false' }}
DOCKERHUB_SLUG: arabcoders/watchstate
GHCR_SLUG: ghcr.io/arabcoders/watchstate
PNPM_VERSION: 10
NODE_VERSION: 20
jobs:
validate-build-config:
name: Validate Build Configuration
runs-on: ubuntu-latest
permissions: {}
steps:
- name: Ensure at least one architecture is enabled
run: |
if [ "${{ env.BUILD_AMD64 }}" = "false" ] && [ "${{ env.BUILD_ARM64 }}" = "false" ]; then
echo "::error::Both BUILD_AMD64 and BUILD_ARM64 are disabled. Enable at least one architecture."
exit 1
fi
echo "Build configuration is valid"
echo "BUILD_AMD64: ${{ env.BUILD_AMD64 }}"
echo "BUILD_ARM64: ${{ env.BUILD_ARM64 }}"
test:
name: Tests & Frontend Build
runs-on: ubuntu-latest
needs: [validate-build-config]
permissions:
contents: read
strategy:
fail-fast: true
matrix:
php: [8.4]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update Version File
uses: arabcoders/write-version-to-file@master
with:
filename: "/config/config.php"
placeholder: "$(version_via_ci)"
with_date: "true"
with_branch: "true"
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pdo, mbstring, ctype, curl, sqlite3
coverage: none
tools: composer:v2
- name: Get composer cache directory
id: composer-cache
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"
- name: Restore cached dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.COMPOSER_CACHE_DIR }}
key: "${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}"
restore-keys: ${{ matrix.php }}-composer-
- name: Install PHP dependencies
run: composer install --prefer-dist --no-interaction --no-progress
- name: Run PHP formatter check
run: composer run format:ci
- name: Run PHP linter
run: composer run lint
- name: Run PHP tests
run: composer run test
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: ${{ env.PNPM_VERSION }}
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: pnpm
cache-dependency-path: "frontend/pnpm-lock.yaml"
- name: Install frontend dependencies
working-directory: frontend
run: pnpm install --frozen-lockfile --prefer-offline
- name: Run nuxt prepare
working-directory: frontend
env:
NODE_ENV: development
run: pnpm run prepare:test
- name: Run frontend linter
working-directory: frontend
run: pnpm run lint
- name: Run frontend typecheck
working-directory: frontend
run: pnpm run lint:tsc
- name: Build frontend
working-directory: frontend
env:
NODE_ENV: production
run: pnpm run generate
- name: Set up Python
if: env.REGISTRY == '' && github.event_name != 'pull_request'
uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install GitPython
if: env.REGISTRY == '' && github.event_name != 'pull_request'
run: pip install gitpython
- name: Generate CHANGELOG.json
if: env.REGISTRY == '' && github.event_name != 'pull_request'
run: |
python3 .github/scripts/generate_changelog.py -p . -f ./frontend/exported/CHANGELOG.json
- name: Upload frontend build
if: env.REGISTRY == '' && github.event_name != 'pull_request'
uses: actions/upload-artifact@v4
with:
name: frontend-build
path: frontend/exported/
retention-days: 1
docker-build-amd64:
name: Build Container (amd64)
runs-on: ubuntu-latest
needs: [test, validate-build-config]
if: (github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.build)) && vars.BUILD_AMD64 != 'false'
permissions:
packages: write
contents: write
env:
ARCH: amd64
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download frontend build
if: env.REGISTRY == ''
uses: actions/download-artifact@v4
with:
name: frontend-build
path: frontend/exported/
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Update config/config.php
run: |
VERSION="${GITHUB_REF##*/}"
SHA=$(git rev-parse HEAD)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
DATE=$(date -u +"%Y%m%d")
echo "Current version: ${VERSION}, SHA: ${SHA}, Date: ${DATE}"
echo "APP_VERSION=${VERSION}" >> "$GITHUB_ENV"
echo "APP_SHA=${SHA}" >> "$GITHUB_ENV"
echo "APP_DATE=${DATE}" >> "$GITHUB_ENV"
echo "APP_BRANCH=${BRANCH}" >> "$GITHUB_ENV"
sed -i \
-e "s/'version' => 'dev-master'/'version' => '${VERSION}'/" \
-e "s/'version_sha' => 'unknown'/'version_sha' => '${SHA}'/" \
-e "s/'version_build' => 'unknown'/'version_build' => '${DATE}'/" \
-e "s/'version_branch' => 'unknown'/'version_branch' => '${BRANCH}'/" \
config/config.php
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
name=${{ env.DOCKERHUB_SLUG }},enable=${{ env.REGISTRY == '' }}
name=${{ env.GHCR_SLUG }},enable=${{ env.REGISTRY == '' }}
name=${{ env.REGISTRY }}/${{ github.repository }},enable=${{ env.REGISTRY != '' }}
flavor: |
latest=false
suffix=-${{ env.ARCH }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
- name: Login to Private Registry
uses: docker/login-action@v3
if: env.REGISTRY != ''
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GIT_TOKEN && secrets.GIT_TOKEN || secrets.GITHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: env.REGISTRY == ''
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
uses: docker/login-action@v3
if: env.REGISTRY == ''
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/${{ env.ARCH }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ github.workflow_ref }}-${{ env.ARCH }}
cache-to: type=gha,mode=max,scope=${{ github.workflow_ref }}-${{ env.ARCH }}
provenance: true
docker-build-arm64:
name: Build Container (arm64)
runs-on: ubuntu-latest
needs: [test, validate-build-config]
if: (github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.build)) && vars.BUILD_ARM64 != 'false'
permissions:
packages: write
contents: write
env:
ARCH: arm64
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download frontend build
if: env.REGISTRY == ''
uses: actions/download-artifact@v4
with:
name: frontend-build
path: frontend/exported/
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Update config/config.php
run: |
VERSION="${GITHUB_REF##*/}"
SHA=$(git rev-parse HEAD)
BRANCH=$(git rev-parse --abbrev-ref HEAD)
DATE=$(date -u +"%Y%m%d")
echo "Current version: ${VERSION}, SHA: ${SHA}, Date: ${DATE}"
echo "APP_VERSION=${VERSION}" >> "$GITHUB_ENV"
echo "APP_SHA=${SHA}" >> "$GITHUB_ENV"
echo "APP_DATE=${DATE}" >> "$GITHUB_ENV"
echo "APP_BRANCH=${BRANCH}" >> "$GITHUB_ENV"
sed -i \
-e "s/'version' => 'dev-master'/'version' => '${VERSION}'/" \
-e "s/'version_sha' => 'unknown'/'version_sha' => '${SHA}'/" \
-e "s/'version_build' => 'unknown'/'version_build' => '${DATE}'/" \
-e "s/'version_branch' => 'unknown'/'version_branch' => '${BRANCH}'/" \
config/config.php
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
name=${{ env.DOCKERHUB_SLUG }},enable=${{ env.REGISTRY == '' }}
name=${{ env.GHCR_SLUG }},enable=${{ env.REGISTRY == '' }}
name=${{ env.REGISTRY }}/${{ github.repository }},enable=${{ env.REGISTRY != '' }}
flavor: |
latest=false
suffix=-${{ env.ARCH }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
- name: Login to Private Registry
uses: docker/login-action@v3
if: env.REGISTRY != ''
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GIT_TOKEN && secrets.GIT_TOKEN || secrets.GITHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: env.REGISTRY == ''
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
uses: docker/login-action@v3
if: env.REGISTRY == ''
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/${{ env.ARCH }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ github.workflow }}-${{ env.ARCH }}
cache-to: type=gha,mode=max,scope=${{ github.workflow }}-${{ env.ARCH }}
provenance: true
docker-publish-manifest:
name: Publish multi-arch manifest
runs-on: ubuntu-latest
needs: [docker-build-amd64, docker-build-arm64]
if: |
!cancelled() && github.event_name != 'pull_request' &&
(needs.docker-build-amd64.result == 'success' || needs.docker-build-arm64.result == 'success')
permissions:
packages: write
contents: write
env:
AMD64_RESULT: ${{ needs.docker-build-amd64.result }}
ARM64_RESULT: ${{ needs.docker-build-arm64.result }}
steps:
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
name=${{ env.DOCKERHUB_SLUG }},enable=${{ env.REGISTRY == '' }}
name=${{ env.GHCR_SLUG }},enable=${{ env.REGISTRY == '' }}
name=${{ env.REGISTRY }}/${{ github.repository }},enable=${{ env.REGISTRY != '' }}
flavor: |
latest=false
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
- name: Login to Private Registry
uses: docker/login-action@v3
if: env.REGISTRY != ''
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GIT_TOKEN && secrets.GIT_TOKEN || secrets.GITHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: env.REGISTRY == ''
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
uses: docker/login-action@v3
if: env.REGISTRY == ''
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create and push manifest list
shell: bash
run: |
IFS=$'\n'
BUILD_AMD64="${{ env.BUILD_AMD64 }}"
BUILD_ARM64="${{ env.BUILD_ARM64 }}"
AMD64_RESULT="${AMD64_RESULT}"
ARM64_RESULT="${ARM64_RESULT}"
for tag in $(echo "${{ steps.meta.outputs.tags }}"); do
echo "Creating manifest for ${tag}"
IMAGES=()
if [ "${BUILD_AMD64}" = "true" ] && [ "${AMD64_RESULT}" = "success" ]; then
IMAGES+=("${tag}-amd64")
fi
if [ "${BUILD_ARM64}" = "true" ] && [ "${ARM64_RESULT}" = "success" ]; then
IMAGES+=("${tag}-arm64")
fi
if [ ${#IMAGES[@]} -eq 0 ]; then
echo "::error::No images available for manifest creation"
exit 1
fi
docker buildx imagetools create --tag "${tag}" "${IMAGES[@]}"
done
- name: Overwrite GitHub release notes
if: startsWith(github.ref, 'refs/tags/v') && env.REGISTRY == ''
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const tag = context.ref.replace('refs/tags/', '');
const { data: releases } = await github.rest.repos.listReleases({
owner: context.repo.owner,
repo: context.repo.repo,
});
const release = releases.find(r => r.tag_name === tag);
if (!release) {
core.setFailed(`Release with tag ${tag} not found`);
return;
}
const baseTag = releases[1]?.tag_name || '';
if (!baseTag) {
core.setFailed('Could not determine a base tag (no previous release found)');
return;
}
const { data: comparison } = await github.rest.repos.compareCommits({
owner: context.repo.owner,
repo: context.repo.repo,
base: baseTag,
head: tag,
});
const commits = comparison.commits.filter(c => c.parents.length === 1);
const changelog = commits
.map(c => `- ${c.sha.substring(0, 7)} ${c.commit.message.split('\n')[0]}`)
.join('\n');
if (!changelog) {
core.setFailed('No commits found for the changelog');
return;
}
const existingBody = (release.body || '').trim();
const separator = `\n\n---\n\n## Commits since ${baseTag}\n\n`;
const newBody = existingBody
? `${existingBody}${separator}${changelog}`
: `## Commits since ${baseTag}\n\n${changelog}`;
console.log(`Updating release ${tag}. Appending ${commits.length} commits under existing body.`);
await github.rest.repos.updateRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id,
body: newBody,
});
dockerhub-sync-readme:
name: DockerHub README sync
runs-on: ubuntu-latest
permissions:
contents: read
if: vars.REGISTRY == '' && ((github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || (github.event_name == 'workflow_dispatch' && github.event.inputs.update_readme == 'true'))
steps:
- name: Sync README
uses: docker://lsiodev/readme-sync:latest
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_TOKEN }}
GIT_REPOSITORY: ${{ github.repository }}
DOCKER_REPOSITORY: ${{ env.DOCKERHUB_SLUG }}
GIT_BRANCH: ${{ github.event.repository.default_branch }}
with:
entrypoint: node
args: /opt/docker-readme-sync/sync