Skip to content

Commit

Permalink
Merge pull request #7 from cbandy/here-strings
Browse files Browse the repository at this point in the history
Compatibility with ubuntu-18.04 runner
  • Loading branch information
nolar authored Apr 10, 2021
2 parents 8c1a637 + cc235e7 commit faab498
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions action.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -42,41 +42,41 @@ jq --slurp '
| reverse
| .[]
| .original
' </tmp/versions.txt >/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/sorted.txt >/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.
Expand Down

0 comments on commit faab498

Please sign in to comment.