| #!/usr/bin/env bash | |
| # This script returns the current BuildKit ref and source repository being used. | |
| # This script will only work with a BuildKit repository hosted on GitHub. | |
| # | |
| # The output of this script may be valid shell script, but is intended for use with | |
| # GitHub Actions' $GITHUB_ENV. | |
| set -euo pipefail | |
| buildkit_pkg=github.com/moby/buildkit | |
| resolve_github_commit_sha() { | |
| local repo=$1 | |
| local ref=$2 | |
| local resolved | |
| if command -v gh > /dev/null 2>&1; then | |
| if resolved=$(gh api "repos/${repo}/commits/${ref}" --jq '.sha // empty' 2> /dev/null) && [[ -n "$resolved" ]]; then | |
| echo "$resolved" | |
| return 0 | |
| fi | |
| fi | |
| local curl_args=(-fsSL) | |
| if [[ -n "${GH_TOKEN:-}" ]]; then | |
| curl_args+=(-H "Authorization: Bearer ${GH_TOKEN}") | |
| fi | |
| if resolved=$(curl "${curl_args[@]}" "https://api.github.com/repos/${repo}/commits/${ref}" | jq -er '.sha // empty'); then | |
| echo "$resolved" | |
| return 0 | |
| fi | |
| return 1 | |
| } | |
| # BUILDKIT_REPO and BUILDKIT_REF may be set to override the values from go.mod. | |
| if [ -n "${BUILDKIT_REPO:-}" ]; then | |
| echo "WARNING: BUILDKIT_REPO is set; overriding BuildKit repository from go.mod with ${BUILDKIT_REPO}" >&2 | |
| fi | |
| if [ -n "${BUILDKIT_REF:-}" ]; then | |
| echo "WARNING: BUILDKIT_REF is set; overriding BuildKit ref from go.mod with ${BUILDKIT_REF}" >&2 | |
| fi | |
| # get BuildKit version and repository from go.mod, unless explicitly overridden. | |
| buildkit_ref=${BUILDKIT_REF:-$(go list -mod=mod -u -m -f '{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}' "$buildkit_pkg")} | |
| buildkit_repo=${BUILDKIT_REPO:-$(go list -mod=mod -u -m -f '{{if .Replace}}{{.Replace.Path}}{{else}}{{.Path}}{{end}}' "$buildkit_pkg")} | |
| buildkit_repo=${buildkit_repo#github.com/} | |
| if [[ "${buildkit_ref}" == *-*-* ]]; then | |
| # if pseudo-version, figure out just the commit sha | |
| commit_ref=$(awk -F"-" '{print $NF}' <<< "$buildkit_ref") | |
| # resolve through GitHub to get the full sha for use as a ref. | |
| if ! buildkit_ref=$(resolve_github_commit_sha "$buildkit_repo" "$commit_ref"); then | |
| echo "failed to resolve BuildKit commit ${buildkit_repo}@${commit_ref}" >&2 | |
| exit 1 | |
| fi | |
| fi | |
| cat << EOF | |
| BUILDKIT_REPO=$buildkit_repo | |
| BUILDKIT_REF=$buildkit_ref | |
| EOF |