-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredbuild.sh
executable file
·81 lines (70 loc) · 2.14 KB
/
redbuild.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
#!/usr/bin/env bash
set -e # exit on error
if [ -n "$DEBUG" ]; then
# echo all commands in debug mode
set -x
fi
print_logo() {
printf "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
}
# print_logo
print_info() {
echo "REDBUILD v2.2.1"
echo " container engine: $CONTAINER_ENGINE"
printf " host: $(uname -s)/$(uname -m) $(uname -r)\n"
printf "\n"
}
# get args passed to script
ARGS="$@"
CWD=$(pwd)
# custom args to container engine
CBUILD_ARGS=$CBUILD_ARGS
CRUN_ARGS=$CRUN_ARGS
testcmd () {
command -v "$1" >/dev/null
}
# detect container engine, prefer podman but fall back to docker
CONTAINER_ENGINE="unknown-container-engine"
detect_container_engine() {
if testcmd podman; then
CONTAINER_ENGINE="podman"
elif testcmd docker; then
CONTAINER_ENGINE="docker"
# i don't like docker
printf "WARNING: docker is not recommended, use podman instead\n"
else
echo "ERROR: no suitable container engine found. please install podman or docker."
exit 1
fi
}
detect_container_engine
print_info
CWD_DIRNAME=$(basename "$CWD")
# lowercase and alphanumeric only
CWD_DIRNAME=$(echo "$CWD_DIRNAME" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]')
CWD_HASH=$(echo "$CWD" | sha256sum | head -c 8)
# BUILDER_TAG=builder_$(head /dev/urandom | tr -dc a-z0-9 | head -c10)
BUILDER_TAG=builder_${CWD_DIRNAME}_${CWD_HASH}
# build the builder image
build_builder_image() {
printf "building builder image [tag: $BUILDER_TAG]"
if [ -n "$CBUILD_ARGS" ]; then
printf " [$CONTAINER_ENGINE args: $CBUILD_ARGS]"
fi
printf "...\n"
$CONTAINER_ENGINE build -t $BUILDER_TAG $CBUILD_ARGS -f build.docker | sed 's/^/ /'
}
# run the build inside the builder image
run_build() {
printf "running build in builder image [tag: $BUILDER_TAG]"
if [ -n "$CRUN_ARGS" ]; then
printf " [$CONTAINER_ENGINE args: $CRUN_ARGS]"
fi
if [ -n "$ARGS" ]; then
printf " [script args: $ARGS]"
fi
printf "...\n"
$CONTAINER_ENGINE run --rm -it -v $(pwd):/prj $CRUN_ARGS $BUILDER_TAG /bin/bash -l -c "cd /prj && ./build.sh $ARGS" | sed 's/^/ /'
}
build_builder_image
run_build