dev.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. #exit on error
  3. set -oe pipefail
  4. ##########################################################################
  5. # Usage: build.sh --variation <variation> --version <version> --os <os>
  6. ##########################################################################
  7. # This script is used to build a Docker image for a specific PHP version
  8. # and variation. It is intended to be used for local development. You may
  9. # also change the DOCKER_REPOSITORY environment variable or pass other
  10. # arguments to the docker build command, like "--no-cache".
  11. ##########################
  12. # Environment Settings
  13. # Script Configurations
  14. SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
  15. PROJECT_ROOT_DIR="$(dirname "$SCRIPT_DIR")"
  16. PHP_BUILD_VERSION=""
  17. PHP_BUILD_VARIATION=""
  18. PHP_BUILD_BASE_OS=""
  19. PHP_BUILD_PREFIX=""
  20. DOCKER_REPOSITORY="${DOCKER_REPOSITORY:-"serversideup/php"}"
  21. DOCKER_ADDITIONAL_BUILD_ARGS=()
  22. CUSTOM_REGISTRY=""
  23. PLATFORM=""
  24. # UI Colors
  25. function ui_set_yellow {
  26. printf $'\033[0;33m'
  27. }
  28. function ui_set_green {
  29. printf $'\033[0;32m'
  30. }
  31. function ui_set_red {
  32. printf $'\033[0;31m'
  33. }
  34. function ui_reset_colors {
  35. printf "\e[0m"
  36. }
  37. function echo_color_message (){
  38. color=$1
  39. message=$2
  40. ui_set_$color
  41. echo "$message"
  42. ui_reset_colors
  43. }
  44. check_vars() {
  45. message=$1
  46. shift
  47. for variable in "$@"; do
  48. if [ -z "${!variable}" ]; then
  49. echo_color_message red "$message: $variable"
  50. echo
  51. help_menu
  52. return 1
  53. fi
  54. done
  55. return 0
  56. }
  57. detect_platform() {
  58. local arch=$(uname -m)
  59. case $arch in
  60. x86_64)
  61. echo "linux/amd64"
  62. ;;
  63. arm64|aarch64)
  64. echo "linux/arm64/v8"
  65. ;;
  66. *)
  67. echo "Unsupported architecture: $arch" >&2
  68. exit 1
  69. ;;
  70. esac
  71. }
  72. build_docker_image() {
  73. build_tag="${DOCKER_REPOSITORY}:${PHP_BUILD_PREFIX}${PHP_BUILD_VERSION}-${PHP_BUILD_VARIATION}-${PHP_BUILD_BASE_OS}"
  74. echo_color_message yellow "๐Ÿณ Building Docker Image: $build_tag"
  75. # Set default platform if not specified
  76. if [ -z "$PLATFORM" ]; then
  77. PLATFORM=$(detect_platform)
  78. fi
  79. docker buildx build \
  80. "${DOCKER_ADDITIONAL_BUILD_ARGS[@]}" \
  81. --platform "$PLATFORM" \
  82. --build-arg PHP_VARIATION="$PHP_BUILD_VARIATION" \
  83. --build-arg PHP_VERSION="$PHP_BUILD_VERSION" \
  84. --build-arg BASE_OS_VERSION="$PHP_BUILD_BASE_OS" \
  85. --tag "$build_tag" \
  86. --file "$PROJECT_ROOT_DIR/src/variations/$PHP_BUILD_VARIATION/Dockerfile" \
  87. "$PROJECT_ROOT_DIR"
  88. echo_color_message green "โœ… Docker Image Built: $build_tag"
  89. if [ -n "$CUSTOM_REGISTRY" ]; then
  90. registry_tag="${CUSTOM_REGISTRY}/${build_tag}"
  91. echo_color_message yellow "๐Ÿท๏ธ Tagging image for custom registry: $registry_tag"
  92. docker tag "$build_tag" "$registry_tag"
  93. echo_color_message yellow "๐Ÿš€ Pushing image to custom registry: $registry_tag"
  94. docker push "$registry_tag"
  95. echo_color_message green "โœ… Image pushed to custom registry: $registry_tag"
  96. fi
  97. }
  98. help_menu() {
  99. echo "Usage: $0 --variation <variation> --version <version> --os <os> [additional options]"
  100. echo
  101. echo "This script is used to build a Docker image for a specific PHP version"
  102. echo "and variation. It is intended to be used for local development. You may"
  103. echo "also change the DOCKER_REPOSITORY environment variable or pass other"
  104. echo "arguments to the docker build command."
  105. echo
  106. echo "Options:"
  107. echo " --variation <variation> Set the PHP variation (e.g., apache, fpm)"
  108. echo " --version <version> Set the PHP version (e.g., 7.4, 8.0)"
  109. echo " --os <os> Set the base OS (e.g., bullseye, bookworm, alpine)"
  110. echo " --prefix <prefix> Set the prefix for the Docker image (e.g., beta)"
  111. echo " --registry <registry> Set a custom registry (e.g., localhost:5000)"
  112. echo " --platform <platform> Set the platform (default: detected from system architecture)"
  113. echo " --* Any additional options will be passed to the docker buildx command"
  114. echo
  115. echo "Environment Variables:"
  116. echo " DOCKER_REPOSITORY The Docker repository (default: serversideup/php)"
  117. }
  118. ##########################
  119. # Main script starts here
  120. while [[ $# -gt 0 ]]; do
  121. case $1 in
  122. --variation)
  123. PHP_BUILD_VARIATION="$2"
  124. shift 2
  125. ;;
  126. --os)
  127. PHP_BUILD_BASE_OS="$2"
  128. shift 2
  129. ;;
  130. --version)
  131. PHP_BUILD_VERSION="$2"
  132. shift 2
  133. ;;
  134. --prefix)
  135. PHP_BUILD_PREFIX="$2-"
  136. shift 2
  137. ;;
  138. --registry)
  139. CUSTOM_REGISTRY="$2"
  140. shift 2
  141. ;;
  142. --platform)
  143. PLATFORM="$2"
  144. shift 2
  145. ;;
  146. --*)
  147. # If there's a next argument and it starts with '--', treat the current argument as standalone.
  148. # Otherwise, pair the current argument with the next.
  149. if [[ $# -gt 1 && $2 =~ ^-- ]]; then
  150. DOCKER_ADDITIONAL_BUILD_ARGS+=("$1")
  151. shift
  152. else
  153. DOCKER_ADDITIONAL_BUILD_ARGS+=("$1")
  154. [[ $# -gt 1 ]] && DOCKER_ADDITIONAL_BUILD_ARGS+=("$2") && shift
  155. shift
  156. fi
  157. ;;
  158. *)
  159. # Skip the argument if not recognized
  160. shift
  161. ;;
  162. esac
  163. done
  164. check_vars \
  165. "๐Ÿšจ Required variables not set" \
  166. PHP_BUILD_VARIATION \
  167. PHP_BUILD_VERSION \
  168. PHP_BUILD_BASE_OS
  169. build_docker_image