-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaml-to-env.sh
executable file
·70 lines (55 loc) · 1.94 KB
/
yaml-to-env.sh
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
#!/usr/bin/env bash
# sourced from https://stackoverflow.com/a/17841619
function _join_by { local IFS="$1"; shift; echo "$*"; }
function _trim() { printf '%s' "${1}" | sed 's/^[[:blank:]]*//; s/[[:blank:]]*$//'; }
function _ltrim_one { printf '%s' "${1}" | sed 's/^[[:blank:]]//'; }
function _rtrim_one { printf '%s' "${1}" | sed 's/[[:blank:]]$//'; }
function _upper() { printf '%s' "${1}" | tr '[:lower:]' '[:upper:]'; }
function _env_name() { _upper "${1}" | sed -E 's/[^a-zA-Z0-9_]/_/g'; }
_write_env_file_line() {
printf '%s' "${1}" | sed -e 's/\\n/\n/g' >> "$GITHUB_ENV"
printf '\n' >> "$GITHUB_ENV"
}
_write_kv_to_env_file() {
local _n
local _v
local _re
_n="$(_env_name "${1}")"
_v="${2}"
_re='\\n'
if [[ "${_v}" =~ $_re ]]; then
_write_env_file_line "${_n}<<EOF"
_write_env_file_line "${_v}"
_write_env_file_line 'EOF'
else
_write_env_file_line "${_n}=${_v}"
fi
}
_yaml_keys=()
_env_names=()
_lines=()
readarray -t _lines <<< "$(yq -o p '.' "$YAMLTOENV_YAML_FILE")"
for _line in "${_lines[@]}"; do
if [ -z "${_line}" ]; then
continue
fi
if [[ "${_line}" =~ ^# ]]; then
continue
fi
_split=()
IFS=$'='; read -r -a _split <<< "${_line}"
_key="$(_rtrim_one "${_split[0]}")"
_value="$(_join_by '=' "${_split[@]:1}")"
_clean_value="$(_ltrim_one "${_value}")"
if [[ "${YAMLTOENV_MASK_VALUES}" == 'true' ]]; then
# sourced https://dev.to/yuyatakeyama/mask-multiple-lines-text-in-github-actions-workflow-1a0
echo "::add-mask::$(echo "$_clean_value" \
| sed -e 's/\\n/\n/g' | sed ':a;N;$!ba;s/%/%25/g' | sed ':a;N;$!ba;s/\r/%0D/g' | sed ':a;N;$!ba;s/\n/%0A/g')"
fi
_yaml_keys+=("${_key}")
_env_names+=("$(_env_name "${_key}")")
_write_kv_to_env_file "${_key}" "${_clean_value}"
done
echo "var-count=${#_env_names[@]}" >> "$GITHUB_OUTPUT"
echo "yaml-keys=$(_join_by "," "${_yaml_keys[@]}")" >> "$GITHUB_OUTPUT"
echo "env-names=$(_join_by "," "${_env_names[@]}")" >> "$GITHUB_OUTPUT"