-
Notifications
You must be signed in to change notification settings - Fork 15
/
version-increment.sh
executable file
·148 lines (128 loc) · 5.81 KB
/
version-increment.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/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}"
if [[ "${increment}" != 'patch' && "${increment}" != 'minor' && "${increment}" != 'major' ]] ; then
echo "🛑 Value of 'increment' is not valid, choose from 'major', 'minor', or 'patch'" 1>&2
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
input_errors='true'
fi
if [[ "${input_errors}" == 'true' ]] ; then
exit 8
fi
##==----------------------------------------------------------------------------
## Git info - branch names, commit short ref
default_branch='main'
# use release_branch if not empty
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
default_branch="$(git remote show origin | grep 'HEAD branch' | cut -d ' ' -f 5)"
fi
fi
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'
if [[ -n "$(echo "${commit_message}" | grep_p "${pcre_conventional_commit_breaking}")" ]] ; then
increment='major'
found_match='true'
elif [[ -n "$(echo "${commit_message}" | grep_p "${pcre_conventional_commit_minor}")" ]] ; then
increment='minor'
found_match='true'
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
##==----------------------------------------------------------------------------
## 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
new_version="${version_array[0]}.${version_array[1]}.${version_array[2]}"
# 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
new_version="${new_version}+${pre_release}"
else
new_version="${new_version}-${pre_release}"
fi
echo "PRE_RELEASE_LABEL=${pre_release}" >> "${GITHUB_OUTPUT}"
fi
if [[ -z "$(echo "${new_version}" | grep_p "${pcre_semver}")" ]] ; then
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}"
# shellcheck disable=SC2129
echo "VERSION=${new_version}" >> "${GITHUB_OUTPUT}"
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}"