120 lines
3.8 KiB
Bash
Executable File
120 lines
3.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
: "${INPUT_CONFIG_FILE:?config-file is required}"
|
|
: "${INPUT_VERSION_FILE:=}"
|
|
: "${INPUT_BASE_BRANCH:?base-branch is required}"
|
|
: "${INPUT_TARGET_BRANCH:?target-branch is required}"
|
|
: "${INPUT_BUMP:?bump is required}"
|
|
: "${INPUT_REMOTE:?remote is required}"
|
|
: "${INPUT_COMMIT_MESSAGE:?commit-message is required}"
|
|
: "${INPUT_GIT_USER_NAME:?git-user-name is required}"
|
|
: "${INPUT_GIT_USER_EMAIL:?git-user-email is required}"
|
|
: "${INPUT_BUMP_MY_VERSION_VERSION:?bump-my-version-version is required}"
|
|
: "${INPUT_PUSH:?push is required}"
|
|
: "${GITHUB_OUTPUT:?GITHUB_OUTPUT is required}"
|
|
|
|
case "${INPUT_PUSH}" in
|
|
true | false) ;;
|
|
*) echo "push must be 'true' or 'false'" >&2; exit 1 ;;
|
|
esac
|
|
|
|
case "${INPUT_BUMP}" in
|
|
major | minor | patch) ;;
|
|
*) echo "bump must be major, minor, or patch" >&2; exit 1 ;;
|
|
esac
|
|
|
|
if [[ ! -f "${INPUT_CONFIG_FILE}" ]]; then
|
|
echo "Configuration file not found: ${INPUT_CONFIG_FILE}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -n "$(git status --porcelain)" ]]; then
|
|
echo "Working tree must be clean before bumping the version" >&2
|
|
exit 1
|
|
fi
|
|
|
|
current_branch=$(git branch --show-current)
|
|
if [[ "${current_branch}" != "${INPUT_TARGET_BRANCH}" ]]; then
|
|
echo "Expected checked-out branch '${INPUT_TARGET_BRANCH}', found '${current_branch:-detached HEAD}'" >&2
|
|
exit 1
|
|
fi
|
|
|
|
git fetch "${INPUT_REMOTE}" \
|
|
"${INPUT_BASE_BRANCH}:refs/remotes/${INPUT_REMOTE}/${INPUT_BASE_BRANCH}"
|
|
|
|
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
|
|
base_ref="refs/remotes/${INPUT_REMOTE}/${INPUT_BASE_BRANCH}"
|
|
current_version=$(python3 "${script_dir}/read_version.py" "${INPUT_CONFIG_FILE}")
|
|
base_version_args=("${INPUT_CONFIG_FILE}" --ref "${base_ref}")
|
|
if [[ -n "${INPUT_VERSION_FILE}" ]]; then
|
|
base_version_args+=(--fallback-version-file "${INPUT_VERSION_FILE}")
|
|
fi
|
|
base_version=$(python3 "${script_dir}/read_version.py" "${base_version_args[@]}")
|
|
|
|
comparison=$(python3 - "${current_version}" "${base_version}" <<'PY'
|
|
import re
|
|
import sys
|
|
|
|
pattern = re.compile(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$")
|
|
versions = []
|
|
for value in sys.argv[1:]:
|
|
match = pattern.fullmatch(value)
|
|
if not match:
|
|
raise SystemExit(f"Version must use stable MAJOR.MINOR.PATCH format: {value}")
|
|
versions.append(tuple(map(int, match.groups())))
|
|
|
|
print((versions[0] > versions[1]) - (versions[0] < versions[1]))
|
|
PY
|
|
)
|
|
|
|
if [[ "${comparison}" == "1" ]]; then
|
|
echo "Version ${current_version} is already prepared (base: ${base_version})"
|
|
{
|
|
echo "bumped=false"
|
|
echo "previous-version=${current_version}"
|
|
echo "current-version=${current_version}"
|
|
echo "commit="
|
|
} >> "${GITHUB_OUTPUT}"
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "${comparison}" == "-1" ]]; then
|
|
echo "Target branch version ${current_version} is older than base branch version ${base_version}" >&2
|
|
echo "Synchronize ${INPUT_TARGET_BRANCH} with ${INPUT_BASE_BRANCH} before releasing" >&2
|
|
exit 1
|
|
fi
|
|
|
|
venv_dir=$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/bump-erp-app-version.XXXXXX")
|
|
trap 'rm -rf "${venv_dir}"' EXIT
|
|
python3 -m venv "${venv_dir}"
|
|
"${venv_dir}/bin/pip" install --disable-pip-version-check \
|
|
"bump-my-version==${INPUT_BUMP_MY_VERSION_VERSION}"
|
|
|
|
git config user.name "${INPUT_GIT_USER_NAME}"
|
|
git config user.email "${INPUT_GIT_USER_EMAIL}"
|
|
|
|
message=${INPUT_COMMIT_MESSAGE//\{version\}/\{new_version\}}
|
|
"${venv_dir}/bin/bump-my-version" bump \
|
|
--config-file "${INPUT_CONFIG_FILE}" \
|
|
--commit \
|
|
--no-tag \
|
|
--message "${message}" \
|
|
"${INPUT_BUMP}"
|
|
|
|
new_version=$(python3 "${script_dir}/read_version.py" "${INPUT_CONFIG_FILE}")
|
|
commit=$(git rev-parse HEAD)
|
|
|
|
if [[ "${INPUT_PUSH}" == "true" ]]; then
|
|
git push "${INPUT_REMOTE}" "HEAD:${INPUT_TARGET_BRANCH}"
|
|
fi
|
|
|
|
{
|
|
echo "bumped=true"
|
|
echo "previous-version=${current_version}"
|
|
echo "current-version=${new_version}"
|
|
echo "commit=${commit}"
|
|
} >> "${GITHUB_OUTPUT}"
|
|
|
|
echo "Prepared ${current_version} -> ${new_version} (${commit})"
|