-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuild.sh
executable file
·336 lines (277 loc) · 8.47 KB
/
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
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
#!/bin/bash
# Script argument handling reference: https://joshtronic.com/2023/03/12/parsing-arguments-in-shell-script/
# Default image name (can be overridden by CLI argument)
IMAGE="throwtheswitch/madsciencelab"
##
## Usage output
##
# Function to print usage instructions
function usage {
echo ""
echo "Usage: $0 [--dir <dir>]* [--variant <name>] [-b|--build] [-v|--version <ver>] [-i|--image <name>] [--platform] [--push|--validate] [-d|--debug] [--verbose] "
echo ""
echo "throwtheswitch/madsciencelab Dockerfile + asset generation with multi-platform Docker image build"
echo ""
echo "File generation options:"
echo " --dir Add <dir> to be processed in addition to build/base"
echo " May be specified multiple times"
echo " Last instance provides variant name"
echo " --variant Override auto-discovered variant name"
echo ""
echo "Docker image build options:"
echo " -b, --build Build Docker image after generating a Dockerfile [defaults to local image on host]"
echo " -v, --version Docker image version, ex: '1.2.3' [default: 'latest']"
echo " -i, --image Override image name [default: '$IMAGE']"
echo " --platform Docker target platforms, ex: 'linux/arm64,linux/amd64'"
echo " --push Push a multi-platform Docker image build to its repository (only with --platform)"
echo " --validate Execute a multi-platform build (only with --platform)"
echo " -d, --debug Produce full Docker image build log"
echo ""
echo "Script options:"
echo " --verbose Print script steps"
echo " -h, --help Print this usage message"
exit 1
}
# If no command line parameters, print usage and exit
if [ $# -eq 0 ]; then usage; fi
##
## Defaults
##
# Docker image tag / version handling
IMAGE_TAG="latest" # Docker image tag (can include 'latest')
CONTAINER_VERSION="" # Version provided to running container via environment variable (blank or a version string)
# Variant handling
VARIANT_NAME_OVERRIDE=false
VARIANT_NAME_RENAME=""
VARIANT_NAME=""
VARIANT_DIR_PATH=""
# First, necessary base directory in list of directories to process
DOCKERFILE_GEN_DIRS="--dir=build/base"
# Docker buildx platforms list (empty defaults to host platform where build is happening)
PLATFORMS=""
# Docker buildx command line argument
PLATFORM_ARGS=""
# Default to standard, simple `docker build`
BUILD_ACTION="build"
# Special build action flags
MULTIPLATFORM_BUILD=false
PUSH=false
VALIDATE=false
##
## Command line argument handling
##
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--dir)
if [ -z "$2" ]; then
echo ""
echo "ERROR: Directory name required for --dir"
usage
fi
if [[ ! -d "$2" ]]; then
echo ""
echo "ERROR: Invalid directory $2. Path does not exist."
exit 1
fi
# Variant name defaults to last provided --dir option
VARIANT_NAME=`basename "$2"`
# Variant directory is last provided in --dir list
VARIANT_DIR_PATH="$2"
# Concatenate to list of --dir arguments for `filegen` utility
DOCKERFILE_GEN_DIRS="$DOCKERFILE_GEN_DIRS --dir=$2"
shift
shift
;;
--variant)
# Capture varient name override, including blank option
# Since we can't control order of CLI args, we save and process later
VARIANT_NAME_RENAME="$2"
VARIANT_NAME_OVERRIDE=true
shift
shift
;;
-b|--build)
BUILD=true
shift
;;
-v|--version)
if [ -z "$2" ]; then
echo ""
echo "ERROR: No argument provided for --version"
usage
fi
CONTAINER_VERSION="$2"
shift
shift
;;
-i|--image)
if [ -z "$2" ]; then
echo ""
echo "ERROR: No name provided for --image"
usage
fi
IMAGE="$2"
shift
shift
;;
--platform)
if [ -z "$2" ]; then
echo ""
echo "ERROR: One or more platforms required by --platform"
usage
fi
PLATFORMS="$2"
PLATFORM_ARGS="--platform=$2"
shift
shift
;;
--push)
# Override default of image build with push to repository
PUSH=true
shift
;;
--validate)
# Override default of image build with push to repository
VALIDATE=true
shift
;;
-d|--debug)
# Force the Docker build to skip caching and produce a full progress dump for review
LOG_ARGS="--no-cache --progress=plain"
shift
;;
--verbose)
VERBOSE=true
shift
;;
-h|--help)
usage
;;
*)
echo "Invalid option: $1"
usage
;;
esac
done
# If platforms blank, ensure --push or --validate not set
if [ -z "$PLATFORMS" ]; then
if [ "$PUSH" = true ]; then
echo ""
echo "ERROR: --push only available in combination with --platform"
usage
fi
if [ "$VALIDATE" = true ]; then
echo ""
echo "ERROR: --validate only available in combination with --platform"
usage
fi
# If platforms not blank, ensure --push or --validate used properly and add to BUILD_ACTION accordingly
else
if [ "$PUSH" = true && "$VALIDATE" = true ]; then
echo ""
echo "ERROR: --validate and --push are mutually exclusive options"
usage
fi
fi
# After build option combination validation above, set our build options
if [ "$PUSH" = true ]; then
BUILD_ACTION="buildx build --push"
MULTIPLATFORM_BUILD=true
elif [ "$VALIDATE" = true ]; then
BUILD_ACTION="buildx build -o type=image"
MULTIPLATFORM_BUILD=true
fi
# Print script statements to STDOUT if --verbose set
if [ "$VERBOSE" = true ]; then
set -x
fi
# Reset variant name auto discovered from --dir list
if $VARIANT_NAME_OVERRIDE; then
VARIANT_NAME="$VARIANT_NAME_RENAME"
fi
# Add variant suffix to image name
if [ -n "$VARIANT_NAME" ]; then
IMAGE="$IMAGE-$VARIANT_NAME"
fi
# Create Docker image tag other than 'latest', if version string provided
if [ -n "$CONTAINER_VERSION" ]; then
IMAGE_TAG="$CONTAINER_VERSION"
fi
##
## File Generation
##
echo "🎯 Target Docker image $IMAGE:$IMAGE_TAG"
echo ""
# Generate the Welcome file displayed within the shell at container launch
bin/filegen welcome $DOCKERFILE_GEN_DIRS build/base/templates/welcome.erb "$VARIANT_DIR_PATH"/assets/shell/welcome
if [ $? -ne 0 ]; then
echo "❌ ERROR: Could not generate welcome file(s)"
echo ""
exit 1
fi
echo ""
# Generate the Dockerfile
bin/filegen dockerfile $DOCKERFILE_GEN_DIRS --variant="$VARIANT_NAME" build/base/templates/Dockerfile.erb "$VARIANT_DIR_PATH"/docker/Dockerfile
if [ $? -ne 0 ]; then
echo "❌ ERROR: Could not generate Dockerfile"
echo ""
exit 1
fi
echo ""
##
## Docker Image Build
##
# Build the Dockerfile we just created with any additional options
if [ "$BUILD" = true ]; then
# If a multi-platform build option is set, enable multi-platform builder
if [ $MULTIPLATFORM_BUILD == true ]; then
# If a buildx builder doesn't already exist, set one up
if ! (docker buildx ls 2>&1 | grep -q 'madsciencelab-builder'); then
docker buildx create --name madsciencelab-builder
if [ $? -ne 0 ]; then
echo "❌ ERROR: Could not create multi-platform Docker builder"
echo ""
exit 1
fi
fi
# Ensure we're using a buildx builder
docker buildx use madsciencelab-builder
if [ $? -ne 0 ]; then
echo "❌ ERROR: Could not enable multi-platform Docker builder"
echo ""
exit 1
fi
fi
# Perform multi-platform build with output as an image or optionally a direct push to the repository
# Always echo this command to the command line
(set -x; docker $BUILD_ACTION $LOG_ARGS -t "$IMAGE":"$IMAGE_TAG" $PLATFORM_ARGS --build-arg CONTAINER_VERSION="$CONTAINER_VERSION" -f "$VARIANT_DIR_PATH"/docker/Dockerfile .)
# Capture exit code from attempted Docker image build
success=$?
# Stop the buildx builder started above
if [ $MULTIPLATFORM_BUILD == true ]; then
docker buildx stop madsciencelab-builder
fi
operation=""
platforms=""
if [ $PUSH == true ]; then
operation="Built and pushed"
else
operation="Built"
fi
if [ -z "$PLATFORMS" ]; then
platforms="host platform"
else
# Break up Docker command line option 'platform,platform' to be readable
platforms="${PLATFORMS/,/, }"
fi
if [ $success -eq 0 ]; then
echo ""
echo "📦 $operation $IMAGE:$IMAGE_TAG for $platforms"
echo ""
else
echo ""
echo "❌ ERROR: Could not build $IMAGE:$IMAGE_TAG for $platforms"
echo ""
fi
fi