-
Notifications
You must be signed in to change notification settings - Fork 5
/
run
executable file
·352 lines (301 loc) · 11.2 KB
/
run
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/sh
set -e
url="https://raw.githubusercontent.com/webuni/shell-task-runner/master/runner"
[ -f ./.runner ] && [ .runner -nt "$0" ] || wget -q "$url" -O- > .runner 2> /dev/null || curl -fso .runner "$url"
. ./.runner
if [ -r ./.env ]; then
. ./.env
fi
IMAGE_NAMESPACE="${IMAGE_NAMESPACE:-minidocks}"
GIT_NAMESPACE="${GIT_NAMESPACE:-minidocks}"
HUB_NAMESPACE="${HUB_NAMESPACE:-minidocks}"
HUB_URL=https://hub.docker.com/v2
HUB_API_URL=https://hub.docker.com/api/build/v1
to_json(){
printf '{"%s"}' "$(echo "$1" | sed -E 's/\\/\\\\/g; s/"/\\"/g; s/=/": "/g; s/&/", "/g; s/"(true|false)"/\1/g')"
}
json_escape()(
printf "%s" "$1" | sed 's/\\/\\\\/g;s/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g'
)
http_code(){
printf "%s" "$1" | head -n1 | cut -d" " -f2
}
http_body(){
printf "%s" "$1" | sed '1,/^$/d'
}
assert_http_code(){
if [ "$(http_code "$1")" != "$2" ]; then
printf "\033[30;41m%s\033[0m\n" "$1" >&2
exit 1
fi
}
query()(
json="$1"
shift
printf '%s' "$json" | _docker_run minidocks/curl jq "$@"
)
do_request(){
_docker_run minidocks/curl \
curl --retry 5 --retry-delay 3 -H "Content-Type: application/json" -H "Accept: application/json" -i -s "$@" \
| tr -d '\r' | tr '\n' '\r' | sed -e 's/.\+\(HTTP\/1.\+\)/-\1-/' | tr '\r' '\n'
}
run_git(){
_docker_run -e FILE_NETRC_PATH="/home/user/.netrc" -e FILE_NETRC_CONTENT="machine github.com login token password $GITHUB_TOKEN" minidocks/git git "$@"
}
github(){
do_request -H "Authorization: token $GITHUB_TOKEN" -X "$1" --data "$3" "https://api.github.com$2"
}
hub(){
if [ -z "$REGISTRY_TOKEN" ] || [ $(( $(date +"%s") - REGISTRY_TOKEN_TIME )) -gt 300 ]; then
response="$(do_request -X "POST" --data "{\"username\": \"$(json_escape "$DOCKER_LOGIN")\", \"password\": \"$(json_escape "$DOCKER_PASSWORD")\"}" "$HUB_URL/users/login/")"
assert_http_code "$response" "200"
REGISTRY_TOKEN="$(query "$(http_body "$response")" -r '.token')"
REGISTRY_TOKEN_TIME="$(date +"%s")"
fi
do_request -H "Authorization: JWT $REGISTRY_TOKEN" -X "$1" --data "$3" "${4:-$HUB_URL}$2"
}
hub_api()(
hub "$1" "$2" "$3" "$HUB_API_URL"
)
lsdir()(
if [ -d "." ]; then
find "." -mindepth 1 -maxdepth 1 -type d -not -path '*/\.*' -printf '%f\n' | sort
fi
)
parents()(
for namespace in $IMAGE_NAMESPACE; do
_print "$1" | grep "FROM $namespace/" | cut -d/ -f2 | cut -d' ' -f1 | uniq
done
)
deps()(
dir="${1:-$(pwd)}"
basename="$(basename "$dir")"
parents=""
if [ -d "$dir" ]; then
for file in $(find "$dir" -name "Dockerfile"); do
parents="$(parents "$(cat "$file")") $parents"
done
fi
parents="$(echo "$parents" | sed -e 's/ /\n/g' | grep -v "$basename" | cut -d: -f1 | sort | uniq | xargs)"
for parent in $parents; do
parents="$(deps "$parent") $parents"
done
echo "$parents" | xargs
)
split()(
name="$1"
repo="$GIT_NAMESPACE/$1"
url="https://github.com/$repo"
_ansi --yellow --nl "Split $name -> $url"
response="$(github "GET" "/repos/$repo")"
if [ "$(http_code "$response")" = "404" ]; then
printf 'Create git repository "%s"\n' "$repo"
response="$(github "POST" "/orgs/$GIT_NAMESPACE/repos" "$(to_json "name=$name")")"
assert_http_code "$response" "201"
fi
#run_git splitsh --prefix "$name" --remote "$url" --force
run_git subsplit publish "$name:$url"
response="$(github "PUT" "/repos/$repo/actions/workflows/main.yml/enable")"
assert_http_code "$response" "204"
)
versions()(
if [ -f "$1/build.sh" ]; then
"$1/build.sh" --versions
elif [ -d "$1" ]; then
find "$1"/* -maxdepth 0 \( -type d -o -type l \) | sort | cut -d/ -f2
fi
)
build()(
package="" version="" source_dir="" build_parents="false" output="" opts=""
while [ -n "$1" ]; do
case "$1" in
--force) opts="$opts --no-cache";;
--no-clear) no_clear="1";;
--push) output="--push";;
--build-parents=*) build_parents="${1#*=}";;
--dir*) source_dir="$2"; shift;;
-*) opts="$opts $1";;
*) if [ -z "$package" ]; then package="$1"; else version="$1"; fi;;
esac
shift
done
if [ -f "$source_dir/build.sh" ]; then
source_dir="$source_dir"
elif [ -f "$package/build.sh" ]; then
source_dir="$package"
elif [ -z "$source_dir" ]; then
source_dir="${package}/${version}"
else
source_dir="${source_dir}/${version}"
fi
if [ ! -f "$source_dir/Dockerfile" ]; then
_ansi --bg-yellow --nl --black -f 'Skip "%s". Unable to find file "%s/Dockerfile"' "$source_dir" >&2
return
fi
if [ "$build_parents" = "true" ]; then
parents="$(parents "$(cat "$source_dir/Dockerfile")")"
for parent in $parents; do
tag="$(echo "$parent" | cut -d: -f2 -s)"
task_build "$(echo "$parent" | cut -d: -f1)" "${tag:-latest}" --build-parents="true" $opts $output
done
fi
target_dir="/tmp/${package}-${version}"
if [ -f "$source_dir/build.sh" ]; then
for namespace in $IMAGE_NAMESPACE; do
_ansi --nl --green --format "Build %s/%s:%s - %s" "$namespace" "$package" "$version" "$source_dir"
echo namespace="$namespace" docker_opts="$(echo "$opts $output" | xargs)" "$source_dir/build.sh" "$version"
if ! namespace="$namespace" docker_opts="$(echo "$opts $output" | xargs)" GITHUB_TOKEN="$GITHUB_TOKEN" "$source_dir/build.sh" "$version";
then exit 1; fi
done
printf "\n"
elif [ ! -d "$target_dir" ] || [ "$no_clear" != 1 ]; then
rm -rf "$target_dir"
cp -rfL "$source_dir" "$target_dir"
for namespace in $IMAGE_NAMESPACE; do
_ansi --nl --green --format "Build %s/%s:%s - %s" "$namespace" "$package" "$version" "$source_dir"
if ! docker buildx build $opts -t "$namespace/$package:$version" "$target_dir"
then exit 1; fi
done
if [ "$no_clear" != 1 ]; then rm -rf "$target_dir"; fi
printf "\n"
else
_ansi --gren --nl --format "Image %s/%s:%s exists" "$IMAGE_NAMESPACE" "$package" "$version"
fi
)
# Format Markdown
#
# Argument
# $1: Markdown file
task_format__markdown()(
_docker_run minidocks/pandoc --columns 80 --markdown-headings=setext -t commonmark-smart+pipe_tables -s -o "$1" "$1" && sed -i 's/``` /```/g' "$1" && _pass || _fail
)
# Build docker image
#
# Argument
# $1: Package name
# [$2: Version]
#
# Options:
# --dir
# --force
task_build()(
package="" version="" dir="" clone="" build_parents="false" opts="--progress=plain"
while [ -n "$1" ]; do
case "$1" in
--dir*) dir="$2"; shift;;
--force) opts="${opts} --no-cache";;
--clone) clone=1;;
--build-parents=*) build_parents="${1#*=}";;
-*) opts="${opts} $1";;
*) if [ -z "$package" ]; then package="$1"; else version="$1"; fi;;
esac
shift
done
if [ "${package#*/}" != "$package" ]; then
version="$(printf '%s' "$package" | cut -d/ -f2)"
package="$(printf '%s' "$package" | cut -d/ -f1)"
fi
if [ "$clone" = "1" ] && [ ! -d "${dir:-$package}" ]; then
run_git clone "https://github.com/$GIT_NAMESPACE/$package"
fi
versions="$(versions "${dir:-$package}")"
if [ -n "$version" ] && echo "$versions" | grep -qx "$version"; then
if ! build "$package" "$version" --dir "$dir" --build-parents="$build_parents" $opts
then exit 1; fi
else
for version in $versions; do
if ! build "$package" "$version" --dir "$dir" --build-parents="$build_parents" $opts
then exit 1; fi
done
fi
)
# Split monorepository
task_split()(
rm -rf .repo
run_git clone "https://github.com/$GIT_NAMESPACE/images" .repo
cd .repo
run_git subsplit init "https://github.com/$GIT_NAMESPACE/images"
processed=" "
for name in ${1:-$(lsdir)}; do
for dep in $(deps "$name"); do
if [ -z "${processed##* $dep *}" ]; then
continue
fi
split "$dep"
processed=" $dep $processed"
done
if [ -z "${processed##* $name *}" ]; then
continue
fi
split "$name"
processed=" $name $processed"
done
cd ..
rm -rf .repo
)
# Sync docker hub https://hub.docker.com with repository
task_sync()(
for name in ${1:-$(lsdir)}; do
for namespace in $HUB_NAMESPACE; do
hub_name="$namespace/$name"
_ansi --yellow --nl "Synchronize docker hub $hub_name"
base_data="$(to_json "registry=registry-1.docker.io&build_in_farm=true&repo_links=false&owner=$GIT_NAMESPACE&provider=github&repository=$name")"
description="Minimal $name images"
data="$(to_json "description=$description&namespace=$namespace&name=$name&image=$hub_name&privacy=public&is_private=false")"
data="${data%?}, ${base_data#?}"
response="$(hub "GET" "/repositories/$hub_name/")"
if [ "$(http_code "$response")" = "404" ]; then
printf "Create repository \"%s\"" "$hub_name"
response="$(hub "POST" "/repositories/" "$data")"
assert_http_code "$response" "201" && _pass || _fail
else
printf "Update repository \"%s\"" "$hub_name"
response="$(hub "PATCH" "/repositories/$hub_name/" "$data")"
assert_http_code "$response" "200" && _pass || _fail
fi
readme=""
if [ -r "$name/README.md" ]; then
readme="$(cat "$name/README.md")"
elif [ -r "README.md" ]; then
readme="$(cat "README.md")"
fi
if [ -n "$readme" ]; then
printf "Update README"
response="$(hub "PATCH" "/repositories/$hub_name/" "{\"registry\":\"registry-1.docker.io\",\"full_description\":\"$(json_escape "$readme")\"}")"
assert_http_code "$response" "200" && _pass || _fail
fi
done
done
)
# Push docker image to https://hub.docker.com
task_push()(
for name in ${1:-$(lsdir)}; do
for namespace in $HUB_NAMESPACE; do
hub_name="$namespace/$name"
for version in $(docker image ls --format "{{.Tag}}" "$hub_name" | grep -v 'none'); do
_println " - $hub_name:$version"
docker push "$hub_name:$version"
done
done
done
)
# Build image via github actions
task_github__build()(
REPOSITORY_NAME="${GITHUB_REPOSITORY#*/}"
task_build "$REPOSITORY_NAME" --dir "." $@
)
# Push docker image via github actions
task_github__push()(
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_LOGIN" --password-stdin
REPOSITORY_NAME="${GITHUB_REPOSITORY#*/}"
task_sync "$REPOSITORY_NAME" $@
task_push "$REPOSITORY_NAME" $@
)
# Build and push docker multi-platform image via github actions
task_github__build_and_push()(
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_LOGIN" --password-stdin
REPOSITORY_NAME="${GITHUB_REPOSITORY#*/}"
task_sync "$REPOSITORY_NAME"
mkdir -p /tmp/docker-cache
task_build "$REPOSITORY_NAME" --dir "." --push --platform="${DOCKER_PLATFORM:-linux/amd64,linux/arm64}" --no-cache --build-parents="${BUILD_PARENTS:-false}"
)