-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
189 lines (168 loc) · 7.27 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
# SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
# SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
# SPDX-License-Identifier: Apache-2.0
#
# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
name: (Yet Another) Setup yq
description: Installs the yq command-line YAML, JSON, XML, CSV and properties parser/processor on Linux, macOS and Windows runners
author: Vegard IT GmbH (https://vegardit.com)
branding:
color: green
icon: package
inputs:
version:
default: any
description: "yq version to install: 'any', 'latest', or a version number, e.g. '4.30.8'"
use-cache:
default: 'true'
description: if the downloaded yq binary should be cached using the GHA caching service
outputs:
path:
value: ${{ steps.YQ-SETUP.outputs.path }}
description: Path to the yq executable
version:
value: ${{ steps.YQ-SETUP.outputs.version }}
description: Version of the installed yq program
runs:
using: 'composite'
steps:
##################################################
# Setup Cache
##################################################
- name: 'yq: cache restore'
id: YQ-CACHE
if: ${{ (inputs.use-cache == true || inputs.use-cache == 'true') && !env.ACT }} # https://github.com/nektos/act#skipping-steps
uses: actions/cache/restore@v4 # https://github.com/actions/cache/tree/main/restore
with:
path: ${{ runner.temp }}/yq
key: ${{ runner.os }}-yq-${{ inputs.version }}
##################################################
# Install yq
##################################################
- name: 'yq: setup'
id: YQ-SETUP
shell: bash
env:
GH_TOKEN: ${{ github.token }} # required by "gh actions-cache delete"
INPUTS_VERSION: ${{ inputs.version }}
INPUTS_USE_CACHE: ${{ inputs.use-cache }}
CACHE_HIT: ${{ steps.YQ-CACHE.outputs.cache-hit }}
CACHE_CACHE_KEY: ${{ steps.YQ-CACHE.outputs.cache-primary-key }}
run: |
echo '::group::setup: yq'
function get_installed_version() {
"${1:-$app_home/$app_target_bin}" --version | grep -o '[^ ]*$' | grep -o '[^/]*$' | cut -c2-
}
INPUTS_VERSION_LOWERCASE="$(tr '[:upper:]' '[:lower:]' <<<"$INPUTS_VERSION")"
if [[ $INPUTS_VERSION_LOWERCASE == 'any' ]] && hash yq &>/dev/null; then
# set outputs
echo "need_cache_update=false" | tee -a "$GITHUB_OUTPUT"
case "$RUNNER_OS" in
Windows) echo "path=$(cygpath -w "$(which yq)")" | tee -a "$GITHUB_OUTPUT" ;;
*) echo "path=$(which yq)" | tee -a "$GITHUB_OUTPUT" ;;
esac
echo "version=$(get_installed_version "$(which yq)")" | tee -a "$GITHUB_OUTPUT"
else
APP_REPO_ROOT=https://github.com/mikefarah/yq
case "$RUNNER_OS" in
macOS)
app_home="$RUNNER_TEMP/yq"
app_target_bin=yq
case "$(machine)" in
*arm*) app_source_bin='yq_darwin_arm64' ;;
*) app_source_bin='yq_darwin_amd64' ;;
esac
;;
Linux)
app_home="$RUNNER_TEMP/yq"
app_target_bin=yq
case $(uname -m) in # https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
i386|i686) app_source_bin='yq_linux_386' ;;
x86_64) app_source_bin='yq_linux_amd64' ;;
arm) app_source_bin='yq_linux_arm' ;;
armv*|aarch64*) app_source_bin='yq_linux_arm64' ;;
esac
;;
Windows)
app_home="$(cygpath "$RUNNER_TEMP")/yq"
app_target_bin=yq.exe
app_source_bin='yq_windows_amd64.exe'
;;
esac
echo "app_home: $app_home"
echo "app_source_bin: $app_source_bin"
echo "app_target_bin: $app_target_bin"
function get_latest_version() {
# temporary workaround for https://github.com/curl/curl/issues/13845
if [[ $RUNNER_OS == "Windows" ]]; then
(set -x; powershell -Command "(Invoke-WebRequest -Uri '$APP_REPO_ROOT/releases/latest' -Method Head -MaximumRedirection 0 -ErrorAction Ignore -UseBasicParsing).Headers.Location" | grep -o '[^/]*$' | cut -c2-)
return
fi
(set -x; curl -sSfL --max-time 5 -o /dev/null -w '%{url_effective}' $APP_REPO_ROOT/releases/latest | grep -o '[^/]*$' | cut -c2-)
}
app_downloaded=false
function download_app() {
app_download_url="$APP_REPO_ROOT/releases/download/v$1/$app_source_bin"
echo "Downloading [$app_download_url]..."
mkdir -p "$app_home"
(set -x; curl -fsSL --max-time 10 --retry 3 --retry-delay 5 -o "$app_home/$app_target_bin" "$app_download_url")
chmod 777 "$app_home/$app_target_bin"
app_downloaded=true
}
case "$INPUTS_VERSION_LOWERCASE" in
any)
if [[ ! -f "$app_home/$app_target_bin" ]]; then
latest_app_version=$(get_latest_version)
download_app "$latest_app_version"
fi
;;
latest)
if [[ -f "$app_home/$app_target_bin" ]]; then
latest_app_version=$(get_latest_version)
if [[ $latest_app_version != "$(get_installed_version)" ]]; then
download_app "$latest_app_version"
fi
else
download_app "$(get_latest_version)"
fi
;;
*) # install specific release
if [[ -f "$app_home/$app_target_bin" ]]; then
if [[ $(get_installed_version) != "v$INPUTS_VERSION" ]]; then
download_app "$INPUTS_VERSION"
fi
else
download_app "$INPUTS_VERSION"
fi
;;
esac
echo "$RUNNER_TEMP/yq" >> "$GITHUB_PATH"
# prepare cache update
if [[ $INPUTS_USE_CACHE == 'true' && ${ACT:-} != 'true' ]]; then # $ACT is set when running via nektos/act
if [[ $app_downloaded == 'true' ]]; then
if [[ $CACHE_HIT == 'true' ]]; then
gh extension install actions/gh-actions-cache || true
gh actions-cache delete "$CACHE_CACHE_KEY" --confirm || true
fi
need_cache_update=true
else
need_cache_update=false
fi
else
need_cache_update=false
fi
# set outputs
echo "need_cache_update=$need_cache_update" | tee -a "$GITHUB_OUTPUT"
case "$RUNNER_OS" in
Windows) echo "path=$(cygpath -w "$app_home/$app_target_bin")" | tee -a "$GITHUB_OUTPUT" ;;
*) echo "path=$app_home/$app_target_bin" | tee -a "$GITHUB_OUTPUT" ;;
esac
echo "version=$(get_installed_version)" | tee -a "$GITHUB_OUTPUT"
fi
echo '::endgroup::'
- name: 'yq: cache update'
uses: actions/cache/save@v4
if: ${{ steps.YQ-SETUP.outputs.need_cache_update == 'true' }}
with:
path: ${{ runner.temp }}/yq
key: ${{ steps.YQ-CACHE.outputs.cache-primary-key }}