-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
action.sh
executable file
·79 lines (67 loc) · 2.9 KB
/
action.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
71
72
73
74
75
76
77
78
79
#!/bin/bash
set -eu
#set -x # for debugging
: ${GITHUB_API_URL:=https://api.github.com}
: ${VERSION:=latest}
: ${REPO:=rancher/k3s}
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
authz=("-H" "Authorization: Bearer ${GITHUB_TOKEN}")
else
authz=()
fi
# Fetch all K3s versions usable for the specified partial version.
# Even if the version is specific and complete, assume it is possibly partial.
# 2-3 pages are enough to reach v0 while not depleting the GitHub API limits.
versions=""
for page in 1 2 ; do
url="${GITHUB_API_URL}/repos/${REPO}/releases?per_page=999&page=${page}"
releases=$(curl --silent --fail --location "${authz[@]-}" "$url")
versions+=$(jq <<< "$releases" '.[] | select(.prerelease==false) | .tag_name')
versions+=$'\n'
done
versions_sorted=$(sort <<< "$versions" --field-separator=- --key=1,1rV --key=2,2rV)
echo "::group::All available K3s versions (newest on top)"
echo "$versions_sorted"
echo "::endgroup::"
# The "latest" version is not directly exposed, but we hard-code its meaning.
if [[ "${VERSION}" == "latest" ]]; then
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.
versions_matching=$(jq --slurp <<< "$versions_sorted" --arg version "${VERSION}" '
.[]
| select((.|startswith($version + ".")) or
(.|startswith($version + "-")) or
(.|startswith($version + "+")) or
(.==$version))
')
echo "::group::All matching K3s versions (newest on top)"
echo "$versions_matching"
echo "::endgroup::"
# Validate that we could identify the version (even a very specific one).
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=$(jq --slurp <<< "$versions_matching" --raw-output '.[0]')
K8S=${K3S%%+*}
# Communicate back to GitHub Actions.
echo "::set-output name=k3s-version::${K3S}"
echo "::set-output name=k8s-version::${K8S}"
# Install K3d and start a K3s cluster. It takes 20 seconds usually.
# Name & args can be empty or multi-value. For this, they are not quoted.
curl --silent --fail https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
k3d cluster create ${K3D_NAME:-} --wait --image=rancher/k3s:"${K3S//+/-}" ${K3D_ARGS:-}
# Sometimes, the service account is not created immediately. Nice trick, but no:
# we need to wait until the cluster is fully ready before starting the tests.
if [[ -z "${SKIP_READINESS}" ]]; then
echo "::group::Waiting for cluster readiness"
while ! kubectl get serviceaccount default >/dev/null; do sleep 1; done
echo "::endgroup::"
else
echo "Skipping the readiness wait. The cluster can be not fully ready yet."
fi