From 335bb70f89b56711d2f22c8e645d60eef1bdadd8 Mon Sep 17 00:00:00 2001 From: somaz Date: Fri, 12 Jun 2026 12:48:25 +0900 Subject: [PATCH] feat: add version_file input to read the kind version from a file Signed-off-by: somaz --- .github/workflows/test.yaml | 26 ++++++++++++++++++++++++++ README.md | 1 + action.yml | 3 +++ main.sh | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f01ada9..16c52ae 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -140,6 +140,32 @@ jobs: kubectl cluster-info kubectl get nodes + test-with-version-file: + runs-on: ubuntu-latest + + permissions: + contents: read + + steps: + - name: Checkout + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + persist-credentials: false + + - name: Create version file + run: echo "kind v0.23.0" > .tool-versions + + - name: Create kind cluster from version file + uses: ./ + with: + version_file: ".tool-versions" + + - name: Test + run: | + kind version | grep "v0.23.0" + kubectl cluster-info + kubectl get nodes + test-with-custom-kubeconfig: runs-on: ubuntu-latest diff --git a/README.md b/README.md index 68efd8f..f41b93f 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ For more information, reference the GitHub Help Documentation for [Creating a wo For more information on inputs, see the [API Documentation](https://developer.github.com/v3/repos/releases/#input) - `version`: The kind version to use (default: `v0.31.0`) +- `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. - `config`: The path to the kind config file - `node_image`: The Docker image for the cluster nodes - `cluster_name`: The name of the cluster to create (default: `chart-testing`) diff --git a/action.yml b/action.yml index cecb446..3fe4da7 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,9 @@ inputs: description: "The kind version to use (default: v0.31.0)" required: false default: "v0.31.0" + version_file: + 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." + required: false config: description: "The path to the kind config file" required: false diff --git a/main.sh b/main.sh index bd9e67e..9c87e0b 100755 --- a/main.sh +++ b/main.sh @@ -20,10 +20,45 @@ set -o pipefail SCRIPT_DIR=$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}" || realpath "${BASH_SOURCE[0]}")") +# Resolve the kind version from a version file. Supports the asdf +# '.tool-versions' format (a line such as "kind v0.31.0") as well as a plain +# file that contains only the version string. +resolve_version_from_file() { + local file="$1" + local version="" + + if [[ ! -f "${file}" ]]; then + echo "ERROR: version_file '${file}' does not exist." >&2 + exit 1 + fi + + # asdf '.tool-versions' format: a line such as "kind v0.31.0". + version=$(grep -E '^[[:space:]]*kind[[:space:]]+' "${file}" | head -n 1 | tr -d '\r' | awk '{print $2}') || true + + # Otherwise treat the file as a plain version file (first token of the + # first non-empty, non-comment line). + if [[ -z "${version}" ]]; then + version=$(grep -vE '^[[:space:]]*(#|$)' "${file}" | head -n 1 | tr -d '\r' | awk '{print $1}') || true + fi + + if [[ -z "${version}" ]]; then + echo "ERROR: no kind version found in version_file '${file}'." >&2 + exit 1 + fi + + echo "${version}" +} + main() { local args=() local registry_args=() + # A version file takes precedence over the 'version' input when set. + if [[ -n "${INPUT_VERSION_FILE:-}" ]]; then + INPUT_VERSION="$(resolve_version_from_file "${INPUT_VERSION_FILE}")" + echo "Using kind version '${INPUT_VERSION}' from version_file '${INPUT_VERSION_FILE}'." >&2 + fi + if [[ -n "${INPUT_VERSION:-}" ]]; then args+=(--version "${INPUT_VERSION}") fi