-
-
Notifications
You must be signed in to change notification settings - Fork 0
361 lines (347 loc) · 14.1 KB
/
ci.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
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
name: Add-on CI/CD
on:
push:
tags:
- '*'
branches:
- "*"
pull_request:
branches:
- "main"
jobs:
discover:
name: Discover Add-ons
runs-on: ubuntu-latest
outputs:
addon_dirs: ${{ steps.folders.outputs.addon_dirs }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
- name: Find add-on directories
id: folders
run: |
ADDON_DIRS=$(find addons -mindepth 1 -maxdepth 1 -type d | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "Found add-on directories: $ADDON_DIRS"
echo "addon_dirs=$ADDON_DIRS" >> $GITHUB_OUTPUT
- name: Debug - List directories
run: |
echo "Found add-on directories: ${{ steps.folders.outputs.addon_dirs }}"
information:
name: Gather add-on information
needs: discover
runs-on: ubuntu-latest
strategy:
matrix:
folder: ${{ fromJson(needs.discover.outputs.addon_dirs) }}
outputs:
addon_dirs: ${{ needs.discover.outputs.addon_dirs }}
architectures: ${{ steps.information.outputs.architectures }}
build: ${{ steps.information.outputs.build }}
description: ${{ steps.information.outputs.description }}
name: ${{ steps.information.outputs.name }}
slug: ${{ steps.information.outputs.slug }}
target: ${{ steps.information.outputs.target }}
version: ${{ steps.read_version.outputs.version }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
with:
fetch-depth: 0 # Ensure all tags are fetched
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Run add-on information action
id: information
uses: frenck/action-addon-information@v1
with:
path: "./${{ matrix.folder }}"
- name: Read version from version.txt
id: read_version
run: |
VERSION=$(cat ./${{ matrix.folder }}/version.txt)
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Ensure version is 'dev-versionnumber' for branches
if: github.ref != 'refs/heads/main'
run: |
VERSION=$(cat ./${{ matrix.folder }}/version.txt)
jq --arg version "dev-$VERSION" '.version = $version' ./${{ matrix.folder }}/config.json > tmp.json && mv tmp.json ./${{ matrix.folder }}/config.json
- name: Ensure version is correct for main
if: github.ref == 'refs/heads/main'
run: |
VERSION=$(cat ./${{ matrix.folder }}/version.txt)
jq --arg version "$VERSION" '.version = $version' ./${{ matrix.folder }}/config.json > tmp.json && mv tmp.json ./${{ matrix.folder }}/config.json
- name: Update build.json
run: |
VERSION=$(cat ./${{ matrix.folder }}/version.txt)
jq --arg version "$VERSION" '.BUILD_VERSION = $version' ./${{ matrix.folder }}/build.json > tmp.json && mv tmp.json ./${{ matrix.folder }}/build.json
- name: Commit and push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add ./${{ matrix.folder }}/config.json ./${{ matrix.folder }}/build.json
if ! git diff-index --quiet HEAD; then
git commit -m "Update version in config.json and build.json"
git pull --rebase origin ${{ github.ref }}
git push https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }}
else
echo "No changes to commit"
fi
- name: Debug - List extracted information
run: |
echo "Architectures: ${{ steps.information.outputs.architectures }}"
echo "Build: ${{ steps.information.outputs.build }}"
echo "Description: ${{ steps.information.outputs.description }}"
echo "Name: ${{ steps.information.outputs.name }}"
echo "Slug: ${{ steps.information.outputs.slug }}"
echo "Target: ${{ steps.information.outputs.target }}"
echo "Version: ${{ steps.read_version.outputs.version }}"
lint-hadolint:
name: Hadolint
needs: information
runs-on: ubuntu-latest
strategy:
matrix:
folder: ${{ fromJson(needs.information.outputs.addon_dirs) }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Run Hadolint
uses: brpaz/hadolint-action@v1.5.0
with:
dockerfile: "./${{ matrix.folder }}/Dockerfile"
config: .hadolint.yaml
lint-json:
name: JSON Lint
needs: information
runs-on: ubuntu-latest
strategy:
matrix:
folder: ${{ fromJson(needs.information.outputs.addon_dirs) }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Run JQ
run: |
shopt -s globstar
cat ./${{ matrix.folder }}/**/*.json | jq '.'
lint-shellcheck:
name: Shellcheck
needs: information
runs-on: ubuntu-latest
strategy:
matrix:
folder: ${{ fromJson(needs.information.outputs.addon_dirs) }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Run Shellcheck
uses: ludeeus/action-shellcheck@2.0.0
env:
SHELLCHECK_OPTS: -s bash
lint-yamllint:
name: YAMLLint
needs: information
runs-on: ubuntu-latest
strategy:
matrix:
folder: ${{ fromJson(needs.information.outputs.addon_dirs) }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Run YAMLLint
uses: frenck/action-yamllint@v1.5
with:
config: .yamllint
lint-prettier:
if: github.ref != 'refs/heads/main'
name: Prettier
needs: information
runs-on: ubuntu-latest
strategy:
matrix:
folder: ${{ fromJson(needs.information.outputs.addon_dirs) }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
- name: Debug - Print addon_dirs
run: |
echo "addon_dirs: ${{ needs.information.outputs.addon_dirs }}"
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Run Prettier
uses: creyD/prettier_action@v4.3
with:
prettier_options: --write ./${{ matrix.folder }}/**/*.{json,js,md,yaml}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Commit and push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add ./${{ matrix.folder }}/config.json ./${{ matrix.folder }}/build.json
if ! git diff-index --quiet HEAD; then
git commit -m "Prettified Code!"
git pull --rebase origin ${{ github.ref }}
git push https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git HEAD:${{ github.ref }}
else
echo "No changes to commit"
fi
build:
name: Build ${{ matrix.architecture }}
needs:
- lint-prettier
- lint-hadolint
- lint-json
- lint-shellcheck
- lint-yamllint
- information
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
architecture: ${{ fromJson(needs.information.outputs.architectures || '["amd64"]') }}
folder: ${{ fromJson(needs.information.outputs.addon_dirs || '["addons/example"]') }}
outputs:
version: ${{ steps.set_version.outputs.version }}
slug_lower: ${{ steps.convert.outputs.slug_lower }}
repository_owner_lower: ${{ steps.convert.outputs.repository_owner_lower }}
folder: ${{ matrix.folder }}
steps:
- name: Check out code from GitHub
uses: actions/checkout@v3
- name: Set version for branches
id: set_version
run: |
VERSION=${{ needs.information.outputs.version }}
if [ "${{ github.ref }}" != "refs/heads/main" ]; then
VERSION="dev-${VERSION}"
fi
echo "version=${VERSION}" >> $GITHUB_OUTPUT
- name: Debug - Print matrix folder path
run: |
echo "Matrix folder path: ${{ matrix.folder }}"
- name: Set up build cache
id: cache
uses: actions/cache@v4.0.2
with:
path: /tmp/.docker-cache
key: docker-${{ matrix.architecture }}-${{ github.sha }}
restore-keys: |
docker-${{ matrix.architecture }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3.0.0
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.3.0
- name: Compose build flags
id: flags
run: |
echo "date=$(date '+%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
from=$(yq eval '.build_from["${{ matrix.architecture }}"]' "${{ needs.information.outputs.build }}")
from=$(echo $from | tr -d '"')
echo "from=${from}" >> $GITHUB_OUTPUT
echo "BUILD_VERSION=${{ needs.information.outputs.version }}" >> $GITHUB_OUTPUT
if [[ "${{ matrix.architecture }}" == "amd64" ]]; then
echo "platform=linux/amd64" >> $GITHUB_OUTPUT
elif [[ "${{ matrix.architecture }}" == "i386" ]]; then
echo "platform=linux/386" >> $GITHUB_OUTPUT
elif [[ "${{ matrix.architecture }}" == "armhf" ]]; then
echo "platform=linux/arm/v6" >> $GITHUB_OUTPUT
elif [[ "${{ matrix.architecture }}" == "armv7" ]]; then
echo "platform=linux/arm/v7" >> $GITHUB_OUTPUT
elif [[ "${{ matrix.architecture }}" == "aarch64" ]]; then
echo "platform=linux/arm64/v8" >> $GITHUB_OUTPUT
else
echo "::error ::Could not determine platform for architecture ${{ matrix.architecture }}"
exit 1
fi
- name: Debug - Print 'from' variable
run: |
echo "From variable: ${{ steps.flags.outputs.from }}"
- name: Download base image
if: steps.flags.outputs.from != 'null'
run: |
echo "Pulling base image: ${{ steps.flags.outputs.from }}"
docker pull ${{ steps.flags.outputs.from }}
- name: Convert to Lowercase
id: convert
run: |
SLUG_LOWER=$(echo ${{ needs.information.outputs.slug }} | tr '[:upper:]' '[:lower:]')
REPOSITORY_LOWER=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')
REPOSITORY_OWNER_LOWER=$(echo ${{ github.repository_owner }} | tr '[:upper:]' '[:lower:]')
echo "slug_lower=$SLUG_LOWER" >> $GITHUB_OUTPUT
echo "repository_lower=$REPOSITORY_LOWER" >> $GITHUB_OUTPUT
echo "repository_owner_lower=$REPOSITORY_OWNER_LOWER" >> $GITHUB_OUTPUT
- name: Build and Push Docker image
uses: docker/build-push-action@v6.2.0
with:
push: true
context: ./${{ matrix.folder }}
file: ./${{ matrix.folder }}/Dockerfile
cache-from: |
type=local,src=/tmp/.docker-cache
format('ghcr.io/{0}/{1}/{2}:edge', github.repository_owner, needs.information.outputs.slug, matrix.architecture)
cache-to: type=local,mode=max,dest=/tmp/.docker-cache-new
platforms: ${{ steps.flags.outputs.platform }}
build-args: |
BUILD_ARCH=${{ matrix.architecture }}
BUILD_DATE=${{ steps.flags.outputs.date }}
BUILD_DESCRIPTION=${{ needs.information.outputs.description }}
BUILD_FROM=${{ steps.flags.outputs.from }}
BUILD_NAME=${{ needs.information.outputs.name }}
BUILD_REF=${{ github.sha }}
BUILD_REPOSITORY=${{ steps.convert.outputs.repository_lower }}
BUILD_VERSION=${{ needs.information.outputs.version }}
tags: ghcr.io/${{ steps.convert.outputs.repository_owner_lower }}/${{ steps.convert.outputs.slug_lower }}:${{ steps.set_version.outputs.version }}-${{ matrix.architecture }}
- name: Swap build cache
run: |
if [ -d /tmp/.docker-cache-new ]; then
rm -rf /tmp/.docker-cache
mv /tmp/.docker-cache-new /tmp/.docker-cache
else
echo "Cache directory /tmp/.docker-cache-new does not exist"
fi
update-config:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up jq
run: sudo apt-get install jq
- name: Update config.json with new image URL
run: |
IMAGE_URL="ghcr.io/${{ needs.build.outputs.repository_owner_lower }}/${{ needs.build.outputs.slug_lower }}:${{ needs.build.outputs.version }}"
jq --arg image "$IMAGE_URL" '.image = $image' ./${{ needs.build.outputs.folder }}/config.json > tmp.json && mv tmp.json ./${{ needs.build.outputs.folder }}/config.json
- name: Commit and push updated config.json
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add ./${{ needs.build.outputs.folder }}/config.json
git commit -m "Update config.json with new image URL"
git pull --rebase origin ${{ github.ref }}
git push