forked from bigtreetech/CB1-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
360 lines (315 loc) · 15.1 KB
/
main.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
##
## TODO: The following issues still needs to be fixed
##
## - [ ] Fix the apt repo itself, so it's a valid apt repo, compatible with Debian etc.
##
## - [ ] Build time dependencies, possibly even cached build files, should be cached appropriately,
## as otherwise builds can take a VERY long time to complete!
##
## - [ ] Update the apt repo instead of replacing it, possibly by simply caching the packages?
##
## - [ ] Rename or refactor the built kernel version, at the very least append a timestamp
## and/or a short git commit hash, so we can easily distinguish between different versions
##
## - [ ] Decide on the appropriate codename, suite, components etc. and stick with that naming
##
## - [ ] Run a quick automated test after deploying the apt repo to GitHub Pages,
## just to confirm that a) the website is functional and b) it is a valid apt repo (if possible)
##
name: Build and Deploy
on:
## TODO: Setup scheduled builds, but only after we have caching working!
## TODO: Setup proper commit, PR, tags etc. building rules, also after caching works!
#push:
# branches: [ 'build', 'kernel-*' ]
## FIXME: Add tag based builds, depending on how we want to handle
## building, deploying and releasing different things?!
# Run on a schedule
# (note that caches are only kept for a week)
schedule:
# Run once a week, every Monday morning
# - cron: '0 3 * * MON'
# Run once a day
- cron: '0 3 * * *'
# Run when manually triggered
workflow_dispatch:
inputs:
force:
type: boolean
description: 'Forced build and deploy (also updates cache)'
required: false
default: false
env:
REPO_PATH: ${{ github.workspace }}/repo
PACKAGES_PATH: ${{ github.workspace }}/packages
KEYS_PATH: ${{ github.workspace }}/keys
BUILD_PATH: ${{ github.workspace }}/build
OUTPUT_PATH: ${{ github.workspace }}/output
TARGET_BRANCH: kernel-5.16
PACKAGES_ARTIFACT: 'cb1-kernel-packages'
jobs:
# Build job
build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 300 ## TODO: Increase this if necessary! Benchmark a bit, look into caching etc.
strategy:
fail-fast: true
outputs:
updated: ${{ steps.copy-packages.outputs.update == 'true' || steps.cache-packages.outputs.cache-hit != 'true' }}
steps:
- name: Checkout Repository
timeout-minutes: 15
uses: actions/checkout@v3
with:
# Use the target branch instead of our current branch
ref: ${{ env.TARGET_BRANCH }}
# Always limit depth
#fetch-depth: 1
token: ${{ secrets.GITHUB_TOKEN }}
- name: Prepare Environment
timeout-minutes: 5
run: mkdir -p ${{ env.REPO_PATH }} ${{ env.PACKAGES_PATH }} ${{ env.KEYS_PATH }}
- name: Prepare Kernel
id: prepare-kernel
timeout-minutes: 15
uses: didstopia/cb1-kernel-builder@master
with:
repo: github.com/${{ github.repository }}
branch: ${{ env.TARGET_BRANCH }}
build-path: ${{ env.BUILD_PATH }}
only-detect-kernel-version: yes
- name: Check Kernel Revision
id: check-kernel-revision
timeout-minutes: 5
run: |-
# Enable debugging
#set -x
## TODO: Add/change echo statements to utilize GHA logging instead!
## TODO: Add support for "pre-release" revisions, based on
## commit hashes, if possible or feasible?
# Prepare the target kernel revision and the revision update state
CURRENT_REVISION="current"
TARGET_REVISION="current"
IS_NEW_REVISION="false"
##
## NOTE: We can detect the current revision like so:
## grep -oP '(?<=REVISION=")[^"]+' scripts/main.sh
##
# Detect and validate the current kernel revision (eg. "2.3.2")
CURRENT_REVISION_NEW=${{ steps.prepare-kernel.outputs.revision }}
if [ -z "$CURRENT_REVISION_NEW" ]; then
# Output the current values and exit with an error code
echo "target_revision=${TARGET_REVISION}" > $GITHUB_OUTPUT
echo "is_new_revision=${IS_NEW_REVISION}" > $GITHUB_OUTPUT
echo "Failed to get the current kernel revision"
exit 1
fi
# Store and log the current kernel revision
CURRENT_REVISION="${CURRENT_REVISION_NEW}"
echo "Detected current kernel revision: ${CURRENT_REVISION_NEW}"
# Fetch the latest tags
#git remote add upstream [email protected]:bigtreetech/CB1-Kernel.git
#git fetch --tags upstream
git fetch --tags || true
# Get the latest repository tag, remove the "V" prefix
# (if any), and validate it as the new kernel revision
TARGET_REVISION_NEW=$(git describe --tags `git rev-list --tags --max-count=1 2>/dev/null` 2>/dev/null) || true
if [ -z "$TARGET_REVISION_NEW" ]; then
# Target the current revision instead
TARGET_REVISION_NEW="${CURRENT_REVISION_NEW}"
echo "No git tags found, falling back to revision ${TARGET_REVISION_NEW}"
# Push the new tag to the remote repository
echo "Pushing a new tag for V${TARGET_REVISION_NEW}"
git config user.email "[email protected]"
git config user.name "GitHub Actions"
git tag -a "V${TARGET_REVISION_NEW}" -m "V${TARGET_REVISION_NEW}"
git push origin "V${TARGET_REVISION_NEW}" --force
# Output the current values and exit with an error code
#echo "target_revision=${TARGET_REVISION}" > $GITHUB_OUTPUT
#echo "is_new_revision=${IS_NEW_REVISION}" > $GITHUB_OUTPUT
#echo "Failed to get the new kernel revision"
#exit 1
fi
# Remove the "V" prefix if it exists
TARGET_REVISION_NEW=${TARGET_REVISION_NEW#V}
# Store and log the new current kernel revision
TARGET_REVISION="${TARGET_REVISION_NEW}"
echo "Detected new kernel revision: ${TARGET_REVISION_NEW}"
# Compare the current and new kernel revisions
if [ "$CURRENT_REVISION_NEW" = "$TARGET_REVISION" ]; then
echo "Detected no changes between kernel revisions"
IS_NEW_REVISION="false"
else
## TODO: Wouldn't it make more sense to do semver compatible
## version comparisons, but also validate them as semvers?
echo "Detected changes between kernel revisions"
IS_NEW_REVISION="true"
fi
# Output the target kernel revision and the revision update state
echo "Generating output data:"
echo "-> target_revision=${TARGET_REVISION}
echo "-> is_new_revision=${IS_NEW_REVISION}
echo "target_revision=${TARGET_REVISION}" > $GITHUB_OUTPUT
echo "is_new_revision=${IS_NEW_REVISION}" > $GITHUB_OUTPUT
## TODO: This is only a test! This might not work AT ALL because there's a ton of files!
- name: Cache Build
id: cache-build
if: ${{ steps.check-kernel-revision.outputs.is_new_revision == 'true' }}
uses: actions/cache@v3
with:
#path: ${{ env.BUILD_PATH }}
path: build/
key: ${{ runner.os }}-build-${{ env.TARGET_BRANCH }}
- name: Build Kernel
if: ${{ steps.check-kernel-revision.outputs.is_new_revision == 'true' }}
timeout-minutes: 120 # NOTE: This typically takes less than an hour, but may take longer!
uses: didstopia/cb1-kernel-builder@master
with:
repo: github.com/${{ github.repository }}
branch: ${{ env.TARGET_BRANCH }}
build-path: ${{ env.BUILD_PATH }}
output-path: ${{ env.OUTPUT_PATH }}
target-board: h616
target-branch: ${{ steps.check-kernel-revision.outputs.target_revision }}
# target-branch: current # FIXME: Should be keep this "current", to match the official builds?
# target-branch: ${{ env.TARGET_BRANCH }}
# target-branch: cb1-${{ env.TARGET_BRANCH }}
target-build-option: kernel # FIXME: We also want to build u-boot AND the image, right?!
target-release: bullseye # FIXME: Do we want to set this to something else?
- name: Build U-Boot
if: ${{ steps.check-kernel-revision.outputs.is_new_revision == 'true' }}
timeout-minutes: 120
uses: didstopia/cb1-kernel-builder@master
with:
repo: github.com/${{ github.repository }}
branch: ${{ env.TARGET_BRANCH }}
build-path: ${{ env.BUILD_PATH }}
output-path: ${{ env.OUTPUT_PATH }}
target-board: h616
target-branch: ${{ steps.check-kernel-revision.outputs.target_revision }}
# target-branch: current # FIXME: Should be keep this "current", to match the official builds?
# target-branch: ${{ env.TARGET_BRANCH }}
# target-branch: cb1-${{ env.TARGET_BRANCH }}
target-build-option: u-boot # FIXME: We also want to build u-boot AND the image, right?!
target-release: bullseye # FIXME: Do we want to set this to something else?
## TODO: Does this even work correctly? Pretty sure it does NOT,
## and instead it just updates the packages with every single run?!
- name: Copy Packages
id: copy-packages
timeout-minutes: 5
run: |-
# Generate hashes for the previous and new packages
OLD_PACKAGES_HASH=${{ hashFiles('packages/**/*') }}
NEW_PACKAGES_HASH=${{ hashFiles('output/**/*') }}
# Check if we are forcefully updating
if [ "${{ github.event.inputs.force }}" = "true" ]; then
echo "Forced update detected, updating all packages (where possible)"
# Copy any built packages to the packages folder
#cp -fr ${{ env.OUTPUT_PATH }}/debs/*.deb ${{ env.PACKAGES_PATH }}/
find ${{ env.OUTPUT_PATH }} -name '*.deb' -exec cp -prv '{}' '${{ env.PACKAGES_PATH }}/' ';'
echo "update=true" >> "$GITHUB_OUTPUT"
# Check if the package hashes haven't changed
elif [ "$OLD_PACKAGES_HASH" = "$NEW_PACKAGES_HASH" ]; then
echo "No changes detected in packages, skipping updates"
# No changes to packages, no update necessary
echo "update=false" >> "$GITHUB_OUTPUT"
# Check if we have a new kernel revision
elif [ "${{ steps.check-kernel-revision.outputs.is_new_revision }}" = "true" ]; then
echo "New kernel revision detected, updating all packages (where possible)"
# Copy any built packages to the packages folder
#cp -fr ${{ env.OUTPUT_PATH }}/debs/*.deb ${{ env.PACKAGES_PATH }}/
find ${{ env.OUTPUT_PATH }} -name '*.deb' -exec cp -prv '{}' '${{ env.PACKAGES_PATH }}/' ';'
echo "update=true" >> "$GITHUB_OUTPUT"
# Fallback, update all packages where possible
else
echo "Fallback detected, updating all packages (where possible)"
# Copy any built packages to the packages folder
#cp -fr ${{ env.OUTPUT_PATH }}/debs/*.deb ${{ env.PACKAGES_PATH }}/
find ${{ env.OUTPUT_PATH }} -name '*.deb' -exec cp -prv '{}' '${{ env.PACKAGES_PATH }}/' ';'
echo "update=true" >> "$GITHUB_OUTPUT"
fi
## TODO: Remove when done debugging!
- name: List Packages
#if: ${{ steps.copy-packages.outputs.update == 'true' }}
run: ls -la ${{ env.PACKAGES_PATH }}/ || true
## TODO: Does this work? Verify that it does!
- name: Cache Packages
#if: ${{ steps.copy-packages.outputs.update == 'true' }}
id: cache-packages
uses: actions/cache@v3
with:
#path: ${{ env.PACKAGES_PATH }}
path: packages/
# key: ${{ runner.os }}-packages-${{ env.TARGET_BRANCH }}
# key: ${{ runner.os }}-packages-${{ hashFiles('packages/**/*') }}
# key: ${{ runner.os }}-packages-${{ hashFiles('packages/*.deb') }}
key: ${{ runner.os }}-packages-${{ hashFiles('**.deb') }}
## TODO: Verify that this works as intended, as we're
## likely getting package updates when we shouldn't be..
- name: Upload Build Artifacts
if: ${{ steps.copy-packages.outputs.update == 'true' }}
timeout-minutes: 15
uses: actions/upload-artifact@v3
with:
name: build-artifacts-${{ env.TARGET_BRANCH }}
#path: ${{ env.OUTPUT_PATH }}
path: output/
retention-days: 30
if-no-files-found: error
- name: Build Apt Repository
if: ${{ (steps.copy-packages.outputs.update == 'true' || steps.cache-packages.outputs.cache-hit != 'true') }}
timeout-minutes: 5
uses: didstopia/simple-apt-repo@master
with:
# timezone: Europe/Helsinki
timezone: UTC
repo-origin: Didstopia CB1 Apt Repo
repo-label: Didstopia BTT CB1 Apt Repository (2023)
repo-version: 0.1.0
repo-description: Didstopia custom apt repository for the BIGTREETECH CB1 hardware, including both stock and custom kernel packages.
key-email: [email protected]
key-public: ${{ secrets.APT_REPO_PUBLIC_KEY }}
key-private: ${{ secrets.APT_REPO_PRIVATE_KEY }}
- name: Generate Repository File Index
if: ${{ (steps.copy-packages.outputs.update == 'true' || steps.cache-packages.outputs.cache-hit != 'true') }}
uses: jayanta525/[email protected]
with:
FOLDER: repo
- name: List Repository Contents
if: ${{ (steps.copy-packages.outputs.update == 'true' || steps.cache-packages.outputs.cache-hit != 'true') }}
run: ls -la ${{ env.REPO_PATH }}/
## TODO: Customize apt repository HTML/JS/CSS here!
- name: Upload Apt Repository
if: ${{ (steps.copy-packages.outputs.update == 'true' || steps.cache-packages.outputs.cache-hit != 'true') }}
timeout-minutes: 15
uses: actions/upload-pages-artifact@v1
with:
path: ${{ env.REPO_PATH }}/
retention-days: 1
# Deploy job
deploy:
name: Deploy
# Add a dependency to the build job
needs: build
# Only run if we got new builds
if: ${{ needs.build.outputs.updated == 'true' }}
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source
# Deploy to the github-pages environment
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
# Specify runner + deployment step
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
- name: Display Deployment URL
run: |
echo "::notice::${{ steps.deployment.outputs.page_url }}"
echo "URL=${{ steps.deployment.outputs.page_url }}" >> $GITHUB_OUTPUT