This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
212 lines (189 loc) · 7.91 KB
/
build.yaml
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
---
name: NextJS build and release
# description: lint, test, build and release NextJS
on: push
env:
FORCE_COLOR: true # display terminal colors
# APP_NAME: app_name
# GHCR_IMAGE: ghcr.io/<user>/<repo name>
CONTAINER_REGISTRY: ghcr.io
IMAGE_NAME: clumsy-coder/pihole-dashboard
####################################################################################################
jobs:
# install npm packages and store them as cache.
install:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: latest
- name: Cache node modules
id: cache-primes
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node-
# skip npm ci if `package.json` didn't change
# https://github.com/actions/cache#outputs
# https://github.com/actions/cache#restoring-and-saving-cache-using-a-single-action
- name: Install npm dependencies
if: steps.cache-primes.outputs.cache-hit != 'true'
run: npm ci --include=dev
################################################################################################
# lint source code using ESlint and Typescript
lint:
needs: install
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: latest
- name: Cache node modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node-
- name: Lint project
run: npm run lint
################################################################################################
# prepare docker built
# extract the next version tag
prepare-docker-build:
needs: install
runs-on: ubuntu-latest
outputs:
NEXT_VERSION: ${{ steps.set-env.outputs.NEXT_VERSION }}
PUBLISH: ${{ steps.set-env.outputs.PUBLISH }}
BUILD_DATE: ${{ steps.set-env.outputs.BUILD_DATE }}
GIT_SHA: ${{ steps.set-env.outputs.GIT_SHA }}
GIT_REF: ${{ steps.set-env.outputs.GIT_REF }}
new-release-published: ${{ steps.get-next-version.outputs.new-release-published }}
new-release-version: ${{ steps.get-next-version.outputs.new-release-version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: latest
- name: Cache node modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node-
- name: Extract next semantic-release version
# run: npx semantic-release --dry-run --branches="*"
run: npx semantic-release --dry-run
id: get-next-version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# needed in case semantic-release doesn't run on branches other than 'master' or 'development'
- name: Set NEXT_VERSION if there's NO new release
if: |
steps.get-next-version.outputs.new-release-published == '' ||
steps.get-next-version.outputs.new-release-published == 'false'
run: |
node -p "require('./package').version"
node -p "require('./package').version" | awk '{print "NEXT_VERSION=" $1}' >> "$GITHUB_ENV"
echo "PUBLISH=false" >> "$GITHUB_ENV"
- name: Set NEXT_VERSION if there's a NEW release
if: steps.get-next-version.outputs.new-release-published == 'true'
run: |
echo ${{ steps.get-next-version.outputs.new-release-version }}
echo "NEXT_VERSION=${{ steps.get-next-version.outputs.new-release-version }}" >> "$GITHUB_ENV"
echo "PUBLISH=true" >> "$GITHUB_ENV"
- name: Set Environment Variables
id: set-env
run: |
{
echo "NEXT_VERSION=$NEXT_VERSION"
echo "PUBLISH=$PUBLISH"
echo "BUILD_DATE=$(date +'%Y-%m-%d %H:%M:%S')"
echo "GIT_SHA=$(echo ${{ github.sha }} | cut -c1-7)"
# replace `/` with `-`
# mainly used for branches created by dependabot. they contain `/` which docker tagname cannot have
# https://docs.docker.com/engine/reference/commandline/tag/#description
# https://stackoverflow.com/a/73799519/3053548
# https://stackoverflow.com/a/73467468/3053548
echo "GIT_REF=${GITHUB_REF_NAME//\//-}"
# echo "GHCR_IMAGE=$(echo 'console.log("ghcr.io/${{ github.repository }}".toLowerCase())' | node -)" >> $GITHUB_ENV
} | tee -a "$GITHUB_ENV" "$GITHUB_OUTPUT"
- run: echo "$GITHUB_ENV"
################################################################################################
# build docker images using reusable workflow and github action matrix
docker-build-image:
needs: prepare-docker-build
strategy:
matrix:
# using paired matrix values
# obtained from
# https://stackoverflow.com/a/76547617/3053548
include:
- version: latest
publish: ${{ needs.prepare-docker-build.outputs.publish == 'true' }}
# next version
- version: ${{ needs.prepare-docker-build.outputs.next_version }}
publish: ${{ needs.prepare-docker-build.outputs.publish == 'true' }}
# nightly
- version: nightly
publish: ${{ github.ref == 'refs/heads/development' }}
# branches that are not `development` or `master`
# used for testing code of development branches
- version: ${{ needs.prepare-docker-build.outputs.git_ref }}
publish: ${{ github.ref != 'refs/heads/development' && github.ref != 'refs/heads/master' && github.actor != 'dependabot[bot]' }}
uses: ./.github/workflows/reusable-docker-build.yml
with:
publish: ${{ matrix.publish }}
tags: ${{ matrix.version }}
secrets: inherit
################################################################################################
# verify matrix job results
# since branch protection rules can't group the job matrix together as a required status check.
# this will serve as a workaround
# code obtained from
# - https://github.com/orgs/community/discussions/26822#discussioncomment-5122101
matrix-results:
needs: docker-build-image
runs-on: ubuntu-latest
name: verify job matrix results
if: always()
steps:
- run: exit 1
# see https://stackoverflow.com/a/67532120/4907315
if: >-
${{
contains(needs.*.result, 'failure')
|| contains(needs.*.result, 'cancelled')
}}
################################################################################################
semantic-release:
# prerequisite of a job that uses matrix
# https://github.com/orgs/community/discussions/42010#discussioncomment-4439644
needs: docker-build-image
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/development'
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-node@v4
with:
node-version: latest
- name: Cache node modules
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: ${{ runner.os }}-node-
- run: npm install --production=false
- name: semantic-release
run: npx semantic-release --ci
env:
GITHUB_TOKEN: ${{ secrets.DEPENDABOT_TOKEN }}
####################################################################################################