diff --git a/.github/actions/setup-go-kpt/action.yml b/.github/actions/setup-go-kpt/action.yml index 860cc61f10..8f84d76845 100644 --- a/.github/actions/setup-go-kpt/action.yml +++ b/.github/actions/setup-go-kpt/action.yml @@ -13,13 +13,17 @@ # limitations under the License. name: "Setup Go and kpt" -description: "Sets up Go from go.mod and installs kpt version matching go.mod" +description: "Sets up Go from go.mod and installs kpt, either from the kpt-version input or the version pinned in go.mod" inputs: install-kpt: description: "Whether to install kpt" required: false default: "true" + kpt-version: + description: "Exact kpt version to install (e.g. v1.0.2-pre.4). Takes precedence over kpt-version-file." + required: false + default: "" go-version: description: "Exact version for Go version setup, go-version-file is ignored if set" required: false @@ -28,7 +32,7 @@ inputs: required: false default: "go.mod" kpt-version-file: - description: "Path to the go.mod file used to resolve the kpt version. Defaults to go-version-file." + description: "Path to the go.mod file used to resolve the kpt version. Defaults to go-version-file. Ignored when kpt-version is set." required: false default: "" go-cache: @@ -56,36 +60,67 @@ runs: cache: ${{ inputs.go-cache == 'true' }} cache-dependency-path: ${{ inputs.cache-dependency-path }} - - name: Get kpt version from go.mod + - name: Resolve kpt version if: inputs.install-kpt == 'true' id: kpt-version shell: bash env: + KPT_VERSION_INPUT: ${{ inputs.kpt-version }} GO_VERSION_FILE: ${{ inputs.go-version-file }} KPT_VERSION_FILE: ${{ inputs.kpt-version-file }} run: | set -euo pipefail - VERSION_FILE="${KPT_VERSION_FILE:-$GO_VERSION_FILE}" - if [ ! -f "$VERSION_FILE" ]; then - echo "::error::kpt version file '${VERSION_FILE}' not found." - exit 1 - fi - GO_MOD_DIR=$(dirname -- "$VERSION_FILE") - KPT_VERSION=$(cd "$GO_MOD_DIR" && go mod edit -json \ - | jq -r '(.Require[]? | select(.Path=="github.com/kptdev/kpt") | .Version) // ""') - if [ -z "$KPT_VERSION" ]; then - echo "::error::Could not resolve github.com/kptdev/kpt from ${VERSION_FILE}." - exit 1 + if [ -n "${KPT_VERSION_INPUT}" ]; then + KPT_VERSION="${KPT_VERSION_INPUT}" + echo "Using kpt version from kpt-version input" + else + VERSION_FILE="${KPT_VERSION_FILE:-$GO_VERSION_FILE}" + if [ ! -f "$VERSION_FILE" ]; then + echo "::error::kpt version file '${VERSION_FILE}' not found." + exit 1 + fi + GO_MOD_DIR=$(dirname -- "$VERSION_FILE") + KPT_VERSION=$(cd "$GO_MOD_DIR" && go mod edit -json \ + | jq -r '(.Require[]? | select(.Path=="github.com/kptdev/kpt") | .Version) // ""') + if [ -z "$KPT_VERSION" ]; then + echo "::error::Could not resolve github.com/kptdev/kpt from ${VERSION_FILE}." + exit 1 + fi fi + # Normalise to the go.mod form, e.g. 1.0.2-pre.4 -> v1.0.2-pre.4 + case "${KPT_VERSION}" in + v*) ;; + *) KPT_VERSION="v${KPT_VERSION}" ;; + esac echo "Using kpt version: ${KPT_VERSION}" echo "version=${KPT_VERSION}" >> "$GITHUB_OUTPUT" + - name: Resolve kpt release tag + if: inputs.install-kpt == 'true' + id: kpt-tag + shell: bash + env: + KPT_VERSION: ${{ steps.kpt-version.outputs.version }} + run: | + set -euo pipefail + # Newer kpt releases use the cli/ tag, older ones the plain . + # Output is captured first so a git failure aborts instead of looking like "no cli/ tag". + if ! REFS=$(GIT_TERMINAL_PROMPT=0 git ls-remote --tags https://github.com/kptdev/kpt "refs/tags/cli/${KPT_VERSION}"); then + echo "::error::Could not query kptdev/kpt tags to resolve the release layout for ${KPT_VERSION}." + exit 1 + fi + if [ -n "${REFS}" ]; then + TAG="cli/${KPT_VERSION}" + else + TAG="${KPT_VERSION}" + fi + echo "Using kpt release tag: ${TAG}" + echo "tag=${TAG}" >> "$GITHUB_OUTPUT" + - name: Install kpt if: inputs.install-kpt == 'true' uses: jaxxstorm/action-install-gh-release@v2.1.0 with: repo: kptdev/kpt - # The kpt CLI binaries are published on the cli/ module's release tag - # (e.g. cli/v1.0.2-pre.4); the root module tag carries no assets. - tag: cli/${{ steps.kpt-version.outputs.version }} + tag: ${{ steps.kpt-tag.outputs.tag }} chmod: 0755