build.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/bin/bash
  2. # Build script for ui-apt-mirror
  3. # This script builds Docker images for multiple architectures and saves them to dist/
  4. set -e
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Configuration
  12. IMAGE_NAME="ui-apt-mirror"
  13. VERSION="latest"
  14. DIST_DIR="dist"
  15. # Function to print colored output
  16. print_status() {
  17. echo -e "${BLUE}[INFO]${NC} $1"
  18. }
  19. print_success() {
  20. echo -e "${GREEN}[SUCCESS]${NC} $1"
  21. }
  22. print_warning() {
  23. echo -e "${YELLOW}[WARNING]${NC} $1"
  24. }
  25. print_error() {
  26. echo -e "${RED}[ERROR]${NC} $1"
  27. }
  28. # Function to check prerequisites
  29. check_prerequisites() {
  30. print_status "Checking prerequisites..."
  31. # Check if Docker is installed and running
  32. if ! command -v docker &> /dev/null; then
  33. print_error "Docker is not installed. Please install Docker first."
  34. exit 1
  35. fi
  36. if ! docker info &> /dev/null; then
  37. print_error "Docker is not running. Please start Docker first."
  38. exit 1
  39. fi
  40. # Check if Docker Buildx is available
  41. if ! docker buildx version &> /dev/null; then
  42. print_warning "Docker Buildx not available. Installing..."
  43. docker buildx install
  44. fi
  45. # Check if Dockerfile exists
  46. if [ ! -f "Dockerfile" ]; then
  47. print_error "Dockerfile not found in current directory."
  48. exit 1
  49. fi
  50. # Create dist directory if it doesn't exist
  51. mkdir -p "$DIST_DIR"
  52. print_success "Prerequisites check completed."
  53. }
  54. # Function to create multi-platform builder
  55. setup_builder() {
  56. print_status "Setting up multi-platform builder..."
  57. # Create a new builder instance if it doesn't exist
  58. if ! docker buildx inspect multiarch &> /dev/null; then
  59. docker buildx create --name multiarch --use
  60. else
  61. docker buildx use multiarch
  62. fi
  63. # Bootstrap the builder
  64. docker buildx inspect --bootstrap
  65. print_success "Multi-platform builder setup completed."
  66. }
  67. # Function to build images for all architectures
  68. build_images() {
  69. print_status "Building images for all architectures..."
  70. # Define architectures
  71. ARCHITECTURES=("linux/amd64" "linux/arm64")
  72. # Build for each architecture
  73. for arch in "${ARCHITECTURES[@]}"; do
  74. arch_short=$(echo "$arch" | sed 's/linux\///')
  75. print_status "Building for $arch..."
  76. # Build the image
  77. docker buildx build \
  78. --platform "$arch" \
  79. --tag "${IMAGE_NAME}:${VERSION}" \
  80. --file "Dockerfile" \
  81. --load \
  82. .
  83. # Save the image to tar file
  84. output_file="${DIST_DIR}/${IMAGE_NAME}-${arch_short}.tar"
  85. print_status "Saving image to $output_file..."
  86. docker save "${IMAGE_NAME}:${VERSION}" -o "$output_file"
  87. # Compress the tar file
  88. print_status "Compressing $output_file..."
  89. gzip -f "$output_file"
  90. print_success "Built and saved ${IMAGE_NAME}-${arch_short}.tar.gz"
  91. done
  92. }
  93. # Function to build and push to registry (optional)
  94. build_and_push() {
  95. if [ "$1" = "--push" ]; then
  96. print_status "Building and pushing to registry..."
  97. # Check if registry is specified
  98. if [ -z "$REGISTRY" ]; then
  99. print_error "REGISTRY environment variable not set. Skipping push."
  100. return
  101. fi
  102. # Build and push for all architectures
  103. docker buildx build \
  104. --platform linux/amd64,linux/arm64 \
  105. --tag "${REGISTRY}/${IMAGE_NAME}:${VERSION}" \
  106. --file "Dockerfile" \
  107. --push \
  108. .
  109. print_success "Images pushed to registry: ${REGISTRY}/${IMAGE_NAME}:${VERSION}"
  110. fi
  111. }
  112. # Function to clean up
  113. cleanup() {
  114. print_status "Cleaning up..."
  115. # Remove the built image
  116. docker rmi "${IMAGE_NAME}:${VERSION}" 2>/dev/null || true
  117. print_success "Cleanup completed."
  118. }
  119. # Function to show usage
  120. show_usage() {
  121. echo "Usage: $0 [OPTIONS]"
  122. echo ""
  123. echo "Options:"
  124. echo " --push Build and push to registry (requires REGISTRY env var)"
  125. echo " --clean Clean up built images after saving"
  126. echo " --help Show this help message"
  127. echo ""
  128. echo "Environment variables:"
  129. echo " REGISTRY Docker registry URL (e.g., docker.io/username)"
  130. echo " VERSION Image version (default: latest)"
  131. echo ""
  132. echo "Examples:"
  133. echo " $0 # Build and save images locally"
  134. echo " $0 --push # Build and push to registry"
  135. echo " REGISTRY=docker.io/username $0 --push # Build and push to specific registry"
  136. }
  137. # Main execution
  138. main() {
  139. local push_flag=false
  140. local clean_flag=false
  141. # Parse command line arguments
  142. while [[ $# -gt 0 ]]; do
  143. case $1 in
  144. --push)
  145. push_flag=true
  146. shift
  147. ;;
  148. --clean)
  149. clean_flag=true
  150. shift
  151. ;;
  152. --help)
  153. show_usage
  154. exit 0
  155. ;;
  156. *)
  157. print_error "Unknown option: $1"
  158. show_usage
  159. exit 1
  160. ;;
  161. esac
  162. done
  163. print_status "Starting build process for ui-apt-mirror..."
  164. # Check prerequisites
  165. check_prerequisites
  166. # Setup builder
  167. setup_builder
  168. # Build images
  169. build_images
  170. # Push to registry if requested
  171. if [ "$push_flag" = true ]; then
  172. build_and_push --push
  173. fi
  174. # Cleanup if requested
  175. if [ "$clean_flag" = true ]; then
  176. cleanup
  177. fi
  178. print_success "Build process completed successfully!"
  179. print_status "Images saved to $DIST_DIR/:"
  180. ls -la "$DIST_DIR"/*.tar.gz 2>/dev/null || print_warning "No tar files found in $DIST_DIR"
  181. }
  182. # Run main function with all arguments
  183. main "$@"