-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-release.sh
executable file
·136 lines (119 loc) · 4.22 KB
/
make-release.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
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Author: William Desportes <[email protected]>
ENV=""
TAG_NAME=""
PUSH_TAG=0
SHOW_HELP=0
OFFLINE=0
VERSION_SEPARATOR='-'
# Source: https://stackoverflow.com/a/46793269/5155484 and https://stackoverflow.com/a/28466267/5155484
optspec="hpo-:e:n:"
while getopts "$optspec" OPTCHAR; do
if [ "$OPTCHAR" = "-" ]; then # long option: reformulate OPT and OPTARG
OPTCHAR="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#$OPT}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
OPTARG=${OPTARG#*=}
# echo "OPTARG: ${OPTARG[*]}"
# echo "OPTIND: ${OPTIND[*]}"
# echo "OPTCHAR: ${OPTCHAR}"
case "${OPTCHAR}" in
h|help)
SHOW_HELP=1
;;
o|offline)
OFFLINE=1
;;
p|push-tag)
PUSH_TAG=1
;;
n|tag-name)
TAG_NAME="${OPTARG}"
;;
e|env)
ENV="${OPTARG}"
;;
*)
if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
echo "Non-option argument: '-${OPTARG}'" >&2
fi
;;
esac
done
shift $((OPTIND-1)) # remove parsed options and args from $@ list
if [ ${SHOW_HELP} -gt 0 ]; then
echo 'Usage:'
echo './make-release.sh --env=5.2 -p'
echo './make-release.sh --env=6.0 -p'
echo 'POSIX options: long options:'
echo ' -h --help To have some help'
echo ' -e --env= To specify the env (5.2/6.0)'
echo ' -n --tag-name= To specify the tag name'
echo ' -p --push-tag To push the tag'
echo ' -o --offline Do not fetch tags'
exit 0;
fi
if [ -z $ENV ]; then
echo "please enter a --env"
exit 1
fi
if [ ${OFFLINE} -eq 0 ]; then
echo "Fetching latest tags..."
git fetch --prune origin "+refs/tags/*:refs/tags/*"
fi
echo "Get last release"
TODAY_TAG="$ENV/$(date +'%Y-%m-%d')"
# Last tag at the end sorted by the format of the tag
DAY_TAGS=$(git tag -l HEAD "$TODAY_TAG*" | sort -t / -k 2,2 -n)
# No tag name defined so use the latest tag
if [ -z "${TAG_NAME}" ]; then
# The tag name is not defined
LAST_RELEASE=$(echo -e "${DAY_TAGS}" | tail -n1)
else
# Tag name defined so use the last tag before last one (pick two, use the first one)
LAST_RELEASE=$(echo -e "${DAY_TAGS}" | grep -v -F "${TAG_NAME}" | tail -n1)
fi
if [ -z "$LAST_RELEASE" ]; then
echo "None today, creating first one"
LAST_RELEASE=$(echo "$TODAY_TAG-0");# will be +1 below
# Last found release for ENV
# No tag name defined so use the latest tag
# Last tag at the end sorted by the format of the tag
TWO_LAST_TAGS=$(git tag -l HEAD "$ENV/*" | sort -t / -k 2,2 -n | tail -n2)
if [ -z "${TAG_NAME}" ]; then
# The tag name is not defined
PREVIOUS_RELEASE=$(echo -e "${TWO_LAST_TAGS}" | tail -n1)
else
# If the tag name is already defined then strip it off from the list and then use what will be the last tag
PREVIOUS_RELEASE=$(echo -e "${TWO_LAST_TAGS}" | grep -v -F "${TAG_NAME}" | tail -n1)
fi
echo "Found previous release: $PREVIOUS_RELEASE"
else
PREVIOUS_RELEASE="$LAST_RELEASE"
echo "Found previous release: $PREVIOUS_RELEASE"
fi
echo "Version bump..."
if [ -z "${TAG_NAME}" ]; then
# Cut on last - and bump last number
VERSION_NAME=$(echo "${LAST_RELEASE}" | awk -F"${VERSION_SEPARATOR}" '{print substr($0, 0, length($0) - length($NF)) $NF + 1 }')
else
VERSION_NAME="$TAG_NAME"
fi
echo "New version: $VERSION_NAME"
if [ -z "${TAG_NAME}" ]; then
echo "TAG_NAME=${VERSION_NAME}" >> $GITHUB_OUTPUT
git tag --sign --message="release: $VERSION_NAME
user: $USER" $VERSION_NAME
if [ ${PUSH_TAG} -eq 1 ]; then
git push origin $VERSION_NAME
fi
else
echo "TAG_NAME=${TAG_NAME}" >> $GITHUB_OUTPUT
echo "Using tag: ${TAG_NAME}"
fi
TAG_WORKS=$?
exit $TAG_WORKS