-
Notifications
You must be signed in to change notification settings - Fork 5
/
builder.sh
159 lines (115 loc) · 4.03 KB
/
builder.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
#!/bin/bash -e
### @accetto, August 2021
### updated: August 2023
### depends on the hook scripts
### set the environment variables first, e.g. 'source .secrets'
### usage: './<script-name> <blend> <cmd> [build-options]'
die() {
local message="${1:-(unknown)}"
local -i code=${2:-1}
local place="${3:-$0}"
echo -e "EXITING at line "${BASH_LINENO[0]}" in '${place}' with code ${code}: ${message}" >&2
exit ${code}
}
clear_log() {
### just for debugging
# cp -f "${_ci_builder_log}" "${_ci_builder_log}_copy"
> "${_builder_log}"
echo -e "\n==> EXECUTING @$(date -u +'%Y-%m-%d_%H-%M-%S'): ${0} $@\n"
}
show_error() {
echo -e "\nERROR: ${@:-(unknown)}\n" >&2
}
show_unlogged_help() {
# help is never logged
exec 1>&-
{
cat <<EOT
This script can:
- build and publish the individual images
- execute the individual hook scripts of the building pipeline '/docker/hooks/'
- refresh the local builder 'g3-cache'
Usage: $0 <blend> <command> [<docker-cli-options>]
blend := (latest|bookworm|bullseye)[-chromium|-firefox])
command := (all|all-no-push)|(pre_build|build|push|post_push|cache)
The <docker-cli-options> (e.g. '--no-cache') are passed to the Docker CLI commands used internally.
The script creates a complete execution log.
EOT
} >&3
}
main() {
if [[ $# -eq 0 ]] ; then
show_unlogged_help
return 0
fi
if [[ "${1}" == "-h" || "${1}" =~ help ]] ; then
show_unlogged_help
return 0
fi
local blend=${1}
local cmd=${2}
if [[ $# -ge 2 ]] ; then shift 2 ; fi
local -a all_pipeline
local -i exit_code=0
case "${cmd}" in
pre_build | build | push | post_push | cache )
clear_log
"${_build_context}"/hooks/"${cmd}" dev "${blend}" $@
exit_code=$?
if [[ ${exit_code} -ne 0 ]] ; then die "Hook script '${cmd}' failed with code ${exit_code}." ${exit_code} ; fi
;;
all | all-no-push )
clear_log
"${_build_context}"/hooks/pre_build dev "${blend}" $@
exit_code=$?
if [[ ${exit_code} -ne 0 ]] ; then die "Hook script 'pre_build' failed with code ${exit_code}." ${exit_code} ; fi
if [[ ! -f "${_build_context}"/scrap-demand-stop-building ]] ; then
case "${cmd}" in
all-no-push ) all_pipeline=("build") ;;
all ) all_pipeline=("build" "push" "post_push") ;;
* )
show_error "Unknown command: '${cmd}'"
;;
esac
for c in ${all_pipeline[@]} ; do
echo
echo "==> ${c} '${blend}'"
echo
"${_build_context}"/hooks/"${c}" dev "${blend}" $@
exit_code=$?
if [[ ${exit_code} -ne 0 ]] ; then die "Hook script '${c}' failed with code ${exit_code}." ${exit_code} ; fi
done
echo
case "${cmd}" in
all-no-push )
echo "==> Built '${blend}'"
;;
all )
echo "==> Published '${blend}'"
;;
* )
show_error "Unknown command: '${cmd}'"
;;
esac
echo
else
echo
echo "==> No build needed for '${blend}'"
echo
fi
;;
*)
show_error "Invalid arguments '${blend}' '${cmd}'"
;;
esac
echo -e "\n==> FINISHED @$(date -u +'%Y-%m-%d_%H-%M-%S'): ${0} $@\n"
}
declare _build_context="./docker"
declare _builder_log="scrap_builder.log"
### duplicate 'stdout' so we can close it when displaying help
exec 3>&1
### main entry point
declare -i __exit_code=0
main $@ 2>&1 | tee -a "${_builder_log}"
__exit_code=${PIPESTATUS[0]}
exit ${__exit_code}