-
Notifications
You must be signed in to change notification settings - Fork 1
/
compose-to-matrix.sh
executable file
·229 lines (198 loc) · 5.6 KB
/
compose-to-matrix.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
set -e
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Console step increment
i=1
# Get versions
DOCKER_VERSION="$(docker --version)"
# Default
REGISTRY="docker.io"
# TAGS="latest"
COMMIT_SHA="$(git rev-parse --short HEAD)"
PLATFORMS="linux/amd64"
CSV=false
RECURSIVE=false
unset MAJOR_VERSION
unset MINOR_VERSION
unset PATCH_VERSION
# Declare script helper
TEXT_HELPER="\nThis script aims to build matrix for Github CI/CD. It will parse the given docker-compose file and return a json object with images infos (name, tag, context, dockerfile and if it need to be build)
Following flags are available:
-a Create recursive tags, if it match 'x.x.x' it will create 'x.x' and 'x'.
-c Use csv list formated output for tags instead of json array.
-f Docker-compose file used to build matrix.
-n Namespace used to tag images. e.g 'username/reponame'.
-p Target platforms used to build matrix (List/CSV format. ex: 'linux/amd64,linux/arm64').
Default is '$PLATFORMS'.
-r Registry host used to build matrix.
Default is '$REGISTRY'.
-t Docker tag used to build matrix.
Default is '$TAGS'.
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hacf:n:p:r:t: flag; do
case "${flag}" in
a)
RECURSIVE=true;;
c)
CSV=true;;
f)
COMPOSE_FILE=${OPTARG};;
n)
NAMESPACE=${OPTARG};;
p)
PLATFORMS=${OPTARG};;
r)
REGISTRY=${OPTARG};;
t)
TAGS=${OPTARG};;
h | *)
print_help
exit 0;;
esac
done
# Script condition
if [ ! -f "$(readlink -f $COMPOSE_FILE)" ]; then
echo "\nDocker compose file $COMPOSE_FILE does not exist."
print_help
exit 1
fi
if [ "$REGISTRY" ] && [[ "$REGISTRY" != */ ]]; then
REGISTRY="$REGISTRY/"
fi
if [ "$NAMESPACE" ] && [[ "$NAMESPACE" != */ ]]; then
NAMESPACE="$NAMESPACE/"
fi
# Build core matrix
MATRIX=$(cat "$COMPOSE_FILE" \
| docker run -i --rm mikefarah/yq -o=json \
| jq \
--arg d "$(dirname $(readlink -f $COMPOSE_FILE))" \
--arg p "$PLATFORMS" \
--arg r "$REGISTRY" \
--arg t "$TAGS" \
'.services | to_entries | map({
image: (.value.image),
name: (.value.image | split(":")[0] | split("/")[-1]),
defaultTag: (.value.image | split(":")[1]),
build: (
if .value.build then {
context: ($d + "/" + .value.build.context),
dockerfile: ($d + "/" + .value.build.context + "/" + .value.build.dockerfile),
target: (.value.build.target),
platforms: [],
tags: []
}
else
false
end)
})')
# Add tags in matrix
if [ ! -z "$TAGS" ]; then
for t in $(echo $TAGS | tr "," "\n"); do
if [[ "$t" == *"."*"."* ]] && [[ "$RECURSIVE" == "true" ]]; then
MAJOR_VERSION="$(echo $t | cut -d "." -f 1)"
MINOR_VERSION="$(echo $t | cut -d "." -f 2)"
PATCH_VERSION="$(echo $t | cut -d "." -f 3)"
MATRIX=$(echo "$MATRIX" \
| jq \
--arg r "$REGISTRY" \
--arg n "$NAMESPACE" \
--arg major "$MAJOR_VERSION" \
--arg minor "$MINOR_VERSION" \
'map(. |
if .build != false then
.build.tags += [
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":" + $major),
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":" + $major + "." + $minor)
]
else
.
end
)')
fi
MATRIX=$(echo "$MATRIX" \
| jq \
--arg t "$t" \
--arg r "$REGISTRY" \
--arg n "$NAMESPACE" \
'map(. |
if .build != false then
.build.tags += [
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":" + $t)
]
else
.
end
)')
done
else
MATRIX=$(echo "$MATRIX" \
| jq \
--arg r "$REGISTRY" \
--arg n "$NAMESPACE" \
'map(. |
if .build != false then
if .defaultTag | test("^[0-9].[0-9].[0-9]") then
.build.tags += [
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":" + (.defaultTag | split(".")[0])),
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":" + (.defaultTag | split(".")[0]) + "." + (.defaultTag | split(".")[1])),
($r + $n + .image),
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":latest")
]
else
.build.tags += [
($r + $n + .image),
($r + $n + (.image | split(":")[0] | split("/")[-1]) + ":latest")
]
end
else
.
end
)')
fi
# Add platforms in matrix
for p in $(echo $PLATFORMS | tr "," "\n"); do
MATRIX=$(echo "$MATRIX" \
| jq \
--arg p "$p" \
'map(. |
if .build != false then
.build.platforms += [
($p)
]
else
.
end
)')
done
# Update image key with first tag
MATRIX=$(echo "$MATRIX" \
| jq \
--arg t "$TAGS" \
'map(. |
if .build != false then
.image = (.image | split(":")[0] | split("/")[-1] + ":" + ($t | split(",")[0]))
else
.
end
)')
# Convert tags & platforms from json array to csv list (use join instead of @csv to don't get unwanted quotes)
if [ "$CSV" == "true" ]; then
MATRIX=$(echo "$MATRIX" \
| jq -r \
'map(. |
if .build != false then
.build.tags = (.build.tags | join(",")) |
.build.platforms = (.build.platforms | join(","))
else
.
end
)')
fi
echo "$MATRIX" | jq .