cyberark/secretless-broker

View on GitHub
bin/build_release

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash

set -eo pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"

# Get the version of Go specified by the "go directive" in go.mod
# Grep it to avoid Go binary dependency
GO_VERSION="v$(grep "^\bgo\b" "${REPO_ROOT}/go.mod" | awk '{print $2}')"

# Determine where VERSION file is based on goreleaser argument
if [[ "$1" == '--skip-validate' ]]; then
  VERSION=$(<"${REPO_ROOT}/../VERSION")
else
  VERSION=$(<"${REPO_ROOT}/VERSION")
fi

# Remove Jenkins build number from VERSION
VERSION="${VERSION/-*/}"

# Use a GoReleaser Docker image containing cross-compilation tools
# This image is recommended by the official GoReleaser docs
# https://goreleaser.com/cookbooks/cgo-and-crosscompiling/
GORELEASER_IMAGE="goreleaser/goreleaser-cross"

# Get the latest tag matching the GO_VERSION
GORELEASER_TAGS_JSON="$(curl --silent --show-error https://registry.hub.docker.com/v2/repositories/${GORELEASER_IMAGE}/tags?page_size=100)"
GORELEASER_LATEST_TAG="$(echo "${GORELEASER_TAGS_JSON}" | \
  jq \
    --raw-output \
    --arg GO_VERSION "${GO_VERSION}" \
    '
      .results |
      map(select(.name | contains($GO_VERSION) and (contains("-") | not))) |
      first |
      .name
    '
)"

if [[ -z "${GORELEASER_LATEST_TAG}" ]]; then
  echo "Could not find tag for Docker image \"${GORELEASER_IMAGE}\" matching GO_VERSION=${GO_VERSION}"
  exit 1
fi

echo "Docker image for release build: ${GORELEASER_IMAGE}:${GORELEASER_LATEST_TAG}"

docker run --rm -t \
  --env VERSION="${VERSION}" \
  --volume "${REPO_ROOT}:/secretless-broker" \
  --workdir /secretless-broker \
  "${GORELEASER_IMAGE}:${GORELEASER_LATEST_TAG}" --rm-dist "$@"

echo "Releases built. Archives can be found in dist/goreleaser"