-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-build.sh
executable file
·143 lines (114 loc) · 3.46 KB
/
docker-build.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
#!/bin/bash
set -eu
if [ -n "$(git status --porcelain=v1)" ]; then
echo "Please commit or stash changes first!"
exit 2
fi
do_cmd() {
if [ ! "$dryrun" = "true" ]; then
echo "$1"
shift
"$@"
else
shift
if [ "$#" -eq 1 -a "$(type -t "$1")" = function ]; then
echo ":: $(type "$1" | sed -E '1,3d;$d;s/ {4}//')"
else
echo ":: $*"
fi
fi
}
docker_tag() {
do_cmd "Tagging $1 as $2..." docker tag "$1" "$2"
images+=("$2")
}
rm_old() {
docker image ls "$imgname" --format '{{.Tag}}' | awk -v repo="$imgname" '{print repo":"$0}' | xargs -n20 -r -P5 docker rmi
}
[ -n "${DEBUG:-}" ] && set -x
# Change this line when adopting the script to another project
readonly default_owner=romw314 name=bashgenn fullname=Bashgenn
declare -A base
base[debian]=debian:bullseye
base[alpine]=bash:5.2-alpine3.19
readonly base
declare -A variantver
variantver[alpine]=alpine3.19
variantver[debian]=bullseye
readonly variantver
readonly imgname="${owner:-"$default_owner"}/$name"
declare dryrun all push
if grep -q dryrun <<< "$*"; then
dryrun=true
else
dryrun=false
fi
if grep -q push <<< "$*"; then
push=true
else
push=false
fi
if grep -q all <<< "$*"; then
do_cmd "Deleting old images..." rm_old
all=true
else
all=false
fi
if ! grep -q build <<< "$*"; then
(
echo "docker-build.sh - Script for building $fullname's docker image"
echo
echo "Usage: $0 build [dryrun] [all] [push]"
echo
echo "The \`dryrun' flag makes the script print Docker commands but does not actually run them."
echo "The \`all' flag makes the script delete all existing images for the repo and rebuild them."
echo "The \`push' flag makes the script push the images to Docker Hub."
echo
echo "Run with DEBUG=1 to print all commands executed."
) >&2
exit 1
fi
readonly dryrun all
readonly latestver="v$(git tag --list | grep -E 'v[0-9]+' | cut -dv -f2 | sort -nr | head -n1)"
declare -a images
# Keep the same script when doing git checkout
if [ -z "${_BASHGENN_DOCKER_BUILD:-}" ]; then
tempscript="$(mktemp)"
cp -vf "$0" "$tempscript"
chmod +x "$tempscript"
_BASHGENN_DOCKER_BUILD=1 "$tempscript" "$@"
fi
for variant in debian alpine; do
for version in $(git tag --list | grep -E 'v[0-9]+'); do
do_cmd "Resetting git changes..." git reset --hard
do_cmd "Checking out $version..." git checkout "$version"
do_cmd "Adding master's Dockerfile..." git checkout master Dockerfile docker-build.sh
tagname="$version-${variantver["$variant"]}"
images+=("$imgname:$tagname")
if [ ! "$all" = "true" ] && docker image ls "$imgname" --format '{{.Tag}}' | awk -v tag="$tagname" '$0==tag{print "skip"}' | grep -q skip; then
echo "Skipped $imgname:$tagname."
continue
fi
do_cmd "Building $imgname:$tagname..." docker build -t "$imgname:$tagname" \
--build-arg VARIANT="$variant" \
--build-arg BASHGENN_VERSION="$version" \
--build-arg BASE="${base["$variant"]}" \
.
case "$variant" in
debian) docker_tag "$imgname:$tagname" "$imgname:$version";;
alpine) docker_tag "$imgname:$tagname" "$imgname:$version-alpine";;
esac
done
done
# Debian
docker_tag "$imgname:$latestver" "$imgname:latest"
# Alpine Linux
docker_tag "$imgname:$latestver-${variantver[alpine]}" "$imgname:${variantver[alpine]}"
docker_tag "$imgname:${variantver[alpine]}" "$imgname:alpine"
if [ "$push" = "true" ]; then
for img in "${images[@]}"; do
do_cmd "Pushing $img to Docker Hub..." docker push "$img"
done
fi
do_cmd "Resetting git changes..." git reset --hard
do_cmd "Checking out master..." git checkout master