diff --git a/build-docker.sh b/build-docker.sh index bb1710ccb3..bfb2d76931 100755 --- a/build-docker.sh +++ b/build-docker.sh @@ -3,18 +3,6 @@ set -o nounset set -o pipefail set -o xtrace -TAGS="" -# Check if CIRCLE_BRANCH is set and not empty -if [[ -n "${CIRCLE_BRANCH}" ]]; then - BRANCH_TAG=${CIRCLE_BRANCH//\//-} - TAGS="${CIRCLE_BUILD_NUM},branch-${BRANCH_TAG}" -fi - -# We only want to tag main with latest on ECR -if [[ "${CIRCLE_BRANCH}" == "main" ]]; then - TAGS+=",latest" -fi - # Determine a version number based on most recent version tag in git. # git describe - Return the most recent annotated tag that is reachable from a commit. # --tags - OK, any tag, not just the annotated ones. We don't always remember to annotate a version tag. @@ -24,7 +12,9 @@ fi # - The build was on git commit ID a1b2c3d. # - v2.1.1 is the most recent version tag in the history behind that commit # - That commit is 45 commits ahead of that version tag. -VERSION=$(git describe --tags --match='v[0-9]*' --always) +VERSION_FROM_GIT=$(git describe --tags --match='v[0-9]*' --always) +# trim the v prefix per Docker image version-tagging conventions +VERSION=${VERSION_FROM_GIT#'v'} # If we are doing a dev build on circle, append the build number (job id) to the version # Ex: v2.1.1-45-ga1b2c3d-ci8675309 @@ -34,23 +24,40 @@ if [[ -n "${CIRCLE_BUILD_NUM:-}" ]]; then VERSION="${VERSION}-ci${CIRCLE_BUILD_NUM}" fi +### Image tagging ### +TAGS="${VERSION}" + +## CI dev tagging: append the dev branch name to image tags +if [[ -n "${CIRCLE_BRANCH:-}" ]]; then + BRANCH_TAG=${CIRCLE_BRANCH//\//-} + TAGS+=",branch-${BRANCH_TAG}" + + # If the dev build is on main, we tag it as latest in ECR + if [[ "${CIRCLE_BRANCH}" == "main" ]]; then + TAGS+=",latest" + fi +fi + +## CI release tagging: apply major, major.minor, major.minor.patch, and latest tags + # if we're running off a git tag, it is a release which we tag with the versions as well as latest # caution: this means if we ever release an update to a previous version, it will be marked latest # it is probably best if people just use the major or minor version tags if [[ -n ${CIRCLE_TAG:-} ]]; then - # trim 'v' prefix if present - VERSION=${CIRCLE_TAG#"v"} + VERSION=${CIRCLE_TAG#"v"} # trim the v prefix per version-tagging convention # Extract major, major.minor, and major.minor.patch versions MAJOR_VERSION=${VERSION%%.*} MINOR_VERSION=${VERSION%.*} - # Append versions to image tags - # So 2.1.1 would be tagged with "2","2.1","2.1.1" + # Reset tag list: add major, major.minor, major.minor.patch, and latest + # So 2.1.1 would be tagged with "2","2.1","2.1.1", and "latest". TAGS="$MAJOR_VERSION,$MINOR_VERSION,$VERSION,latest" fi +GIT_COMMIT=${CIRCLE_SHA1:-$(git rev-parse HEAD)} + unset GOOS unset GOARCH export KO_DOCKER_REPO=${KO_DOCKER_REPO:-ko.local} @@ -63,5 +70,5 @@ export SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH:-$(make latest_modification_time)} --platform "linux/amd64,linux/arm64" \ --image-label org.opencontainers.image.source=https://github.com/honeycombio/refinery \ --image-label org.opencontainers.image.licenses=Apache-2.0 \ - --image-label org.opencontainers.image.revision=${CIRCLE_SHA1} \ + --image-label org.opencontainers.image.revision=${GIT_COMMIT} \ ./cmd/refinery