dev.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. # UI Colors
  24. function ui_set_yellow {
  25. printf $'\033[0;33m'
  26. }
  27. function ui_set_green {
  28. printf $'\033[0;32m'
  29. }
  30. function ui_set_red {
  31. printf $'\033[0;31m'
  32. }
  33. function ui_reset_colors {
  34. printf "\e[0m"
  35. }
  36. function echo_color_message (){
  37. color=$1
  38. message=$2
  39. ui_set_$color
  40. echo "$message"
  41. ui_reset_colors
  42. }
  43. check_vars() {
  44. message=$1
  45. shift
  46. for variable in "$@"; do
  47. if [ -z "${!variable}" ]; then
  48. echo_color_message red "$message: $variable"
  49. echo
  50. help_menu
  51. return 1
  52. fi
  53. done
  54. return 0
  55. }
  56. build_docker_image() {
  57. build_tag="${DOCKER_REPOSITORY}:${PHP_BUILD_PREFIX}${PHP_BUILD_VERSION}-${PHP_BUILD_VARIATION}-${PHP_BUILD_BASE_OS}"
  58. echo_color_message yellow "๐Ÿณ Building Docker Image: $build_tag"
  59. docker build \
  60. ${DOCKER_ADDITIONAL_BUILD_ARGS[@]} \
  61. --build-arg PHP_VARIATION="$PHP_BUILD_VARIATION" \
  62. --build-arg PHP_VERSION="$PHP_BUILD_VERSION" \
  63. --build-arg BASE_OS_VERSION="$PHP_BUILD_BASE_OS" \
  64. --tag "$build_tag" \
  65. --file "$PROJECT_ROOT_DIR/src/variations/$PHP_BUILD_VARIATION/Dockerfile" \
  66. "$PROJECT_ROOT_DIR"
  67. echo_color_message green "โœ… Docker Image Built: $build_tag"
  68. if [ -n "$CUSTOM_REGISTRY" ]; then
  69. registry_tag="${CUSTOM_REGISTRY}/${build_tag}"
  70. echo_color_message yellow "๐Ÿท๏ธ Tagging image for custom registry: $registry_tag"
  71. docker tag "$build_tag" "$registry_tag"
  72. echo_color_message yellow "๐Ÿš€ Pushing image to custom registry: $registry_tag"
  73. docker push "$registry_tag"
  74. echo_color_message green "โœ… Image pushed to custom registry: $registry_tag"
  75. fi
  76. }
  77. help_menu() {
  78. echo "Usage: $0 --variation <variation> --version <version> --os <os> [additional options]"
  79. echo
  80. echo "This script is used to build a Docker image for a specific PHP version"
  81. echo "and variation. It is intended to be used for local development. You may"
  82. echo "also change the DOCKER_REPOSITORY environment variable or pass other"
  83. echo "arguments to the docker build command."
  84. echo
  85. echo "Options:"
  86. echo " --variation <variation> Set the PHP variation (e.g., apache, fpm)"
  87. echo " --version <version> Set the PHP version (e.g., 7.4, 8.0)"
  88. echo " --os <os> Set the base OS (e.g., bullseye, bookworm, alpine)"
  89. echo " --prefix <prefix> Set the prefix for the Docker image (e.g., beta)"
  90. echo " --registry <registry> Set a custom registry (e.g., localhost:5000)"
  91. echo " --* Any additional options will be passed to the docker buildx command"
  92. echo
  93. echo "Environment Variables:"
  94. echo " DOCKER_REPOSITORY The Docker repository (default: serversideup/php)"
  95. }
  96. ##########################
  97. # Main script starts here
  98. while [[ $# -gt 0 ]]; do
  99. case $1 in
  100. --variation)
  101. PHP_BUILD_VARIATION="$2"
  102. shift 2
  103. ;;
  104. --os)
  105. PHP_BUILD_BASE_OS="$2"
  106. shift 2
  107. ;;
  108. --version)
  109. PHP_BUILD_VERSION="$2"
  110. shift 2
  111. ;;
  112. --prefix)
  113. PHP_BUILD_PREFIX="$2-"
  114. shift 2
  115. ;;
  116. --registry)
  117. CUSTOM_REGISTRY="$2"
  118. shift 2
  119. ;;
  120. --*)
  121. # If there's a next argument and it starts with '--', treat the current argument as standalone.
  122. # Otherwise, pair the current argument with the next.
  123. if [[ $# -gt 1 && $2 =~ ^-- ]]; then
  124. DOCKER_ADDITIONAL_BUILD_ARGS+=("$1")
  125. shift
  126. else
  127. DOCKER_ADDITIONAL_BUILD_ARGS+=("$1")
  128. [[ $# -gt 1 ]] && DOCKER_ADDITIONAL_BUILD_ARGS+=("$2") && shift
  129. shift
  130. fi
  131. ;;
  132. *)
  133. # Skip the argument if not recognized
  134. shift
  135. ;;
  136. esac
  137. done
  138. check_vars \
  139. "๐Ÿšจ Required variables not set" \
  140. PHP_BUILD_VARIATION \
  141. PHP_BUILD_VERSION \
  142. PHP_BUILD_BASE_OS
  143. build_docker_image