generated from NebraLtd/hnt_openfleet_template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
437 lines (364 loc) · 16.5 KB
/
build-open-fleet.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
name: Build and Preload OpenFleet
on:
repository_dispatch:
types: [build-open-fleet]
workflow_dispatch:
env:
balena-cli: v16.7.6
raspberrypi3-64: 3.1.1
raspberrypi4-64: 3.1.1
raspberrypicm4-ioboard: 2.112.12
rockpi-4b-rk3399: 2.108.25+rev1
balena-bobcat-px30-image: 2023.07.17
bobcat-px30: 2.115.3
balena-bobcat-rk3566-image: 2023.08.15
bobcat-rk3566: 2.115.3
cloud: nebra-cloud
jobs:
build-open-fleet:
runs-on: ubuntu-20.04
steps:
- name: Export short SHA of commit from upstream trigger
run: |
if [ -z "${{ github.event.client_payload.firmware }}" ]; then
FIRMWARE_SHORT_HASH=$( echo ${GITHUB_SHA:0:7} )
else
FIRMWARE_SHORT_HASH=${{ github.event.client_payload.firmware }}
fi
if [ "${{ github.event.client_payload.branch }}" = "production" ]; then
BRANCH="master"
UPSTREAM_BRANCH="production"
else
BRANCH="testnet"
UPSTREAM_BRANCH="master"
fi
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
echo "UPSTREAM_BRANCH=$UPSTREAM_BRANCH" >> $GITHUB_ENV
echo "FIRMWARE_SHORT_HASH=$FIRMWARE_SHORT_HASH" >> $GITHUB_ENV
echo "Firmware Version: $FIRMWARE_SHORT_HASH"
echo "Branch: $BRANCH"
echo "Upstream Branch: $UPSTREAM_BRANCH"
- uses: actions/checkout@v3
with:
ref: ${{ env.BRANCH }}
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Generate Docker Compose
run: |
mkdir templates
wget https://raw.githubusercontent.com/NebraLtd/helium-miner-software/"${{ env.UPSTREAM_BRANCH }}"/settings.ini
wget https://raw.githubusercontent.com/NebraLtd/helium-miner-software/"${{ env.UPSTREAM_BRANCH }}"/requirements.txt
wget https://raw.githubusercontent.com/NebraLtd/helium-miner-software/"${{ env.UPSTREAM_BRANCH }}"/gen_docker_compose_by_variant.py
wget https://raw.githubusercontent.com/NebraLtd/helium-miner-software/"${{ env.UPSTREAM_BRANCH }}"/templates/docker-compose.template -O templates/docker-compose.template
pip install -r requirements.txt
rm -f docker-compose.yml
miner="$(echo ${{ github.event.repository.name }} | cut -d '-' -f2-)"
echo "Repo: $miner"
if [[ "$miner" =~ "nebra" ]] || [[ "$miner" =~ "bobcat" ]] ; then
variant="$miner"
elif [[ "$miner" == "og" ]] ; then
variant="helium-fl1"
else
variant="$miner-fl1"
fi
echo "VARIANT=$variant" >> $GITHUB_ENV
echo "Variant: $variant"
FIRMWARE_SHORT_HASH=${{ env.FIRMWARE_SHORT_HASH }}
python gen_docker_compose_by_variant.py "$variant" -o docker-compose.yml
- name: Determine if Raspberry Pi 3
if: env.BRANCH == 'master'
run: |
repo="${{ github.event.repository.name }}"
if grep -q "outdoor2" <<<"$repo"; then
echo "Not RasPi 3 (ROCK Pi 4B)"
RASPI3="NO"
ROCKPI="YES"
elif grep -q "indoor2" <<<"$repo"; then
echo "Not RasPi 3 (ROCK Pi 4B)"
RASPI3="NO"
ROCKPI="YES"
elif grep -q "outdoor1" <<<"$repo"; then
echo "RasPi 3 Outdoor"
RASPI3="YES"
ROCKPI="NO"
elif grep -q "indoor1" <<<"$repo"; then
echo "RasPi 3 Indoor"
RASPI3="YES"
ROCKPI="NO"
elif grep -q "risinghf" <<<"$repo"; then
echo "RisingHF"
RASPI3="YES"
ROCKPI="NO"
elif grep -q "rkcm3" <<<"$repo"; then
echo "Not RasPi 3 (ROCK CM3)"
RASPI3="NO"
ROCKPI="YES"
else
echo "Not RasPi 3 (Other)"
RASPI3="NO"
ROCKPI="NO"
fi
echo "RASPI3=$RASPI3" >> $GITHUB_ENV
echo "ROCKPI=$ROCKPI" >> $GITHUB_ENV
- name: Random sleep (balena API workaround)
if: env.BRANCH == 'master'
run: sleep $[ ( $RANDOM % 540 ) + 1 ]s
- name: Install latest balena-cli
if: env.BRANCH == 'master'
run: |
sudo apt-get update
sudo apt-get install -y curl unzip zip
cd /opt/
echo "Uses Balena CLI version: ${{ env.balena-cli }}"
curl -O -sSL "https://github.com/balena-io/balena-cli/releases/download/${{ env.balena-cli }}/balena-cli-${{ env.balena-cli }}-linux-x64-standalone.zip"
unzip balena-cli-*-linux-x64-standalone.zip
sudo ln -s /opt/balena-cli/balena /usr/bin/
cd ~
- name: Download Bobcat PX30 base Balena OS image
uses: dsaltares/fetch-gh-release-asset@master
if: github.event.repository.name == 'helium-bobcat-px30' && env.BRANCH == 'master'
with:
repo: 'NebraLtd/balena-bobcat-px30'
version: 'tags/${{ env.balena-bobcat-px30-image }}'
file: 'balena-image-bobcat-px30-${{ env.balena-bobcat-px30-image }}.img.zip'
target: 'images/balena-base-${{ github.event.repository.name }}.img.zip'
token: ${{ secrets.MR_BUMP }}
- name: Download Bobcat RK3566 base Balena OS image
uses: dsaltares/fetch-gh-release-asset@master
if: github.event.repository.name == 'helium-bobcat-rk3566' && env.BRANCH == 'master'
with:
repo: 'NebraLtd/balena-bobcat-rk3566'
version: 'tags/${{ env.balena-bobcat-rk3566-image }}'
file: 'balena-image-bobcat-rk3566-${{ env.balena-bobcat-rk3566-image }}.img.zip'
target: 'images/balena-base-${{ github.event.repository.name }}.img.zip'
token: ${{ secrets.MR_BUMP }}
- name: Generate a Balena OS image for the given board, variant, frequency
if: env.BRANCH == 'master'
run: |
LATEST_GA=${{ github.event.client_payload.tag }}
if [ "${LATEST_GA}" != "" ] ; then
echo "New release pushed to main repo: $LATEST_GA"
echo "LATEST_GA=$LATEST_GA" >> $GITHUB_ENV
else
echo "No release version provided! Applying tag TEST"
echo "LATEST_GA=TEST" >> $GITHUB_ENV
fi
IMAGE_BASE_PATH="$(pwd)/images"
DATE=$(date +%F)
USER=""
NODRYRUN=true
VERBOSE=true
# Make the base path for the generated images.
mkdir -p "$IMAGE_BASE_PATH"
# Print the parameters for generating images.
if [[ $USER != "" ]]; then
echo " - user: ${USER}"
fi
if [[ $NODRYRUN == false ]]; then
echo "(Dryrun mode enabled)"
fi
if $VERBOSE ; then
echo "(Verbose mode enabled)"
fi
# Download balena OS
if [ ${{ env.RASPI3 }} == 'YES' ] ; then
balena_os="raspberrypi3-64"
balena_os_version="${{ env.raspberrypi3-64 }}"
elif [ ${{ github.event.repository.name }} == 'helium-syncrobit' ] || [ ${{ github.event.repository.name }} == 'helium-linxdot' ] || [ ${{ github.event.repository.name }} == 'helium-pycom' ] || [ ${{ github.event.repository.name }} == 'helium-midas' ] ; then
balena_os="raspberrypicm4-ioboard"
balena_os_version="${{ env.raspberrypicm4-ioboard }}"
elif [ ${{ github.event.repository.name }} == 'helium-bobcat-px30' ] ; then
balena_os="isg-503"
balena_os_version="${{ env.bobcat-px30 }}"
elif [ ${{ github.event.repository.name }} == 'helium-bobcat-rk3566' ] ; then
balena_os="rockpro64"
balena_os_version="${{ env.bobcat-rk3566 }}"
elif [ ${{ env.ROCKPI }} == 'YES' ] ; then
balena_os="rockpi-4b-rk3399"
balena_os_version="${{ env.rockpi-4b-rk3399 }}"
else
balena_os="raspberrypi4-64"
balena_os_version="${{ env.raspberrypi4-64 }}"
fi
base_image_path="$IMAGE_BASE_PATH/balena-base-${{ github.event.repository.name }}.img"
if $VERBOSE ; then
echo -e "Downloading image ${balena_os} for version ${balena_os_version} and writing to ${base_image_path}"
fi
if $NODRYRUN ; then
if [ ${{ github.event.repository.name }} != 'helium-bobcat-px30' ] && [ ${{ github.event.repository.name }} != 'helium-bobcat-rk3566' ]; then
# Download the base image
balena os download "$balena_os" \
-o "$base_image_path" \
--version "$balena_os_version"
else
unzip $IMAGE_BASE_PATH/*.zip -d $IMAGE_BASE_PATH/
mv ${IMAGE_BASE_PATH}/*.img ${base_image_path}
fi
fi
echo "IMAGE_BASE_PATH=$IMAGE_BASE_PATH" >> $GITHUB_ENV
echo "DATE=$DATE" >> $GITHUB_ENV
echo "base_image_path=$base_image_path" >> $GITHUB_ENV
echo "balena_os_version=$balena_os_version" >> $GITHUB_ENV
- name: Balena login
if: env.BRANCH == 'master'
run: |
if [ "${{ env.cloud }}" == "nebra-cloud" ]; then
echo "balenaUrl: cloud.nebra.com" > ~/.balenarc.yml
balena login --token ${{ secrets.NEBRAOS_CLOUD_TOKEN }} > /dev/null
else
if [ ${{ env.RASPI3 }} == 'YES' ] ; then
balena login --token ${{ secrets.BALENA_API_TOKEN }} > /dev/null
elif [ ${{ github.event.repository.name }} == 'helium-syncrobit' ] || [ ${{ github.event.repository.name }} == 'helium-linxdot' ] || [ ${{ github.event.repository.name }} == 'helium-pycom' ] || [ ${{ github.event.repository.name }} == 'helium-midas' ] ; then
balena login --token ${{ secrets.BALENA_API_TOKEN_2 }} > /dev/null
elif [ ${{ env.ROCKPI }} == 'YES' ] ; then
balena login --token ${{ secrets.BALENA_API_TOKEN_3 }} > /dev/null
else
balena login --token ${{ secrets.BALENA_API_TOKEN_4 }} > /dev/null
fi
fi
- name: Configure Balena OS image for the given board, variant, frequency
if: env.BRANCH == 'master'
run: |
# Generate image path for the given board, variant, frequency and user
fleet_name="${{ github.event.repository.name }}"
image_name="${fleet_name}-${{ env.DATE }}"
image_path="${{ env.IMAGE_BASE_PATH }}/${image_name}.img"
# Minimum output
echo "Generating image for ${fleet_name} fleet"
if $VERBOSE ; then
echo "Image path is ${image_path}"
fi
# Configure balena OS image
if $VERBOSE ; then
echo "Configuring image"
fi
# Copy base balena image to target image
if $NODRYRUN ; then
cp "${{ env.base_image_path }}" "$image_path"
fi
if $NODRYRUN ; then
if [ ${{ github.event.repository.name }} != 'helium-bobcat-px30' ] && [ ${{ github.event.repository.name }} != 'helium-bobcat-rk3566' ]; then
balena os configure "$image_path" \
--fleet "nebraltd/$fleet_name" \
--config-network ethernet \
--version "${{ env.balena_os_version }}"
else
sudo balena config inject ${GITHUB_WORKSPACE}/config.json --drive "${image_path}"
fi
fi
if $VERBOSE ; then
echo "Pre-loading apps"
fi
# Preload balena OS image
if $NODRYRUN ; then
balena preload "$image_path" \
--fleet "nebraltd/$fleet_name" \
-c "current"
fi
if $VERBOSE ; then
echo "Adding ssh keys to the image"
fi
if $NODRYRUN ; then
final_json_filename=/tmp/final.json
ssh_public_keys=$(cat ssh_public_keys.json)
sudo balena config read --drive "$image_path" -j \
| jq ". += $ssh_public_keys" > $final_json_filename
sudo balena config inject $final_json_filename --drive "$image_path"
fi
if [ ${{ github.event.repository.name }} == 'helium-bobcat-px30' ] || [ ${{ github.event.repository.name }} == 'helium-bobcat-rk3566' ]; then
# Overwrite device type from bobcat specific to balena specific
if $VERBOSE ; then
echo "Overwriting device-type"
fi
if $NODRYRUN ; then
sudo losetup -P -f "${image_path}"
loop_dev=$(losetup -l | grep $(basename "${image_path}") | cut -d ' ' -f 1)
tmpdir=$(mktemp -d)
sudo mount ${loop_dev}p1 ${tmpdir}
if [ ${{ github.event.repository.name }} == 'helium-bobcat-px30' ]; then
sudo sed -i 's/"slug": "bobcat-px30"/"slug": "isg-503"/' ${tmpdir}/device-type.json
elif [ ${{ github.event.repository.name }} == 'helium-bobcat-rk3566' ]; then
sudo sed -i 's/"slug": "bobcat-rk3566"/"slug": "rockpro64"/' ${tmpdir}/device-type.json
fi
sudo umount ${tmpdir}
sudo losetup -d ${loop_dev}
fi
fi
# Copy configuration files into boot and root partition of the balena image
if $VERBOSE ; then
echo "Copying configuration files..."
fi
if $NODRYRUN ; then
if [ -f "./config.txt" ] ; then
echo "Config.txt file found..."
mountpoints=/tmp/balena-image-mountpoints
balena_image_boot_mnt=${mountpoints}/resin-boot
# Setup mountpoints
sudo rm -rf $mountpoints > /dev/null 2>&1 || true
sudo mkdir -p $balena_image_boot_mnt
balena_image_loop_dev="$(sudo losetup -fP --show "$image_path")"
sudo mount "${balena_image_loop_dev}p1" "$balena_image_boot_mnt"
sudo cp "./config.txt" "$balena_image_boot_mnt/config.txt"
sleep 5 # Waits 5 seconds.
# Cleanup
sudo umount "$balena_image_boot_mnt"
sudo losetup -d "$balena_image_loop_dev"
else
echo "Config.txt file not found..."
fi
fi
# Compress Balena image file and generate SHA512 checksum file
# Make the compressed file name from the image file name
compressed_image_path=${image_path/img/zip}
if $VERBOSE ; then
echo "Compressing image"
fi
if $NODRYRUN ; then
zip -9 -j "$compressed_image_path" "$image_path"
fi
if $VERBOSE ; then
echo "Generating checksum"
fi
if $NODRYRUN ; then
sha512sum "$compressed_image_path" > "$compressed_image_path.sha512"
fi
# Export image name and compressed image path to the environment
export IMAGE_NAME="$image_name"
export COMPRESSED_IMAGE_PATH="$compressed_image_path"
echo "Image Name: ${IMAGE_NAME}"
echo "Compressed Image Path: ${COMPRESSED_IMAGE_PATH}"
echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV
echo "COMPRESSED_IMAGE_PATH=${COMPRESSED_IMAGE_PATH}" >> $GITHUB_ENV
- name: Set date
if: env.BRANCH == 'master'
run: |
echo "timestamp=$(date +'%s')" >> $GITHUB_ENV
echo "day=$(date +'%F')" >> $GITHUB_ENV
- name: Copy OS image file and checksum to final location
if: env.BRANCH == 'master'
run: |
cp "${{ env.COMPRESSED_IMAGE_PATH }}" "${{ env.IMAGE_NAME }}.zip"
cp "${{ env.COMPRESSED_IMAGE_PATH }}.sha512" "${{ env.IMAGE_NAME }}.zip.sha512"
- name: Upload image file and checksum to release
if: env.BRANCH == 'master'
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ${{ env.IMAGE_NAME }}.*
tag: ${{ env.LATEST_GA }}-${{ env.IMAGE_NAME }}-OpenFleet
overwrite: true
file_glob: true
- name: Push updated docker compose file
uses: test-room-7/action-update-file@v1
with:
branch: ${{ env.BRANCH }}
file-path: docker-compose.yml
commit-msg: Update ${{ env.VARIANT }} variant docker compose output (${{ env.BRANCH }})
github-token: ${{ secrets.MR_BUMP }}
- name: Trigger Balena App build action in ${{ github.event.repository.name }} repo
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.MR_BUMP }}
event-type: build-balena-app
repository: ${{ github.repository }}