-
Notifications
You must be signed in to change notification settings - Fork 5
/
get-docker-tags.sh
executable file
·101 lines (83 loc) · 2.96 KB
/
get-docker-tags.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
#!/usr/bin/env bash
#
# Extract the data on which docker image tags should be built from the list of git tags
#
set -o errexit
set -o pipefail
set -o nounset
declare TAG
declare ALL_TAGS
declare FOLLOWING_TAGS
# version_weight converts the semver versions into a list which can be compared using `sort -V`
version_weight () {
echo -e "$1" | tr ' ' "\n" | sed -e 's:\+.*$::' | sed -e 's:^v::' | \
sed -Ee 's:^[0-9]+(\.[0-9]+)+$:&-stable:' | \
sed -Ee 's:([^A-Za-z])dev\.?([^A-Za-z]|$):\1.10.\2:g' | \
sed -Ee 's:([^A-Za-z])(alpha|a)\.?([^A-Za-z]|$):\1.20.\3:g' | \
sed -Ee 's:([^A-Za-z])(beta|b)\.?([^A-Za-z]|$):\1.30.\3:g' | \
sed -Ee 's:([^A-Za-z])(rc|RC)\.?([^A-Za-z]|$)?:\1.40.\3:g' | \
sed -Ee 's:([^A-Za-z])stable\.?([^A-Za-z]|$):\1.50.\2:g' | \
sed -Ee 's:([^A-Za-z])pl\.?([^A-Za-z]|$):\1.60.\2:g' | \
sed -Ee 's:([^A-Za-z])(patch|p)\.?([^A-Za-z]|$):\1.70.\3:g' | \
sed -E 's:\.{2,}:.:' | \
sed -E 's:\.$::' | \
sed -E 's:-\.:.:' | \
sed -E 's:^:v:'
}
# semversort will take a list of semantic versions and sort them properly. It takes a list of versions either
# as list of arguments or as an input stream. One tag per line.
# shellcheck disable=SC2120
semversort() {
local -a tags_orig
local -a tags_weight
local -a versions_list
local -a keys
if [[ $# -gt 0 ]]; then
versions_list=$*
else
# catch pipeline output
versions_list=$(cat)
fi
# Map the tags into an array
mapfile -t tags_orig < <(echo "${versions_list}")
# Create a restructured list of versions which can be sorted
mapfile -t tags_weight < <(version_weight "${tags_orig[*]}")
# Calculate the key (index) values on the original array
keys=$(for ix in ${!tags_weight[*]}; do
printf "%s+%s\n" "${tags_weight[${ix}]}" "${ix}"
done | sort -V | cut -d+ -f2)
# Return data from the original array, sorted in ascending order
for ix in ${keys}; do
printf "%s\n" "${tags_orig[${ix}]}"
done
}
# Read the GitHub reference (we expect it to be in format "ref/tags/<tag-name>"
TAG="${GITHUB_REF:10}"
if [[ "$TAG" == v* ]]; then
# Get a list of all tags on the project, sort it by tag semantic version
ALL_TAGS="$(git tag -l | grep -E "^v*" | semversort)"
# Get a list of tags following our tag. List might be empty if this is the latest tag
FOLLOWING_TAGS="$(echo "$ALL_TAGS" | sed -e "1,/^${TAG}/ d")"
# Initialize the tag array
TAGS=""
if [[ -z "$FOLLOWING_TAGS" ]]; then
TAGS="-t boky/socketace:latest"
fi
# Gradually split the tag
# TODO: Also take into account that the tag may be followed by optional ["-" <pre-release>]["+" <build>]
while [[ -n "$TAG" ]]; do
if ! echo "${FOLLOWING_TAGS}" | grep -E -q "$TAG"; then
TAGS="$TAGS -t boky/socketace:$TAG"
fi
NEW_TAG="$(echo "$TAG" | rev | cut -d. -f2- | rev)"
if [[ "$NEW_TAG" == "$TAG" ]]; then
break
else
TAG="$NEW_TAG"
fi
done
echo "::set-env name=TAGS::$TAGS"
else
echo "::set-env name=DO_BUILD_TAG::"
fi
echo "::set-env name=DO_BUILD_TAG::1"