Skip to content

Commit 335bb70

Browse files
committed
feat: add version_file input to read the kind version from a file
Signed-off-by: somaz <genius5711@gmail.com>
1 parent da962b4 commit 335bb70

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

.github/workflows/test.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,32 @@ jobs:
140140
kubectl cluster-info
141141
kubectl get nodes
142142
143+
test-with-version-file:
144+
runs-on: ubuntu-latest
145+
146+
permissions:
147+
contents: read
148+
149+
steps:
150+
- name: Checkout
151+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
152+
with:
153+
persist-credentials: false
154+
155+
- name: Create version file
156+
run: echo "kind v0.23.0" > .tool-versions
157+
158+
- name: Create kind cluster from version file
159+
uses: ./
160+
with:
161+
version_file: ".tool-versions"
162+
163+
- name: Test
164+
run: |
165+
kind version | grep "v0.23.0"
166+
kubectl cluster-info
167+
kubectl get nodes
168+
143169
test-with-custom-kubeconfig:
144170
runs-on: ubuntu-latest
145171

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ For more information, reference the GitHub Help Documentation for [Creating a wo
1616
For more information on inputs, see the [API Documentation](https://developer.github.com/v3/repos/releases/#input)
1717

1818
- `version`: The kind version to use (default: `v0.31.0`)
19+
- `version_file`: The path to a file containing the kind version to use. Supports the asdf [`.tool-versions`](https://asdf-vm.com/manage/configuration.html#tool-versions) layout (the `kind` entry) and plain version files. The version is used as-is, so include the leading `v` (for example `kind v0.31.0`). Takes precedence over `version` when set.
1920
- `config`: The path to the kind config file
2021
- `node_image`: The Docker image for the cluster nodes
2122
- `cluster_name`: The name of the cluster to create (default: `chart-testing`)

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ inputs:
99
description: "The kind version to use (default: v0.31.0)"
1010
required: false
1111
default: "v0.31.0"
12+
version_file:
13+
description: "The path to a file containing the kind version to use (asdf '.tool-versions' or a plain version file; the version is used as-is, e.g. v0.31.0). Takes precedence over 'version' when set."
14+
required: false
1215
config:
1316
description: "The path to the kind config file"
1417
required: false

main.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,45 @@ set -o pipefail
2020

2121
SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}" || realpath "${BASH_SOURCE[0]}")")
2222

23+
# Resolve the kind version from a version file. Supports the asdf
24+
# '.tool-versions' format (a line such as "kind v0.31.0") as well as a plain
25+
# file that contains only the version string.
26+
resolve_version_from_file() {
27+
local file="$1"
28+
local version=""
29+
30+
if [[ ! -f "${file}" ]]; then
31+
echo "ERROR: version_file '${file}' does not exist." >&2
32+
exit 1
33+
fi
34+
35+
# asdf '.tool-versions' format: a line such as "kind v0.31.0".
36+
version=$(grep -E '^[[:space:]]*kind[[:space:]]+' "${file}" | head -n 1 | tr -d '\r' | awk '{print $2}') || true
37+
38+
# Otherwise treat the file as a plain version file (first token of the
39+
# first non-empty, non-comment line).
40+
if [[ -z "${version}" ]]; then
41+
version=$(grep -vE '^[[:space:]]*(#|$)' "${file}" | head -n 1 | tr -d '\r' | awk '{print $1}') || true
42+
fi
43+
44+
if [[ -z "${version}" ]]; then
45+
echo "ERROR: no kind version found in version_file '${file}'." >&2
46+
exit 1
47+
fi
48+
49+
echo "${version}"
50+
}
51+
2352
main() {
2453
local args=()
2554
local registry_args=()
2655

56+
# A version file takes precedence over the 'version' input when set.
57+
if [[ -n "${INPUT_VERSION_FILE:-}" ]]; then
58+
INPUT_VERSION="$(resolve_version_from_file "${INPUT_VERSION_FILE}")"
59+
echo "Using kind version '${INPUT_VERSION}' from version_file '${INPUT_VERSION_FILE}'." >&2
60+
fi
61+
2762
if [[ -n "${INPUT_VERSION:-}" ]]; then
2863
args+=(--version "${INPUT_VERSION}")
2964
fi

0 commit comments

Comments
 (0)