-
Notifications
You must be signed in to change notification settings - Fork 2
/
BUILD
executable file
·178 lines (162 loc) · 3.14 KB
/
BUILD
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
#!/usr/bin/env bash
set -eEuo pipefail
shopt -s inherit_errexit
script=$(basename "$0")
scrdir=$(readlink -f "$(dirname "$0")")
readonly LOG_FILE="/tmp/$script.log"
_p() {
l=$1
shift
echo "$l: $script $*" | tee -a "$LOG_FILE" >&2
}
pt() { _p "[TRACE] " "$*"; }
pd() { _p "[DEBUG] " "$*"; }
pi() { _p "[INFO] " "$*"; }
pw() { _p "[WARNING]" "$*"; }
pe() { _p "[ERROR] " "$*"; }
pf() {
_p "[FATAL] " "$*"
exit 1
}
usage() {
cat <<EOT
$script --help
$script --trace --verbose
$script --user=pjcj --image=cpancover --no-cache
$script --env=[prod|dev]
EOT
exit 0
}
cleanup() {
declare -r res=$?
((verbose)) && pi "Cleaning up"
exit $res
}
PATH="$scrdir:$PATH"
verbose=0
user=pjcj
perl="5.38.2"
image=cpancover
nocache=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
usage
;;
-t | --trace)
set -x
shift
;;
-v | --verbose)
verbose=1
shift
;;
-u | --user)
user="$2"
shift 2
;;
-i | --image)
image="$2"
shift 2
;;
-p | --perl)
perl="$2"
shift 2
;;
-n | --no-cache)
nocache="--no-cache"
shift
;;
-e | --env)
shift
case "$1" in
prod)
user=pjcj
image=cpancover
shift
;;
dev)
user=pjcj
image=cpancover_dev
shift
;;
*)
pf "Unrecognised environment: $1"
;;
esac
;;
*)
break
;;
esac
done
build_perl() {
local p="perl-$perl"
pi "Building docker for $p"
local f="$p.tmp"
cat <<EOF >"$f"
FROM ubuntu:24.04
ENV TERM=xterm
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# hadolint ignore=DL3008
RUN apt-get update && apt-get -y --no-install-recommends \
install wget build-essential \
&& rm -rf /var/lib/apt/lists/*
RUN wget --progress=dot:giga http://www.cpan.org/src/5.0/$p.tar.gz \
-O - | tar xzvf - -C /usr/local/src
WORKDIR /usr/local/src/$p
RUN ./Configure -des && make install
WORKDIR /
RUN rm -rf /usr/local/src/$p
EOF
mkdir -p "$p"
local df="$p/Dockerfile"
if diff -q "$f" "$df"; then
rm "$f"
else
pi "Writing dockerfile $df"
mv "$f" "$df"
fi
local cdf=devel-cover-base/Dockerfile
local l1="FROM pjcj/$p:latest"
if [[ $(head -2 $cdf | tail -1) != "$l1" ]]; then
pi "Updating FROM line in $cdf to $p"
perl -pi -e "\$_ = qq($l1\\n) if \$. == 2" $cdf
fi
}
build() {
pi "Building $user/$image"
docker build $nocache -t "$user/perl-$perl" "perl-$perl" &&
docker build $nocache -t "$user/devel-cover-base" devel-cover-base &&
docker build --no-cache -t "$user/devel-cover-git" devel-cover-git &&
docker build -t "$user/$image" cpancover &&
docker push "$user/$image" &&
pi "done"
}
main() {
((verbose)) && pi "Running $*"
case "${1-build}" in
build)
build_perl
build
;;
devel-cover-base-shell)
docker run -it \
--rm=false \
"$user/devel-cover-base" /bin/bash
;;
options)
perl -nE 'say $1 =~ s/"//gr =~ s/\s*\|\s*/\n/gr' \
-E 'if /^ {2}"?([a-zA-Z0-9_ "|\\-]+)"?(?:\)|\s*\|\s*\\)$/' \
-E '&& $1 !~ /^_/' <"$0"
;;
*)
pf "Unknown option: $1"
;;
esac
}
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
trap cleanup EXIT INT
main "$@"
fi
# vim: ft=bash