-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
359 lines (285 loc) · 12 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
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
# Tutorial to use with: https://github.com/andry81-devops/github-action-extensions
#
name: check-os-version
description: 'GitHub composite action to check and read OS version details into variables'
branding:
icon: info
color: gray
inputs:
gh-var-tags:
description: GitHub variables to return as list of tags
required: false
# example:
# ```yml
# gh-var-tags: runner-os ...
# ```
linux-files:
description: Linux files to read as list of path lines separated by line return
required: false
# example:
# ```yml
# linux-files: |
# /etc/os-release ...
# ```
linux-commands:
description: Linux commands to execute as list of lines with quote characters
required: false
# example:
# ```yml
# linux-commands: |
# echo "$"
# lsb_release -a
# hostnamectl
# uname -r
# ...
# ```
outputs:
json-return-values:
description: Escaped and encoded values in JSON format
value: ${{ steps.return-values.outputs.json-return-values }}
# examples:
# `"gh-vars": [ { "tag": "runner-os", "name": "runner.os", "value": "<escaped-and-encoded-variable-value>" }, ...]`
# `"linux-files": [ { "file": "/etc/os-version", "value": "<escaped-and-encoded-file-content>", "err": "<escaped-and-encoded-file-read-err>" }, ...]`
# `"linux-commands": [ { "line": "lsb_release -a", "command": "lsb_release", "args": [ "-a" ], "out": "<escaped-and-encoded-command-out>" }, "err": "<escaped-and-encoded-command-err>" }, ...]`
#
# , where `<escaped-and-encoded-file-content>` has these character escaping rules in predefined order:
# \ -> \\
# " -> \"
# % -> %25
# \n -> %0A
# \r -> %0D
#
runs:
using: "composite"
steps:
- name: '[check-os-version][01] check GitHub variables'
id: check-gh-vars
shell: bash
run: |
### [check-os-version][01] check GitHub variables
IFS=$' \t' gh_var_tags=( ${{ inputs.gh-var-tags }} )
prev_return_values="$JSON_RETURN_VALUES"
JSON_RETURN_VALUES=''
if [[ -n "$prev_return_values" ]]; then
had_values=1
else
had_values=0
fi
index=0
IFS=$' \t'; for gh_var_tag in "${gh_var_tags[@]}"; do
gh_var_value=''
gh_var_name=''
case "$gh_var_tag" in
runner-os)
gh_var_name='runner.os'
# suppress not zero exit code
{ IFS=$' \t' read -r -d '' gh_var_value || (( 1 )); } << ::EOF::
${{ runner.os }}
::EOF::
# trim trailing line returns
gh_var_value="$(echo "$gh_var_value")"
;;
esac
if [[ -z "$gh_var_value" ]]; then
continue
fi
if (( index )); then
JSON_RETURN_VALUES="$JSON_RETURN_VALUES, "
fi
# escaping
gh_var_value="${gh_var_value//\\/\\\\}"
gh_var_value="${gh_var_value//\"/\\\"}"
gh_var_value="${gh_var_value//%/%25}"
gh_var_value="${gh_var_value//$'\n'/%0A}"
gh_var_value="${gh_var_value//$'\r'/%0D}"
JSON_RETURN_VALUES="$JSON_RETURN_VALUES{ \"tag\": \"$gh_var_tag\", \"name\": \"$gh_var_name\", \"value\": \"$gh_var_value\" }"
# suppress not zero exit code
(( index++, 1 ))
done
if (( index )); then
if (( had_values )); then
prev_return_values="$prev_return_values, "
fi
JSON_RETURN_VALUES="$prev_return_values\"gh-vars\": [ $JSON_RETURN_VALUES ]"
echo "JSON_RETURN_VALUES=$JSON_RETURN_VALUES" >> "$GITHUB_ENV"
fi
- name: '[check-os-version][02] check Linux files'
id: check-linux-files
shell: bash
run: |
### [check-os-version][02] check Linux files
linux_file_paths=()
prev_return_values="$JSON_RETURN_VALUES"
JSON_RETURN_VALUES=''
if [[ -n "$prev_return_values" ]]; then
had_values=1
else
had_values=0
fi
# suppress not zero exit code
{ IFS=$' \t' read -r -d '' linux_file_paths_text || (( 1 )); } << ::EOF::
${{ inputs.linux-files }}
::EOF::
# trim trailing line returns
linux_file_paths_text="$(echo "$linux_file_paths_text")"
while IFS=$'\n' read -r linux_file_path; do
if [[ -n "${linux_file_path//$' \t'/}" ]]; then
linux_file_paths[${#linux_file_paths[@]}]="$linux_file_path"
fi
done <<< "$linux_file_paths_text"
index=0
IFS=$' \t'; for linux_file_path in "${linux_file_paths[@]}"; do
linux_file_value=''
echo ">$linux_file_path"
# disabled because sometimes fails w/o reason
#if ! which "$linux_file_path"; then
# continue
#fi
if (( index )); then
JSON_RETURN_VALUES="$JSON_RETURN_VALUES, "
fi
# suppress not zero exit code
linux_file_value="$(<"$linux_file_path")" 2> "${{ runner.temp }}/err.txt" || (( 1 ))
if [[ -n "$linux_file_value" ]]; then
# escaping
linux_file_value="${linux_file_value//\\/\\\\}"
linux_file_value="${linux_file_value//\"/\\\"}"
linux_file_value="${linux_file_value//%/%25}"
linux_file_value="${linux_file_value//$'\n'/%0A}"
linux_file_value="${linux_file_value//$'\r'/%0D}"
fi
file_read_err="$(<"${{ runner.temp }}/err.txt")"
if [[ -n "$file_read_err" ]]; then
echo "$file_read_err"$'\n---'
# escaping
file_read_err="${file_read_err//\\/\\\\}"
file_read_err="${file_read_err//%/%25}"
file_read_err="${file_read_err//$'\n'/%0A}"
file_read_err="${file_read_err//\"/\\\"}"
file_read_err="${file_read_err//$'\r'/%0D}"
fi
JSON_RETURN_VALUES="$JSON_RETURN_VALUES{ \"file\": \"$linux_file_path\", \"value\": \"$linux_file_value\", \"err\": \"$file_read_err\" }"
# suppress not zero exit code
(( index++, 1 ))
done
if (( index )); then
if (( had_values )); then
prev_return_values="$prev_return_values, "
fi
JSON_RETURN_VALUES="$prev_return_values\"linux-files\": [ $JSON_RETURN_VALUES ]"
echo "JSON_RETURN_VALUES=$JSON_RETURN_VALUES" >> "$GITHUB_ENV"
fi
- name: '[check-os-version][03] check Linux commands'
id: check-linux-commands
shell: bash
run: |
### [check-os-version][03] check Linux commands
linux_command_lines=()
prev_return_values="$JSON_RETURN_VALUES"
JSON_RETURN_VALUES=''
if [[ -n "$prev_return_values" ]]; then
had_values=1
else
had_values=0
fi
# suppress not zero exit code
{ IFS=$' \t' read -r -d '' linux_command_lines_text || (( 1 )); } << ::EOF::
${{ inputs.linux-commands }}
::EOF::
# trim trailing line returns
linux_command_lines_text="$(echo "$linux_command_lines_text")"
while IFS=$'\n' read -r linux_command_line; do
if [[ -n "${linux_command_line//$' \t'/}" ]]; then
linux_command_lines[${#linux_command_lines[@]}]="$linux_command_line"
fi
done <<< "$linux_command_lines_text"
index=0
IFS=$' \t'; for linux_command_line in "${linux_command_lines[@]}"; do
linux_command=''
linux_command_out=''
check_which=0
case "$linux_command_line" in
lsb_release | lsb_release[$' \t']*)
linux_command='lsb_release'
check_which=1
;;
hostnamectl | hostnamectl[$' \t']*)
linux_command='hostnamectl'
check_which=1
;;
uname | uname[$' \t']*)
linux_command='uname'
check_which=1
;;
echo | echo[$' \t']*)
linux_command='echo'
;;
esac
if [[ -z "$linux_command" ]]; then
continue
fi
echo ">$linux_command_line"
# disabled because sometimes fails w/o reason
#if (( check_which )) && ! which "$linux_command"; then
# continue
#fi
if (( index )); then
JSON_RETURN_VALUES="$JSON_RETURN_VALUES, "
fi
eval "linux_command_args=( "${linux_command_line#*[$' \t']}" )"
# suppress not zero exit code
"$linux_command" "${linux_command_args[@]}" > "${{ runner.temp }}/out.txt" 2> "${{ runner.temp }}/err.txt" || (( 1 ))
linux_command_out="$(<"${{ runner.temp }}/out.txt")"
linux_command_err="$(<"${{ runner.temp }}/err.txt")"
if [[ -n "$linux_command_err" ]]; then
echo "$linux_command_err"$'\n---'
# escaping
linux_command_err="${linux_command_err//\\/\\\\}"
linux_command_err="${linux_command_err//\"/\\\"}"
linux_command_err="${linux_command_err//%/%25}"
linux_command_err="${linux_command_err//$'\n'/%0A}"
linux_command_err="${linux_command_err//$'\r'/%0D}"
fi
if [[ -n "$linux_command_out" ]]; then
# escaping
linux_command_out="${linux_command_out//\\/\\\\}"
linux_command_out="${linux_command_out//\"/\\\"}"
linux_command_out="${linux_command_out//%/%25}"
linux_command_out="${linux_command_out//$'\n'/%0A}"
linux_command_out="${linux_command_out//$'\r'/%0D}"
fi
linux_command_line="${linux_command_line//\\/\\\\}"
linux_command_line="${linux_command_line//\"/\\\"}"
JSON_RETURN_VALUES="$JSON_RETURN_VALUES{ \"line\": \"$linux_command_line\", \"command\": \"$linux_command\", \"args\": [ "
index2=0
IFS=$' \t'; for arg in "${linux_command_args[@]}"; do
if (( index2 )); then
JSON_RETURN_VALUES="$JSON_RETURN_VALUES, "
fi
if [[ -n "$arg" ]]; then
# escaping
arg="${arg//\\/\\\\}"
arg="${arg//\"/\\\"}"
JSON_RETURN_VALUES="$JSON_RETURN_VALUES\"$arg\""
fi
# suppress not zero exit code
(( index2++, 1 ))
done
JSON_RETURN_VALUES="$JSON_RETURN_VALUES ], \"out\": \"$linux_command_out\", \"err\": \"$linux_command_err\" }"
# suppress not zero exit code
(( index++, 1 ))
done
if (( index )); then
if (( had_values )); then
prev_return_values="$prev_return_values, "
fi
JSON_RETURN_VALUES="$prev_return_values\"linux-commands\": [ $JSON_RETURN_VALUES ]"
echo "JSON_RETURN_VALUES=$JSON_RETURN_VALUES" >> "$GITHUB_ENV"
fi
- name: '[check-os-version][04] return values'
id: return-values
shell: bash
run: |
### [check-os-version][04] return values
JSON_RETURN_VALUES="{ $JSON_RETURN_VALUES }"
echo "json-return-values=$JSON_RETURN_VALUES" >> "$GITHUB_OUTPUT"