-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
134 lines (114 loc) · 5.38 KB
/
entrypoint.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
#!/bin/bash
# Enable console output
set -x
# Declare a read-only global symbol which each tag starts with
VERSIONSYMBOL="v"
readonly VERSIONSYMBOL
InstallDependencies () {
# Install GIT, and OpenSSH packages
apk add git openssh
}
ConfigureGit () {
# Set Git user configuration
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
# Add shared GitHub Workspace as exception due to CVE-2022-24765
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
}
InitializeTagDictionary () {
# Declare a global associated array that contains tag and commit hash pairs
declare -g -A TAGDICTIONARY
# Read each command output line and split it by space into the tag name and the tag commit
while read TAGNAME TAGCOMMIT; do
# Consider the tag name as the associated array key and the tag commit as the key value
TAGDICTIONARY["${TAGNAME}"]=$TAGCOMMIT
# Launch 'git show-ref --tag' to get the combination of existing tags and their commit hashes
# Pipeline the result with 'awk '{print $2 " " $1}' to bring tags first and then the commit hashes
# Pipeline the result with 'cut -d"/" -f3' to remove 'refs/tags/' from the beginning of the tags
# Pipeline the result with 'cut -d"${VERSIONSYMBOL}" -f2' remove the version symbol from the beginning of the tags
done < <( git ls-remote --tag | awk '{print $2 " " $1}' | cut -d"/" -f3 | cut -d"${VERSIONSYMBOL}" -f2 )
}
PushTag () {
TAGVERSION=$1
TAGNAME=$2
TAGCOMMIT=$3
# Check whether the tag alias exists or not
if [ "${TAGDICTIONARY[$TAGVERSION]}" ]; then
# Check whether the existing tag alias has the same commit hash or not
if [ "${TAGDICTIONARY[$TAGVERSION]}" == "${TAGCOMMIT}" ]; then
# Return immediately due to the equality of the existing commit hash and the requested one
return 0
else
# Delete the existing tag alias to add it again with the requested commit hash later
git tag -d "${TAGNAME}"
# Delete the tag alias from the remote repo if the test mode is not active
if [ ! $TESTMODE ]; then
git push --delete origin "${TAGNAME}" || exit 1
fi
fi
fi
# Add the tag alias with the requested commit hash
git tag "${TAGNAME}" "${TAGCOMMIT}"
# Add the tag alias to the remote repo if the test mode is not active
if [ ! $TESTMODE ]; then
git push origin "${TAGNAME}" || exit 1
fi
}
AliasVersion () {
VERSIONALIAS=$1
# Concat the version symbol with the verion alias to obtain the tag alias
TAGALIAS="${VERSIONSYMBOL}${VERSIONALIAS}"
# Concat the tag alias with '.*' to obtain the tag pattern
TAGPATTERN="${TAGALIAS}.*"
# Declare a local array that contains the matches of the tag pattern
TAGMATCHES=()
# Launch 'git tag -l "${TAGPATTERN}' to get the existing tags starts with the tag pattern
# Pipeline the result with 'cut -d"${VERSIONSYMBOL}" -f2' to remove the version symbol from the beginning of the tags
# Pipeline the result with 'sort -rV' to descendingly sort the tag matches based on version sorting
# Map the descending sorted tag matches to the declared array
mapfile -t TAGMATCHES < <( git tag -l "${TAGPATTERN}" | cut -d"${VERSIONSYMBOL}" -f2 | sort -rV )
# Fetch the latest tag according to its version
LATESTTAG="${TAGMATCHES}"
# Fetch the latest tag commit hash
TAGCOMMIT=${TAGDICTIONARY[$LATESTTAG]}
# Push the tag alias for the latest version
PushTag "${VERSIONALIAS}" "${TAGALIAS}" "${TAGCOMMIT}"
}
# Install dependencies and configure the GIT user if the test mode is not active
if [ ! $TESTMODE ]; then
InstallDependencies
ConfigureGit
fi
InitializeTagDictionary
# Declare a local array that contains the major verions based on the existing tags
MAJORVERSIONS=()
# Launch 'git tag' to get the existing tags
# Pipeline the result with 'cut -d"${VERSIONSYMBOL}" -f2' to remove the version symbol from the beginning of the tags
# Pipeline the result with 'cut -d"." -f1' to parse the major version
# Pipeline the result with 'sort -V' to sort the parsed major verions based on version sorting
# Pipeline the result with 'unique' to remove the similar major verions
# Map the unique major verions to the declared array
mapfile -t MAJORVERSIONS < <( git tag | cut -d"${VERSIONSYMBOL}" -f2 | cut -d"." -f1 | sort -V | uniq )
# Interate over all the major verions
for MAJORVERSION in "${MAJORVERSIONS[@]}"; do
# Ignore major versions less than one
if (( $MAJORVERSION < 1 )); then
continue
fi
AliasVersion "${MAJORVERSION}"
# Declare a local array that contains the minor verions of the major version
MINORVERSIONS=()
# Concat the version symbol, the major verion, and '.*' characters to obtain the pattern
PATTERN="${VERSIONSYMBOL}${MAJORVERSION}.*"
# Launch 'git tag -l "${PATTERN}' to get the existing tags starts with the pattern
# Pipeline the result with 'cut -d"${VERSIONSYMBOL}" -f2' to remove the version symbol from the beginning of the tags
# Pipeline the result with 'cut -d"." -f 1-2' to parse the major version (1) and the minor version (2)
# Pipeline the result with 'sort -V' to sort the parsed major verions based on version sorting
# Pipeline the result with 'unique' to remove the similar major verions
# Map the unique major verions to the declared array
mapfile -t MINORVERSIONS < <( git tag -l "${PATTERN}" | cut -d"${VERSIONSYMBOL}" -f2 | cut -d"." -f 1-2 | sort -V | uniq )
# Interate over all the minor verions
for MINORVERSION in "${MINORVERSIONS[@]}"; do
AliasVersion "${MINORVERSION}"
done
done