From cc235e70c6abb3580a759636714d856ef2903d64 Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Tue, 2 Mar 2021 18:49:21 -0600 Subject: [PATCH] Use Bash here strings rather than temporary files Each process substitution is now a single command with no pipes, so one failure stops the script. Tested on Bash 3.2.57 (macOS Big Sur) and Bash 5.0.3 (debian buster). Signed-off-by: Chris Bandy --- action.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/action.sh b/action.sh index f3afd51..52acfe9 100755 --- a/action.sh +++ b/action.sh @@ -15,11 +15,11 @@ fi # Fetch all K3s versions usable for the specified partial version. # Even if the version is specific and complete, assume it is possibly partial. url="${GITHUB_API_URL}/repos/${REPO}/releases?per_page=999" -curl --silent --fail --location "${authz[@]}" "$url" | \ - jq '.[] | select(.prerelease==false) | .tag_name' >/tmp/versions.txt +releases=$(curl --silent --fail --location "${authz[@]-}" "$url") +versions=$(jq <<< "$releases" '.[] | select(.prerelease==false) | .tag_name') echo "::group::All available K3s versions (unsorted)" -cat /tmp/versions.txt +echo "$versions" echo "::endgroup::" # Sort the versions numerically, not lexographically: @@ -28,7 +28,7 @@ echo "::endgroup::" # 2. Convert parts to numbers when possible ([1, 19, 4, 1]). # 3. Sort numerically instead of lexographically. # 4. Restore the original name of each version. -jq --slurp ' +versions_sorted=$(jq --slurp <<< "$versions" ' [ .[] | { original: ., numeric: @@ -42,41 +42,41 @@ jq --slurp ' | reverse | .[] | .original - ' /tmp/sorted.txt + ') echo "::group::All available K3s versions (newest on top)" -cat /tmp/sorted.txt +echo "$versions_sorted" echo "::endgroup::" # The "latest" version is not directly exposed, but we hard-code its meaning. if [[ "${VERSION}" == "latest" ]]; then - VERSION=$(head -n 1 /tmp/sorted.txt | jq -r) + VERSION=$(jq --slurp <<< "$versions_sorted" --raw-output '.[0]') fi # The select only those versions that match the requested one. # Do not rely on the parsed forms of the versions -- they may miss some parts. # Rely only on the actual name of the version. # TODO: LATER: Handle release candidates: v1.18.2-rc3+k3s1 must be before v1.18.2+k3s1. -jq --slurp --arg version "${VERSION}" ' +versions_matching=$(jq --slurp <<< "$versions_sorted" --arg version "${VERSION}" ' .[] | select((.|startswith($version + ".")) or (.|startswith($version + "-")) or (.|startswith($version + "+")) or (.==$version)) - ' /tmp/matching.txt + ') echo "::group::All matching K3s versions (newest on top)" -cat /tmp/matching.txt +echo "$versions_matching" echo "::endgroup::" # Validate that we could identify the version (even a very specific one). -if [[ ! -s /tmp/matching.txt ]]; then +if [[ -z "$versions_matching" ]]; then echo "::error::No matching K3s versions were found." exit 1 fi # Get the best possible (i.e. the latest) version of K3s/K8s. -K3S=$(head -n 1 /tmp/matching.txt | jq -r) +K3S=$(jq --slurp <<< "$versions_matching" --raw-output '.[0]') K8S=${K3S%%+*} # Communicate back to GitHub Actions.