Files
version-increment/version-increment.sh

149 lines
5.8 KiB
Bash
Raw Normal View History

2021-10-20 11:18:23 +11:00
#!/bin/bash
set -euo pipefail
# https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# shellcheck source=shared.sh
source "${script_dir}/shared.sh"
increment="${increment:-patch}"
2021-10-20 11:18:23 +11:00
if [[ "${increment}" != 'patch' && "${increment}" != 'minor' && "${increment}" != 'major' ]] ; then
echo "🛑 Value of 'increment' is not valid, choose from 'major', 'minor', or 'patch'" 1>&2
2021-10-21 23:38:24 +11:00
input_errors='true'
fi
if [[ -z "${current_version:-}" ]] ; then
echo "🛑 Environment variable 'current_version' is unset or empty" 1>&2
input_errors='true'
elif [[ -z "$(echo "${current_version}" | grep_p "${pcre_master_ver}")" ]] ; then
echo "🛑 Environment variable 'current_version' is not a valid normal version (M.m.p)" 1>&2
2021-10-21 23:38:24 +11:00
input_errors='true'
2021-10-20 11:18:23 +11:00
fi
2021-10-21 23:38:24 +11:00
if [[ "${input_errors}" == 'true' ]] ; then
2021-10-20 11:18:23 +11:00
exit 8
fi
##==----------------------------------------------------------------------------
## Git info - branch names, commit short ref
default_branch='main'
2023-08-25 16:46:55 +01:00
# use release_branch if not empty
2023-09-18 08:55:00 +10:00
if [[ -n "${release_branch:-}" ]] ; then
default_branch="${release_branch}"
elif [[ -z "${BATS_VERSION:-}" ]] ; then
# if we're _not_ testing, then _actually_ check the origin
if [[ "${use_api:-}" == 'true' ]] ; then
default_branch="$(
curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${github_token}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}" \
| jq -r '.default_branch'
)"
else
2024-04-24 10:58:22 +10:00
default_branch="$(git remote show origin | grep 'HEAD branch' | cut -d ' ' -f 5)"
fi
2023-08-25 16:46:55 +01:00
fi
2021-10-20 11:18:23 +11:00
current_ref="${GITHUB_REF:-}"
git_commit_sha=${GITHUB_SHA:-}
if [[ "${use_api:-}" == 'true' ]] ; then
# because we cannot use `rev-parse` with the API, we'll take a punt that 9 characters is enough for uniqueness
# shellcheck disable=SC2001
git_commit="$(echo "${GITHUB_SHA:0:9}" | sed 's/0*//')" # Also, trim leading zeros, because semver doesn't allow that in
else # the 'pre-release version' part, but we can't use the + char
git_commit="$(git rev-parse --short HEAD | sed 's/0*//')" # to make it 'build metadata' as that's not supported in K8s
git_commit_sha="$(git rev-parse --short HEAD)" # labels
fi
##==----------------------------------------------------------------------------
## Conventional commits
if [[ "${scheme}" == 'conventional_commits' ]] ; then
# Get message from given commit
commit_message=$(git log -1 --pretty=format:%B "${git_commit_sha}")
# Check commit message header
found_match='false'
2024-04-24 10:58:22 +10:00
if [[ -n "$(echo "${commit_message}" | grep_p "${pcre_conventional_commit_breaking}")" ]] ; then
increment='major'
found_match='true'
2024-04-24 10:58:22 +10:00
elif [[ -n "$(echo "${commit_message}" | grep_p "${pcre_conventional_commit_minor}")" ]] ; then
increment='minor'
found_match='true'
2024-04-24 10:58:22 +10:00
elif [[ -n "$(echo "${commit_message}" | grep_p "${pcre_conventional_commit_patch}")" ]] ; then
increment='patch'
found_match='true'
fi
if [[ "${found_match}" == 'false' ]] ; then
echo "⚠️ No conventional commit found, defaulting to ${increment} increment" 1>&2
fi
fi
2021-10-20 11:18:23 +11:00
##==----------------------------------------------------------------------------
## Version increment
# increment the month if needed
if [[ "${scheme}" == "calver" ]] ; then
month="$(date '+%Y.%-m.')"
release="${current_version//$month/}"
if [[ "${release}" == "${current_version}" ]] ; then
current_version="$(date '+%Y.%-m.0')"
fi
fi
# increment the patch digit
IFS=" " read -r -a version_array <<< "${current_version//./ }"
if [[ "${increment}" == 'patch' || "${scheme}" == 'calver' ]] ; then
(( ++version_array[2] ))
elif [[ "${increment}" == 'minor' ]] ; then
(( ++version_array[1] ))
version_array[2]='0'
elif [[ "${increment}" == 'major' ]] ; then
(( ++version_array[0] ))
version_array[1]='0'
version_array[2]='0'
fi
2024-08-31 14:38:32 +10:00
new_version="${version_array[0]}.${version_array[1]}.${version_array[2]}"
2021-10-20 11:18:23 +11:00
# check we haven't accidentally forgotten to set scheme to calver
# TODO: provide an override "I know my version numbers are > 2020, but it's semver!" option
if [[ "${version_array[0]}" -gt 2020 && "${scheme}" != "calver" ]] ; then
echo "🛑 The major version number is greater than 2020, but the scheme is not set to 'calver'" 1>&2
exit 11
fi
# add pre-release info to version if not the default branch
if [[ "${current_ref}" != "refs/heads/${default_branch}" ]] ; then
pre_release="pre.${git_commit}"
if [[ "${pep440:-}" == 'true' ]] ; then
2023-04-26 15:18:48 +10:00
new_version="${new_version}+${pre_release}"
else
new_version="${new_version}-${pre_release}"
fi
echo "PRE_RELEASE_LABEL=${pre_release}" >> "${GITHUB_OUTPUT}"
2021-10-20 11:18:23 +11:00
fi
2024-04-24 10:58:22 +10:00
if [[ -z "$(echo "${new_version}" | grep_p "${pcre_semver}")" ]] ; then
2021-10-20 11:18:23 +11:00
echo "🛑 Version incrementing has failed to produce a semver compliant version" 1>&2
echo " See: https://semver.org/spec/v2.0.0.html" 1>&2
echo " Failed version string: '${new_version}'" 1>&2
exit 12
fi
echo " The new version is ${new_version}"
2022-10-19 09:36:31 +11:00
# shellcheck disable=SC2129
echo "VERSION=${new_version}" >> "${GITHUB_OUTPUT}"
2024-08-31 14:38:32 +10:00
echo "V_VERSION=v${new_version}" >> "${GITHUB_OUTPUT}"
echo "MAJOR_VERSION=${version_array[0]}" >> "${GITHUB_OUTPUT}"
echo "MINOR_VERSION=${version_array[1]}" >> "${GITHUB_OUTPUT}"
echo "PATCH_VERSION=${version_array[2]}" >> "${GITHUB_OUTPUT}"
echo "MAJOR_V_VERSION=v${version_array[0]}" >> "${GITHUB_OUTPUT}"
echo "MINOR_V_VERSION=v${version_array[1]}" >> "${GITHUB_OUTPUT}"
echo "PATCH_V_VERSION=v${version_array[2]}" >> "${GITHUB_OUTPUT}"