From 76441deae09117f34f77c4c06f8bf39a8258873d Mon Sep 17 00:00:00 2001 From: Brice Schaffner Date: Thu, 6 Jun 2024 08:37:58 +0200 Subject: [PATCH] PB-579: Fix pre-release tagging issue We could end up on a tag already exists with pre-release tag. The issue occurred when a minor pre-release tag was created followed by a patch pre-release tag, then the next pre-release tag was the same as the last patch. For example such tag history would have led to an issue - v1.11.1-beta.1 - v1.12.0-beta.1 - v1.11.0 - v1.10.0 - .... In this case the the next pre-release tag would have been v1.11.1-beta.1 instead of v1.12.0-beta.2 The issue was that pre-release bumping logic was not done correctly as the tags are sorted by semver and not how they are added. In the first place above creating a v1.11.1-beta.1 after v1.12.0-beta.1 was already not correct and should not have happened. This was du to the fact that the first pre-release tag was done using the default bump type of minor and that the second pre-release tag was created with a #patch in a commit message which should have resulted to v1.12.1-beta.1 or v1.12.0-beta.2 and not v1.11.1-beta.1. Now if a pre-release tag already exist we bump it (ignoring the bump type) unless the bump type is higher as the previous pre-release. This should add a more consistent way of tagging. --- entrypoint.sh | 66 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index b5955c4..5f4935c 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -14,6 +14,12 @@ tag_context=${TAG_CONTEXT:-repo} suffix=${PRERELEASE_SUFFIX:-beta} verbose=${VERBOSE:-true} +if [[ -z "${suffix}" ]] +then + echo "::error::PRERELEASE_SUFFIX is set to an empty string" + exit 1 +fi + # since https://github.blog/2022-04-12-git-security-vulnerability-announced/ runner uses? if [[ "${source}" =~ ^\.$ ]] then @@ -84,6 +90,9 @@ case "${tag_context}" in ;; esac +echo "Last tag: ${tag}" +echo "Last pre-release tag: ${pre_tag}" + # if there are none, start tags at INITIAL_VERSION which defaults to 0.0.0 if [[ -z "${tag}" ]] @@ -114,45 +123,59 @@ fi # echo log if verbose is wanted if ${verbose} then - echo "${log}" + echo "Git logs" + echo "---------------------------" + echo "${log}" + echo "---------------------------" fi case "${log}" in - *#major* ) new=$(semver -i major "${tag}"); part="major";; - *#minor* ) new=$(semver -i minor "${tag}"); part="minor";; - *#patch* ) new=$(semver -i patch "${tag}"); part="patch";; - *#none* ) - echo "Default bump was set to none. Skipping..." - echo "new_tag=${tag}" >> "${GITHUB_OUTPUT}" - echo "tag=${tag}" >> "${GITHUB_OUTPUT}" - exit 0 - ;; + *#major* ) part="major";; + *#minor* ) part="minor";; + *#patch* ) part="patch";; + *#none* ) part="none" ;; * ) if [[ "${default_semvar_bump}" == "none" ]] then - echo "Default bump was set to none. Skipping..." - echo "new_tag=${tag}" >> "${GITHUB_OUTPUT}" - echo "tag=${tag}" >> "${GITHUB_OUTPUT}" - exit 0 + part="none" else - new=$(semver -i "${default_semvar_bump}" "${tag}"); part=${default_semvar_bump} + part="${default_semvar_bump}" fi ;; esac +echo "Bumping type set to ${part}" +echo "part=${part}" >> "${GITHUB_OUTPUT}" + +if [[ "${part}" == "none" ]] +then + echo "Default bump was set to none. Skipping..." + # shellcheck disable=SC2129 + echo "new_tag=${tag}" >> "${GITHUB_OUTPUT}" + echo "tag=${tag}" >> "${GITHUB_OUTPUT}" + exit 0 +fi if ${pre_release} then - # Already a prerelease available, bump it - if [[ "${pre_tag}" == *"${new}"* ]] + if [[ "${tag}" == "${pre_tag}" ]] then - new=$(semver -i prerelease "${pre_tag}" --preid "${suffix}"); part="pre-${part}" + echo "First pre-release tag detected, create initial pre-release tag using bump type ${part}" + new=$(semver -i "pre${part}" "${pre_tag}" --preid "${suffix}") else - new="${new}-${suffix}.1"; part="pre-${part}" + echo "Pre-release tag already exists, increment it based on bump type ${part}" + new=$(semver -i "prerelease" "${pre_tag}" --preid "${suffix}") + next_release=$(semver -i "${part}" "${pre_tag}") + if [[ "${new}" != ${next_release}* ]] + then + # the next release is different higher as the next pre-release, therefore bump a new pre + # release part (e.g. from 1.1.1-beta.2 to 1.2.0-beta.0) + new=$(semver -i "pre${part}" "${pre_tag}" --preid "${suffix}") + fi fi +else + new=$(semver -i "${part}" "${tag}"); fi -echo "${part}" - # prefix with 'v' if ${with_v} then @@ -174,7 +197,6 @@ fi # set outputs echo "new_tag=${new}" >> "${GITHUB_OUTPUT}" -echo "part=${part}" >> "${GITHUB_OUTPUT}" # use dry run to determine the next tag if ${dryrun}