This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-version.sh
executable file
·58 lines (48 loc) · 2.02 KB
/
check-version.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
#! /bin/bash
# exit when any command fails
set -e
function check_versions_consistent () {
local PACKAGE_VERSION=$(yq eval -o yaml '.version' ./package.json)
local PACKAGE_LOCK_VERSION=$(yq eval -o yaml '.version' ./package-lock.json)
if [ "$PACKAGE_VERSION" != "$PACKAGE_LOCK_VERSION" ]; then
echo "Inconsistent versions detected"
echo "PACKAGE_VERSION: $PACKAGE_VERSION"
echo "PACKAGE_LOCK_VERSION: $PACKAGE_LOCK_VERSION"
exit 1
fi
}
check_versions_consistent
function check_version_greater () {
local current=$1
local git_versions="${2:-0.0.0}"
# check if current exists in git_versions, if so not a new version
if [ -n "$(printf "$git_versions" | grep -Fx $current)" ]; then
return 1
fi
# sort all - note crazy hack to deal with prerelease versions by appending a _ character to release versions
local sorted_versions=($(printf "$git_versions\n$current" | awk '{ if ($1 ~ /-/) print; else print $0"_" ; }' | sort -rV | sed 's/_$//'))
# check if the top sorted version equals the current verison. If so we have a new version
if [ "${sorted_versions[0]}" == "$current" ]; then
return 0
else
return 1
fi
}
# Get published git tags that match semver regex with a "v" prefix then remove the "v" character
PUBLISHED_VERSIONS=$(git tag | grep "^v[0-9]\+\.[0-9]\+\.[0-9]\+\(\-[a-zA-Z-]\+\(\.[0-9]\+\)*\)\{0,1\}$" | sed 's/^v\(.*\)$/\1/')
# Get the current version from package.json
CURRENT_VERSION=$(yq eval -o yaml '.version' ./package.json)
if check_version_greater "$CURRENT_VERSION" "$PUBLISHED_VERSIONS"; then
echo "##[set-output name=VERSION;]v$CURRENT_VERSION"
echo "##[set-output name=BUILD_DATE;]$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
echo "##[set-output name=IS_NEW_VERSION;]true"
if [[ $CURRENT_VERSION =~ [-] ]]; then
echo "##[set-output name=IS_PRERELEASE;]true"
echo "##[set-output name=NPM_RELEASE_TAG;]next"
else
echo "##[set-output name=IS_PRERELEASE;]false"
echo "##[set-output name=NPM_RELEASE_TAG;]latest"
fi
else
echo "##[set-output name=IS_NEW_VERSION;]false"
fi