-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·165 lines (146 loc) · 4.05 KB
/
build.sh
File metadata and controls
executable file
·165 lines (146 loc) · 4.05 KB
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
#!/bin/bash
set -eo pipefail
# Configuration parameters
DOCKERHUB_USER="" # Default DockerHub username (empty)
REPO_SUFFIX="dev" # Image suffix
LOG_DIR="build-logs" # Log directory
USE_MIRROR="0" # Use mirror (0: no, 1: yes)
# Help message
show_help() {
echo "Usage: $0 [OPTIONS] [PATH...]"
echo "Build Docker images from Dockerfiles"
echo
echo "Options:"
echo " -h, --help Show this help message"
echo " -u, --user Set DockerHub username"
echo " -s, --suffix Set image suffix (default: $REPO_SUFFIX)"
echo " -l, --logs Set log directory (default: $LOG_DIR)"
echo " -m, --mirror Set whether to use mirror (default: $USE_MIRROR)"
echo
echo "Arguments:"
echo " PATH Dockerfile path or directory (default: auto-discover)"
}
# Parse parameters
PATHS=()
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-u|--user)
DOCKERHUB_USER="$2"
shift; shift
;;
-s|--suffix)
REPO_SUFFIX="$2"
shift; shift
;;
-l|--logs)
LOG_DIR="$2"
shift; shift
;;
-m|--mirror)
USE_MIRROR="$2"
shift; shift
;;
-*)
echo "Error: Unknown option $1" >&2
exit 1
;;
*)
PATHS+=("$1")
shift
;;
esac
done
# Create log directory
mkdir -p "$LOG_DIR"
# Get absolute path
get_abs_path() {
local path="$1"
if [[ -d "$path" ]]; then
(cd "$path" && pwd)
else
echo "$(cd "$(dirname "$path")" && pwd)/$(basename "$path")"
fi
}
# Collect all Dockerfile paths
declare -A dockerfiles
# Process explicitly specified paths
if [[ ${#PATHS[@]} -gt 0 ]]; then
for path in "${PATHS[@]}"; do
abs_path=$(get_abs_path "$path")
if [[ -f "$abs_path" && $(basename "$abs_path") =~ Dockerfile$ ]]; then
dockerfiles["$abs_path"]=1
elif [[ -d "$abs_path" ]]; then
while IFS= read -r -d '' file; do
dockerfiles["$file"]=1
done < <(find "$abs_path" -type f \( -name "Dockerfile" -o -name "*.Dockerfile" \) -print0)
else
echo "Error: Invalid path $path" >&2
exit 1
fi
done
# Auto-discovery mode
else
echo "Discovering Dockerfiles in non-hidden directories..."
while IFS= read -r -d '' file; do
dockerfiles["$file"]=1
done < <(find . -type f \( -name "Dockerfile" -o -name "*.Dockerfile" \) -not -path '*/.*' -print0)
fi
# Check if any Dockerfiles were found
if [[ ${#dockerfiles[@]} -eq 0 ]]; then
echo "Error: No Dockerfiles found" >&2
exit 1
fi
# Build function
build_image() {
local dockerfile="$1"
local context_dir=$(dirname "$dockerfile")
local folder_name=$(basename "$context_dir")
# Generate tag
local raw_tag=$(basename "$dockerfile")
local tag=$(echo "$raw_tag" | sed -E \
-e 's/\.[^.]*$//' \
-e 's/Dockerfile/latest/' \
-e 's/[^a-zA-Z0-9_.-]/-/g')
# Sanitize image name
local sanitized_name=$(echo "$folder_name" | \
tr '[:upper:]' '[:lower:]' | \
tr -cd '[:alnum:]-_' | \
sed 's/^[^a-zA-Z0-9]*//;s/[^a-zA-Z0-9]*$//')
# Compose image name
local image_prefix=""
[[ -n "$DOCKERHUB_USER" ]] && image_prefix="${DOCKERHUB_USER}/"
local image_name="${image_prefix}${sanitized_name}-${REPO_SUFFIX}:${tag}"
# Generate log filename
local log_file="${LOG_DIR}/build-${sanitized_name}-${tag}.log"
echo "--------------------------------------------------"
echo "Building: $dockerfile"
echo "Context: $context_dir"
echo "Image: $image_name"
echo "Log file: $log_file"
# Execute build and log output
if [[ $USE_MIRROR = "1" ]]; then
# Build with mirror
echo "Using mirror..."
docker build \
--build-arg USEMIRROR=1 \
--file "$dockerfile" \
--tag "$image_name" \
"$context_dir" 2>&1 | tee "$log_file"
else
# Build without mirror
echo "Not using mirror..."
docker build \
--file "$dockerfile" \
--tag "$image_name" \
"$context_dir" 2>&1 | tee "$log_file"
fi
}
# Main build loop
for dockerfile in "${!dockerfiles[@]}"; do
build_image "$dockerfile" || exit $?
done
echo "All builds completed successfully!"