-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·228 lines (183 loc) · 5.91 KB
/
build.sh
File metadata and controls
executable file
·228 lines (183 loc) · 5.91 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
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
#!/bin/bash
# Build script for ui-apt-mirror
# This script builds Docker images for multiple architectures and saves them to dist/
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
IMAGE_NAME="ui-apt-mirror"
VERSION="latest"
DIST_DIR="dist"
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check prerequisites
check_prerequisites() {
print_status "Checking prerequisites..."
# Check if Docker is installed and running
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker is not running. Please start Docker first."
exit 1
fi
# Check if Docker Buildx is available
if ! docker buildx version &> /dev/null; then
print_warning "Docker Buildx not available. Installing..."
docker buildx install
fi
# Check if Dockerfile exists
if [ ! -f "Dockerfile" ]; then
print_error "Dockerfile not found in current directory."
exit 1
fi
# Create dist directory if it doesn't exist
mkdir -p "$DIST_DIR"
print_success "Prerequisites check completed."
}
# Function to create multi-platform builder
setup_builder() {
print_status "Setting up multi-platform builder..."
# Create a new builder instance if it doesn't exist
if ! docker buildx inspect multiarch &> /dev/null; then
docker buildx create --name multiarch --use
else
docker buildx use multiarch
fi
# Bootstrap the builder
docker buildx inspect --bootstrap
print_success "Multi-platform builder setup completed."
}
# Function to build images for all architectures
build_images() {
print_status "Building images for all architectures..."
# Define architectures
ARCHITECTURES=("linux/amd64" "linux/arm64")
# Build for each architecture
for arch in "${ARCHITECTURES[@]}"; do
arch_short=$(echo "$arch" | sed 's/linux\///')
print_status "Building for $arch..."
# Build the image
docker buildx build \
--platform "$arch" \
--tag "${IMAGE_NAME}:${VERSION}" \
--file "Dockerfile" \
--load \
.
# Save the image to tar file
output_file="${DIST_DIR}/${IMAGE_NAME}-${arch_short}.tar"
print_status "Saving image to $output_file..."
docker save "${IMAGE_NAME}:${VERSION}" -o "$output_file"
# Compress the tar file
print_status "Compressing $output_file..."
gzip -f "$output_file"
print_success "Built and saved ${IMAGE_NAME}-${arch_short}.tar.gz"
done
}
# Function to build and push to registry (optional)
build_and_push() {
if [ "$1" = "--push" ]; then
print_status "Building and pushing to registry..."
# Check if registry is specified
if [ -z "$REGISTRY" ]; then
print_error "REGISTRY environment variable not set. Skipping push."
return
fi
# Build and push for all architectures
docker buildx build \
--platform linux/amd64,linux/arm64 \
--tag "${REGISTRY}/${IMAGE_NAME}:${VERSION}" \
--file "Dockerfile" \
--push \
.
print_success "Images pushed to registry: ${REGISTRY}/${IMAGE_NAME}:${VERSION}"
fi
}
# Function to clean up
cleanup() {
print_status "Cleaning up..."
# Remove the built image
docker rmi "${IMAGE_NAME}:${VERSION}" 2>/dev/null || true
print_success "Cleanup completed."
}
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --push Build and push to registry (requires REGISTRY env var)"
echo " --clean Clean up built images after saving"
echo " --help Show this help message"
echo ""
echo "Environment variables:"
echo " REGISTRY Docker registry URL (e.g., docker.io/username)"
echo " VERSION Image version (default: latest)"
echo ""
echo "Examples:"
echo " $0 # Build and save images locally"
echo " $0 --push # Build and push to registry"
echo " REGISTRY=docker.io/username $0 --push # Build and push to specific registry"
}
# Main execution
main() {
local push_flag=false
local clean_flag=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--push)
push_flag=true
shift
;;
--clean)
clean_flag=true
shift
;;
--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
print_status "Starting build process for ui-apt-mirror..."
# Check prerequisites
check_prerequisites
# Setup builder
setup_builder
# Build images
build_images
# Push to registry if requested
if [ "$push_flag" = true ]; then
build_and_push --push
fi
# Cleanup if requested
if [ "$clean_flag" = true ]; then
cleanup
fi
print_success "Build process completed successfully!"
print_status "Images saved to $DIST_DIR/:"
ls -la "$DIST_DIR"/*.tar.gz 2>/dev/null || print_warning "No tar files found in $DIST_DIR"
}
# Run main function with all arguments
main "$@"