dev.sh 4.1 KB

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