forked from pguyot/arm-runner-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
309 lines (308 loc) · 12.2 KB
/
action.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
name: 'ARM runner'
description: 'Composite action to run commands within a qemu chrooted environment'
inputs:
base_image:
description: 'System base image'
required: true
default: 'raspios_lite:latest'
image_additional_mb:
description: 'Additional MB for image'
required: false
default: 0
bind_mount_repository:
description: 'Bind mount repository instead of copying it'
required: false
default: 'no'
cpu:
description: 'CPU architecture, arm1176 for arm6vl (Pi Zero)'
required: false
default: 'arm1176:cortex-a53'
cpu_info:
description: 'Path to cpu info file to be mounted on /proc/cpuinfo'
required: false
default: ''
commands:
description: 'Commands to run in the image, executed with /bin/sh'
required: true
default: 'uname -a'
copy_artifact_path:
description: 'Paths of artifacts to copy from inside the image on completion'
required: false
copy_artifact_dest:
description: 'Where to copy artifacts on completion'
required: false
default: '.'
copy_repository_path:
description: 'Where to copy repository path before running commands'
required: false
default: ''
optimize_image:
description: 'Optimize image during final cleanup'
required: false
default: 'yes'
use_systemd_nspawn:
description: 'Use systemd-nspawn instead of chroot'
required: false
default: 'no'
systemd_nspawn_options:
description: 'Options passed to systemd-nspawn, e.g. -E CI=${CI}'
required: false
default: ''
bootpartition:
description: 'Partition number for boot (/boot), empty means none'
required: false
default: '1'
rootpartition:
description: 'Partition number for root (/)'
required: false
default: '2'
shell:
description: 'Path to shell to run the commands with'
required: false
default: '/bin/sh'
shell_package:
description: 'The shell package to install, if different from shell'
required: false
default: ''
user:
description: 'User to run commands on the image. Must exist in the image'
required: false
default: ''
exit_on_fail:
description: 'Exit immediately if a command exits with a non-zero status'
required: false
default: 'yes'
copy_artifacts_on_fail:
description: 'Copy artifacts if a command exits with a non-zero status'
required: false
default: 'no'
debug:
description: 'Display commands as they are executed'
required: false
default: 'yes'
import_github_env:
description: 'Imports $GITHUB_ENV variables into the image environment'
required: false
default: 'no'
export_github_env:
description: 'Exports $GITHUB_ENV from the image environment to subsequent tasks'
required: false
default: 'no'
outputs:
image:
description: "Path to image"
value: ${{ steps.download_image.outputs.image }}
runs:
using: "composite"
steps:
- name: Install dependencies
run:
sudo apt-get update && sudo apt-get install -y qemu qemu-user-static binfmt-support parted wget dosfstools zip
shell: bash
- name: Install qemu wrapper
shell: bash
run: |
case ${{ inputs.cpu }} in
"")
cpu="arm1176:cortex-a53"
;;
arm1176)
cpu="arm1176:cortex-a53"
;;
cortex-a7)
cpu="cortex-a7:cortex-a53"
;;
cortex-a8)
cpu="cortex-a8:max"
;;
cortex-a53)
cpu="max:cortex-a53"
;;
*)
cpu=${{ inputs.cpu }}
esac
arm_cpu=${cpu%:*}
aarch64_cpu=${cpu#*:}
sudo gcc -static ${{ github.action_path }}/qemu-wrapper.c -DQEMU_CPU=${arm_cpu} -O3 -s -o /usr/bin/qemu-arm-static0
sudo gcc -static ${{ github.action_path }}/qemu-wrapper.c -DQEMU_CPU=${aarch64_cpu} -O3 -s -o /usr/bin/qemu-aarch64-static0
sudo update-binfmts --unimport qemu-arm
sudo update-binfmts --unimport qemu-aarch64
sudo update-binfmts --package arm-runner-action --install arm-runner-action-qemu-arm /usr/bin/qemu-arm-static0 --magic '\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' --credentials yes --fix-binary yes
sudo update-binfmts --package arm-runner-action --install arm-runner-action-qemu-aarch64 /usr/bin/qemu-aarch64-static0 --magic '\x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff' --credentials yes --fix-binary yes
- name: Download base image
run: |
bash ${{ github.action_path }}/download_image.sh ${{ inputs.base_image }}
shell: bash
id: download_image
- name: Mount and optionally resize image
run: |
sudo --preserve-env=GITHUB_OUTPUT bash ${{ github.action_path }}/mount_image.sh ${{ steps.download_image.outputs.image }} ${{ inputs.image_additional_mb }} ${{ inputs.use_systemd_nspawn }} ${{ inputs.rootpartition }} ${{ inputs.bootpartition }}
shell: bash
id: mount_image
- name: Mount CPU info
if: ${{ inputs.cpu_info != '' }}
run: |
if [ -e ${{ github.workspace }}/${{ inputs.cpu_info }} ]; then
cpu_info_path=${{ github.workspace }}/${{ inputs.cpu_info }}
else
cpu_info_path=${{ github.action_path }}/${{ inputs.cpu_info }}
fi
sudo mount --bind ${cpu_info_path} ${{ steps.mount_image.outputs.mount }}/proc/cpuinfo
shell: bash
- name: Run commands
id: runcmd
run: |
case "${{ inputs.debug }}" in
yes|true)
debug='x'
set -x
;;
no|false)
debug=''
;;
esac
repository_path=${{ inputs.copy_repository_path }}
if [ "${repository_path}x" = "x" ]; then
repository_name=`basename ${{ github.workspace }}`
repository_path=/${repository_name}
fi
case "${{ inputs.exit_on_fail }}" in
yes|true)
exit_on_fail='e'
;;
no|false)
exit_on_fail=''
;;
esac
shell_with_opts="${{ inputs.shell }}"
shell=${shell_with_opts%% *}
if [ "${shell}" != "${shell_with_opts}" ]; then
shell_opts=${shell_with_opts#* }
else
shell_opts=""
fi
if [ "${{ inputs.user }}" != "" ]; then
if [ "${{ inputs.use_systemd_nspawn }}x" != "x" -a "${{ inputs.use_systemd_nspawn }}x" != "nox" ]; then
user_opt="-u ${{ inputs.user }}"
else
user_opt="--userspec=${{ inputs.user }}"
fi
else
user_opt=""
fi
shell_package=${{ inputs.shell_package }}
[ -x ${{ steps.mount_image.outputs.mount }}/${shell} ] || \
shell_path=$(sudo chroot ${{ steps.mount_image.outputs.mount }} /bin/sh -c "command -v ${shell}") || \
case ${shell} in
bash|sh)
:
;;
*)
sudo chroot ${{ steps.mount_image.outputs.mount }} sudo apt-get install -y ${shell_package:-${shell##*/}}
;;
esac
shell_path=${shell_path:-$(sudo chroot ${{ steps.mount_image.outputs.mount }} /bin/sh -c "command -v ${shell}")}
sudo mkdir -p $(dirname ${{ steps.mount_image.outputs.mount }}${repository_path})
case ${{ inputs.bind_mount_repository }} in
yes|true)
sudo mkdir -p ${{ steps.mount_image.outputs.mount }}${repository_path}
sudo mount --bind ${{ github.workspace }} ${{ steps.mount_image.outputs.mount }}${repository_path}
;;
no|false)
sudo cp -Rp ${{ github.workspace }} ${{ steps.mount_image.outputs.mount }}${repository_path}
;;
esac
if [ "${{ inputs.use_systemd_nspawn }}x" != "x" -a "${{ inputs.use_systemd_nspawn }}x" != "nox" ]; then
chroot_script_dir=/scripts
script_dir=${RUNNER_TEMP:-/home/actions/temp}/scripts
else
chroot_script_dir=/tmp
script_dir=${{ steps.mount_image.outputs.mount }}/tmp
fi
mkdir -p ${script_dir}
script_path=${script_dir}/commands.sh
touch ${script_path}
if [ "${debug}${exit_on_fail}" != "" ]; then
shell_basename=`basename ${shell_path}`
if [ "${shell_basename}" = "sh" -o "${shell_basename}" = "bash" -o "${shell_basename}" = "ksh" -o "${shell_package}" = "ksh" ]; then
echo "set -${debug}${exit_on_fail}" >> ${script_path}
fi
fi
case "${{ inputs.import_github_env }}" in
yes|true)
export | sed -e 's/^declare -x //g;s/^[^=]*$/\0=""/g;s/='\''\(.*\)'\''$/=\1/g' >> ${script_dir}/environment.sh
echo ". ${chroot_script_dir}/environment.sh" >> ${script_path}
;;
esac
case "${{ inputs.export_github_env }}" in
yes|true)
echo "GITHUB_ENV=${chroot_script_dir}/github_env.sh" >> ${script_path}
;;
esac
echo "cd ${repository_path}" >> ${script_path}
cat >> ${script_path} <<"ARM_RUNNER_INPUT_COMMANDS_EOF"
${{ inputs.commands }}
ARM_RUNNER_INPUT_COMMANDS_EOF
if [ "${{ inputs.use_systemd_nspawn }}x" != "x" -a "${{ inputs.use_systemd_nspawn }}x" != "nox" ]; then
sudo apt-get install -y systemd-container
sudo -E systemd-nspawn ${user_opt} -q -a --bind=${script_dir}:${chroot_script_dir} -D ${{ steps.mount_image.outputs.mount }} ${{ inputs.systemd_nspawn_options }} ${shell_path} ${shell_opts} ${chroot_script_dir}/commands.sh
else
sudo -E chroot ${user_opt} ${{ steps.mount_image.outputs.mount }} ${shell_path} ${shell_opts} ${chroot_script_dir}/commands.sh
fi
rc=$?
[ -f ${script_dir}/github_env.sh ] && \
cat ${script_dir}/github_env.sh >> $GITHUB_ENV
exit $rc
shell: bash
- name: Copy artifacts within image
if: ${{ always() && !cancelled() && (inputs.copy_artifacts_on_fail == 'yes' || steps.runcmd.conclusion == 'success') }}
run: |
case "${{ inputs.debug }}" in
yes|true)
set -x
;;
esac
repository_path=${{ inputs.copy_repository_path }}
if [ "${repository_path}x" = "x" ]; then
repository_name=`basename ${{ github.workspace }}`
repository_path=/${repository_name}
fi
if [ "${{ inputs.copy_artifact_path }}x" != "x" ] && [ "${{ inputs.copy_artifact_dest }}x" != "x" ]; then
while read -d\; copy_artifact_path; do
[ -z "${copy_artifact_path}" ] || sudo cp -Rp ${{ steps.mount_image.outputs.mount }}${repository_path}/${copy_artifact_path} ${{ inputs.copy_artifact_dest }}
done <<< "${{ inputs.copy_artifact_path }};"
fi
shell: bash
- name: Unmount CPU info
if: ${{ always() && inputs.cpu_info != '' }}
run: |
sudo umount ${{ steps.mount_image.outputs.mount }}/proc/cpuinfo
shell: bash
- name: Cleanup image
if: always()
run: |
case ${{ inputs.bind_mount_repository }} in
yes|true)
repository_path=${{ inputs.copy_repository_path }}
if [ "${repository_path}x" = "x" ]; then
repository_name=`basename ${{ github.workspace }}`
repository_path=/${repository_name}
fi
sudo umount ${{ steps.mount_image.outputs.mount }}${repository_path}
;;
esac
sudo bash ${{ github.action_path }}/cleanup_image.sh ${{ steps.mount_image.outputs.loopdev }} ${{ steps.mount_image.outputs.mount }} ${{ steps.download_image.outputs.image }} "${{ inputs.optimize_image }}" "${{ inputs.rootpartition }}"
shell: bash
- name: Restore qemu wrapper
if: always()
shell: bash
run: |
sudo update-binfmts --package arm-runner-action --remove arm-runner-action-qemu-arm /usr/bin/qemu-arm-static0
sudo update-binfmts --package arm-runner-action --remove arm-runner-action-qemu-aarch64 /usr/bin/qemu-aarch64-static0
sudo rm /usr/bin/qemu-arm-static0
sudo rm /usr/bin/qemu-aarch64-static0
sudo update-binfmts --import qemu-arm
sudo update-binfmts --import qemu-aarch64
branding:
icon: 'cpu'
color: 'purple'