build: correctly parse package version

This commit is contained in:
Ivan Dyachkov 2023-08-10 08:49:49 +02:00
parent d6476481c9
commit 1c5b42806b
4 changed files with 69 additions and 8 deletions

12
build
View File

@ -378,11 +378,11 @@ make_docker() {
local EMQX_DOCKERFILE="${EMQX_DOCKERFILE:-deploy/docker/Dockerfile}"
local PKG_VSN="${PKG_VSN:-$(./pkg-vsn.sh)}"
# shellcheck disable=SC2155
local VSN_MAJOR="$(echo "$PKG_VSN" | cut -d . -f 1)"
local VSN_MAJOR="$(scripts/semver.sh "$PKG_VSN" --major)"
# shellcheck disable=SC2155
local VSN_MINOR="$(echo "$PKG_VSN" | cut -d . -f 2)"
local VSN_MINOR="$(scripts/semver.sh "$PKG_VSN" --minor)"
# shellcheck disable=SC2155
local VSN_PATCH="$(echo "$PKG_VSN" | cut -d . -f 3)"
local VSN_MINOR="$(scripts/semver.sh "$PKG_VSN" --patch)"
local SUFFIX=''
if [[ "$PROFILE" = *-elixir ]]; then
SUFFIX="-elixir"
@ -430,8 +430,6 @@ make_docker() {
--label org.opencontainers.image.licenses="${LICENSE}" \
--label org.opencontainers.image.otp.version="${EMQX_BUILDER_OTP}" \
--tag "${EMQX_IMAGE_TAG}" \
--tag "${EMQX_BASE_DOCKER_TAG}:${VSN_MAJOR}.${VSN_MINOR}${SUFFIX}" \
--tag "${EMQX_BASE_DOCKER_TAG}:${VSN_MAJOR}.${VSN_MINOR}.${VSN_PATCH}${SUFFIX}" \
--provenance false \
--pull
)
@ -442,7 +440,9 @@ make_docker() {
DOCKER_BUILDX_ARGS+=(--label org.opencontainers.image.elixir.version="${EMQX_BUILDER_ELIXIR}")
fi
if [ "${DOCKER_LATEST:-false}" = true ]; then
DOCKER_BUILDX_ARGS+=(--tag "${DOCKER_REGISTRY}/${DOCKER_ORG}/${PROFILE}:latest${SUFFIX}")
DOCKER_BUILDX_ARGS+=(--tag "${EMQX_BASE_DOCKER_TAG}:latest${SUFFIX}")
DOCKER_BUILDX_ARGS+=(--tag "${EMQX_BASE_DOCKER_TAG}:${VSN_MAJOR}.${VSN_MINOR}${SUFFIX}")
DOCKER_BUILDX_ARGS+=(--tag "${EMQX_BASE_DOCKER_TAG}:${VSN_MAJOR}.${VSN_MINOR}.${VSN_PATCH}${SUFFIX}")
fi
if [ "${DOCKER_PLATFORMS:-default}" != 'default' ]; then
DOCKER_BUILDX_ARGS+=(--platform "${DOCKER_PLATFORMS}")

29
scripts/semver.sh Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -e
function parseSemver() {
local RE='^([0-9]+)\.([0-9]+)\.([0-9]+)(-([a-z]+\.[0-9]+))?$'
echo "$1" | grep -qE "$RE" || exit 1
#shellcheck disable=SC2155
local MAJOR=$( echo "$1" | sed -r "s#$RE#\1#")
#shellcheck disable=SC2155
local MINOR=$( echo "$1" | sed -r "s#$RE#\2#")
#shellcheck disable=SC2155
local PATCH=$( echo "$1" | sed -r "s#$RE#\3#")
#shellcheck disable=SC2155
local SPECIAL=$(echo "$1" | sed -r "s#$RE#\5#")
case "${2}" in
--major) echo "${MAJOR}" ;;
--minor) echo "${MINOR}" ;;
--patch) echo "${PATCH}" ;;
--special) echo "${SPECIAL}" ;;
*)
cat <<EOF
{"major": ${MAJOR}, "minor": ${MINOR}, "patch": ${PATCH}, "special": "${SPECIAL}"}
EOF
;;
esac
}
parseSemver "$1" "$2"

View File

@ -7,13 +7,13 @@ exit_code=0
for test in shelltest/*.test; do
echo "Running $test"
/bin/sh "${test%.test}.setup"
[ -f "${test%.test}.setup" ] && /bin/sh "${test%.test}.setup"
shelltest -c --diff --all --precise -- "$test"
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
exit_code=1
fi
/bin/sh "${test%.test}.cleanup"
[ -f "${test%.test}.cleanup" ] && /bin/sh "${test%.test}.cleanup"
done
exit $exit_code

View File

@ -0,0 +1,32 @@
./semver.sh 5.2.0.1
>>>= 1
./semver.sh 5.1.0
>>>
{"major": 5, "minor": 1, "patch": 0, "special": ""}
>>>= 0
./semver.sh 5.1.0-patch.3
>>>
{"major": 5, "minor": 1, "patch": 0, "special": "patch.3"}
>>>= 0
./semver.sh 5.1.0-patch.3 --major
>>>
5
>>>= 0
./semver.sh 5.1.0-patch.3 --minor
>>>
1
>>>= 0
./semver.sh 5.1.0-patch.3 --patch
>>>
0
>>>= 0
./semver.sh 5.1.0-patch.3 --special
>>>
patch.3
>>>= 0