This commit is contained in:
Executable
+77
@@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env bash
|
||||
# Calculates the next git tag for the current repo and optionally pushes it.
|
||||
# Usage: auto-tag.sh
|
||||
# Inputs (env vars):
|
||||
# TAG_TYPE required tag prefix: dev, beta, prod, ...
|
||||
# TAG_ENV optional "production" switches to <type>-<max>.<major>.<minor>
|
||||
# PUSH_TAG optional "true" creates and pushes the tag (default false)
|
||||
# GIT_PUSH_TOKEN optional token with write access; required when PUSH_TAG=true
|
||||
# Outputs:
|
||||
# stdout log, tag.env (TAG, STREAM, PREVIOUS_TAG) in the current directory,
|
||||
# and TAG / STREAM / PREVIOUS_TAG written to $GITHUB_OUTPUT when it is set.
|
||||
# Requires: full tag history (checkout with fetch-depth: 0).
|
||||
set -euo pipefail
|
||||
|
||||
TAG_TYPE="${TAG_TYPE:?TAG_TYPE is required}"
|
||||
TAG_ENV="${TAG_ENV:-}"
|
||||
PUSH_TAG="${PUSH_TAG:-false}"
|
||||
|
||||
if [[ "${PUSH_TAG}" == "true" ]]; then
|
||||
: "${GIT_PUSH_TOKEN:?GIT_PUSH_TOKEN is required when PUSH_TAG=true}"
|
||||
fi
|
||||
|
||||
if [[ "${TAG_ENV}" == "production" ]]; then
|
||||
PATTERN='^'"${TAG_TYPE}"'-[0-9]+\.[0-9]+\.[0-9]+$'
|
||||
else
|
||||
PATTERN='^'"${TAG_TYPE}"'-[0-9]+$'
|
||||
fi
|
||||
|
||||
echo "auto-tag: type=${TAG_TYPE} env=${TAG_ENV:-<none>} push=${PUSH_TAG}"
|
||||
|
||||
# Latest tag with this prefix (version sort, newest first).
|
||||
PREVIOUS_TAG="$(git tag --list "${TAG_TYPE}-*" | sort -V -r | sed -n 1p)"
|
||||
|
||||
if [[ -z "${PREVIOUS_TAG}" ]]; then
|
||||
if [[ "${TAG_ENV}" == "production" ]]; then
|
||||
TAG="${TAG_TYPE}-2.0.0"
|
||||
else
|
||||
TAG="${TAG_TYPE}-1"
|
||||
fi
|
||||
else
|
||||
if ! [[ "${PREVIOUS_TAG}" =~ ${PATTERN} ]]; then
|
||||
echo "ERROR: latest tag '${PREVIOUS_TAG}' is not in the expected format for TAG_TYPE=${TAG_TYPE} (pattern ${PATTERN})" >&2
|
||||
exit 1
|
||||
fi
|
||||
VERSION="${PREVIOUS_TAG#"${TAG_TYPE}"-}"
|
||||
if [[ "${TAG_ENV}" == "production" ]]; then
|
||||
MAX="${VERSION%%.*}"
|
||||
REST="${VERSION#*.}"
|
||||
TAG="${TAG_TYPE}-$((MAX + 1)).${REST}"
|
||||
else
|
||||
TAG="${TAG_TYPE}-$((VERSION + 1))"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "previous tag: ${PREVIOUS_TAG:-<none>}"
|
||||
echo "next tag: ${TAG}"
|
||||
|
||||
{
|
||||
echo "TAG=${TAG}"
|
||||
echo "STREAM=${TAG_TYPE}"
|
||||
echo "PREVIOUS_TAG=${PREVIOUS_TAG}"
|
||||
} > tag.env
|
||||
|
||||
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
|
||||
cat tag.env >> "${GITHUB_OUTPUT}"
|
||||
fi
|
||||
|
||||
if [[ "${PUSH_TAG}" == "true" ]]; then
|
||||
SERVER_URL="${GITHUB_SERVER_URL:?GITHUB_SERVER_URL is required when PUSH_TAG=true}"
|
||||
REPO="${GITHUB_REPOSITORY:?GITHUB_REPOSITORY is required when PUSH_TAG=true}"
|
||||
PROTO="${SERVER_URL%%://*}"
|
||||
HOST="${SERVER_URL#*://}"
|
||||
git tag "${TAG}"
|
||||
git push "${PROTO}://oauth2:${GIT_PUSH_TOKEN}@${HOST}/${REPO}.git" "refs/tags/${TAG}" >/dev/null 2>&1 \
|
||||
|| { echo "ERROR: failed to push tag ${TAG} (check token scope and tag protection)" >&2; exit 1; }
|
||||
echo "pushed tag ${TAG}"
|
||||
fi
|
||||
Reference in New Issue
Block a user