dockerbuild.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. #
  4. # variables
  5. #
  6. RESET="\033[0m"
  7. RED="\033[0;31m"
  8. YELLOW="\033[0;33m"
  9. MAGENTA="\033[0;95m"
  10. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  11. build_args=()
  12. docker_args=()
  13. #
  14. # Functions
  15. #
  16. __usage() {
  17. echo "Usage: $(basename "${BASH_SOURCE[0]}") <image> [options] [[--] <Arguments>...]"
  18. echo ""
  19. echo "Arguments:"
  20. echo " image The docker image to use."
  21. echo " <Arguments>... Arguments passed to the command. Variable number of arguments allowed."
  22. echo ""
  23. echo "Options:"
  24. echo " -v, --volume <VOLUME> An additional volume mount to add to the build container"
  25. echo " -e, --env <NAME=VAL> Additional environment variables to add to the build container"
  26. echo ""
  27. echo "Description:"
  28. echo " This will run build.sh inside the dockerfile as defined in eng/docker/\$image.Dockerfile."
  29. if [[ "${1:-}" != '--no-exit' ]]; then
  30. exit 2
  31. fi
  32. }
  33. __error() {
  34. echo -e "${RED}error: $*${RESET}" 1>&2
  35. }
  36. __warn() {
  37. echo -e "${YELLOW}warning: $*${RESET}"
  38. }
  39. __machine_has() {
  40. hash "$1" > /dev/null 2>&1
  41. return $?
  42. }
  43. #
  44. # main
  45. #
  46. image="${1:-}"
  47. shift || True
  48. while [[ $# -gt 0 ]]; do
  49. case $1 in
  50. -\?|-h|--help)
  51. __usage --no-exit
  52. exit 0
  53. ;;
  54. -v|--volume)
  55. shift
  56. volume_spec="${1:-}"
  57. [ -z "$volume_spec" ] && __error "Missing value for parameter --volume" && __usage
  58. docker_args[${#docker_args[*]}]="--volume"
  59. docker_args[${#docker_args[*]}]="$volume_spec"
  60. ;;
  61. -e|--env)
  62. shift
  63. env_var="${1:-}"
  64. [ -z "$env_var" ] && __error "Missing value for parameter --env" && __usage
  65. docker_args[${#docker_args[*]}]="-e"
  66. docker_args[${#docker_args[*]}]="$env_var"
  67. ;;
  68. *)
  69. build_args[${#build_args[*]}]="$1"
  70. ;;
  71. esac
  72. shift
  73. done
  74. if [ -z "$image" ]; then
  75. __usage --no-exit
  76. __error 'Missing required argument: image'
  77. exit 1
  78. fi
  79. if ! __machine_has docker; then
  80. __error 'Missing required command: docker'
  81. exit 1
  82. fi
  83. commit_hash="$(git rev-parse HEAD || true)"
  84. if [ ! -z "$commit_hash" ]; then
  85. build_args[${#build_args[*]}]="-p:SourceRevisionId=$commit_hash"
  86. fi
  87. dockerfile="$DIR/eng/docker/$image.Dockerfile"
  88. tagname="aspnetcore-build-$image"
  89. # Use docker pull with retries to pre-pull the image need by the dockerfile
  90. # docker build regularly fails with TLS handshake issues for unclear reasons.
  91. base_imagename="$(grep -E -o 'FROM (.*)' $dockerfile | cut -c 6-)"
  92. pull_retries=3
  93. while [ $pull_retries -gt 0 ]; do
  94. failed=false
  95. docker pull $base_imagename || failed=true
  96. if [ "$failed" = true ]; then
  97. let pull_retries=pull_retries-1
  98. echo -e "${YELLOW}Failed to pull $base_imagename Retries left: $pull_retries.${RESET}"
  99. sleep 1
  100. else
  101. pull_retries=0
  102. fi
  103. done
  104. docker build "$(dirname "$dockerfile")" \
  105. --build-arg "USER=$(whoami)" \
  106. --build-arg "USER_ID=$(id -u)" \
  107. --build-arg "GROUP_ID=$(id -g)" \
  108. --build-arg "WORKDIR=$DIR" \
  109. --tag $tagname \
  110. -f "$dockerfile"
  111. docker run \
  112. --rm \
  113. -t \
  114. -e TF_BUILD \
  115. -e BUILD_NUMBER \
  116. -e BUILD_BUILDID \
  117. -e SYSTEM_TEAMPROJECT \
  118. -e BUILD_BUILDNUMBER \
  119. -e BUILD_REPOSITORY_NAME \
  120. -e BUILD_REPOSITORY_URI \
  121. -e BUILD_REPOSITORY_NAME \
  122. -e BUILD_SOURCEVERSION \
  123. -e BUILD_SOURCEBRANCH \
  124. -e SYSTEM_DEFINITIONID \
  125. -e SYSTEM_TEAMFOUNDATIONCOLLECTIONURI \
  126. -e DOTNET_CLI_TELEMETRY_OPTOUT \
  127. -e Configuration \
  128. -v "$DIR:$DIR" \
  129. ${docker_args[@]+"${docker_args[@]}"} \
  130. $tagname \
  131. ./eng/build.sh \
  132. ${build_args[@]+"${build_args[@]}"}