From 4ecc01f3ad56e608d32d141e8fc67565c195ec4e Mon Sep 17 00:00:00 2001 From: Bjorn Neergaard Date: Tue, 18 Jul 2023 08:12:02 -0600 Subject: [PATCH] ci(buildkit): remove misleading code from buildkit-ref Post-f8c0d92a22bad004cb9cbb4db704495527521c42, BUILDKIT_REPO doesn't really do what it claims to. Instead, don't allow overloading since the import path for BuildKit is always the same, and make clear the provenance of values when generating the final variable definitions. We also better document the script, and follow some best practices for both POSIX sh and Bash. Signed-off-by: Bjorn Neergaard --- .github/workflows/buildkit.yml | 2 -- hack/buildkit-ref | 28 +++++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/buildkit.yml b/.github/workflows/buildkit.yml index 161216d983..3e065c7a00 100644 --- a/.github/workflows/buildkit.yml +++ b/.github/workflows/buildkit.yml @@ -49,8 +49,6 @@ jobs: test: runs-on: ubuntu-20.04 timeout-minutes: 120 - env: - BUILDKIT_REPO: moby/buildkit needs: - build strategy: diff --git a/hack/buildkit-ref b/hack/buildkit-ref index b5991ad04e..99d3cf8401 100755 --- a/hack/buildkit-ref +++ b/hack/buildkit-ref @@ -1,8 +1,12 @@ #!/usr/bin/env bash -# This script returns the current BuildKit ref being used in moby. +# This script returns the current BuildKit ref and source repository being used. +# This script will only work with a BuildKit repository hosted on GitHub. +# If BUILDKIT_REF is already set in the environment, it will be returned as-is. +# +# The output of this script may be valid shell script, but is intended for use with +# GitHub Actions' $GITHUB_ENV. -: "${BUILDKIT_REPO=moby/buildkit}" -: "${BUILDKIT_REF=}" +buildkit_pkg=github.com/moby/buildkit if [ -n "$BUILDKIT_REF" ]; then echo "$BUILDKIT_REF" @@ -10,16 +14,18 @@ if [ -n "$BUILDKIT_REF" ]; then fi # get buildkit version from vendor.mod -BUILDKIT_REF=$(./hack/with-go-mod.sh go list -mod=mod -modfile=vendor.mod -u -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' "github.com/${BUILDKIT_REPO}") -BUILDKIT_REPO=$(./hack/with-go-mod.sh go list -mod=mod -modfile=vendor.mod -u -m -f '{{if .Replace}}{{.Replace.Path}}{{else}}{{.Path}}{{end}}' "github.com/${BUILDKIT_REPO}") -BUILDKIT_REPO=${BUILDKIT_REPO#github.com/} +buildkit_ref=$(./hack/with-go-mod.sh go list -mod=mod -modfile=vendor.mod -u -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' "$buildkit_pkg") +buildkit_repo=$(./hack/with-go-mod.sh go list -mod=mod -modfile=vendor.mod -u -m -f '{{if .Replace}}{{.Replace.Path}}{{else}}{{.Path}}{{end}}' "$buildkit_pkg") +buildkit_repo=${buildkit_repo#github.com/} -if [[ "${BUILDKIT_REF}" == *-*-* ]]; then +if [[ "${buildkit_ref}" == *-*-* ]]; then # if pseudo-version, figure out just the uncommon sha (https://github.com/golang/go/issues/34745) - BUILDKIT_REF=$(echo "${BUILDKIT_REF}" | awk -F"-" '{print $NF}' | awk 'BEGIN{FIELDWIDTHS="7"} {print $1}') + buildkit_ref=$(awk -F"-" '{print $NF}' <<< "$buildkit_ref" | awk 'BEGIN{FIELDWIDTHS="7"} {print $1}') # use github api to return full sha to be able to use it as ref - BUILDKIT_REF=$(curl -s "https://api.github.com/repos/${BUILDKIT_REPO}/commits/${BUILDKIT_REF}" | jq -r .sha) + buildkit_ref=$(curl -s "https://api.github.com/repos/${buildkit_repo}/commits/${buildkit_ref}" | jq -r .sha) fi -echo "BUILDKIT_REPO=$BUILDKIT_REPO" -echo "BUILDKIT_REF=$BUILDKIT_REF" +cat << EOF +BUILDKIT_REPO=$buildkit_repo +BUILDKIT_REF=$buildkit_ref +EOF