-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathcontainer_build.sh
executable file
·162 lines (141 loc) · 3.21 KB
/
container_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
#!/bin/bash
set -euo pipefail
available() {
command -v "$1" >/dev/null
}
select_container_manager() {
if available podman; then
conman_bin="podman"
return 0
elif available docker; then
conman_bin="docker"
return 0
fi
conman_bin="podman"
}
add_build_platform() {
conman_build+=("build")
# This build saves space
if ! $rm_after_build; then
conman_build+=("--no-cache")
fi
conman_build+=("--platform" "$platform")
conman_build+=("-t" "quay.io/ramalama/$image_name")
conman_build+=("-f" "$image_name/Containerfile" ".")
}
rm_container_image() {
if $rm_after_build; then
"$conman_bin" rmi -f "$image_name" || true
fi
}
build() {
cd "container-images"
local image_name="${1//container-images\//}"
local conman_build=("${conman[@]}")
local conman_show_size=("${conman[@]}" "images" "--filter" "reference='quay.io/ramalama/$image_name'")
if [ "$3" == "-d" ]; then
add_build_platform
echo "${conman_build[@]}"
cd - > /dev/null
return 0
fi
case "${2:-}" in
build)
add_build_platform
echo "${conman_build[@]}"
"${conman_build[@]}"
"${conman_show_size[@]}"
rm_container_image
;;
push)
"${conman[@]}" push "quay.io/ramalama/$image_name"
;;
*)
echo "Invalid command: ${2:-}. Use 'build' or 'push'."
return 1
;;
esac
cd - > /dev/null
}
determine_platform() {
local platform="linux/amd64"
if [ "$(uname -m)" = "aarch64" ] || { [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "arm64" ]; }; then
platform="linux/arm64"
fi
echo "$platform"
}
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
-d)
option="$1"
shift
;;
-r)
rm_after_build="true"
shift
;;
build|push)
command="$1"
shift
;;
*)
target="$1"
shift
;;
esac
done
}
process_all_targets() {
local command="$1"
local option="$2"
for i in container-images/*; do
if [ "$i" == "container-images/scripts" ]; then
continue
fi
build "$i" "$command" "$option"
done
}
print_usage() {
echo "Usage: $(basename "$0") [-h|--help] [-d] <command> [target]"
echo
echo "Commands:"
echo " build Build the container images"
echo " push Push the container images"
echo
echo "Options:"
echo " -d Some option description"
echo " -r Remove container image after build"
echo
echo "Targets:"
echo " Specify the target container image to build or push"
echo " If no target is specified, all container images will be processed"
}
main() {
local conman_bin
select_container_manager
local conman=("$conman_bin")
local platform
platform=$(determine_platform)
local target=""
local command=""
local option=""
local rm_after_build="false"
parse_arguments "$@"
if [ -z "$command" ]; then
echo "Error: command is required (build or push)"
print_usage
exit 1
fi
target="${target:-all}"
if [ "$target" = "all" ]; then
process_all_targets "$command" "$option"
else
build "container-images/$target" "$command" "$option"
fi
}
main "$@"