build_and_run_docker.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # Copyright 2016 The gRPC Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # This script is invoked by run_tests.py to accommodate "test under docker"
  17. # scenario. You should never need to call this script on your own.
  18. # shellcheck disable=SC2103
  19. set -ex
  20. cd "$(dirname "$0")/../../.."
  21. git_root=$(pwd)
  22. cd -
  23. # Inputs
  24. # DOCKERFILE_DIR - Directory in which Dockerfile file is located.
  25. # DOCKER_RUN_SCRIPT - Script to run under docker (relative to grpc repo root)
  26. # OUTPUT_DIR - Directory (relatively to git repo root) that will be copied from inside docker container after finishing.
  27. # DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org.
  28. # $@ - Extra args to pass to the "docker run" command.
  29. # Use image name based on Dockerfile location checksum
  30. DOCKER_IMAGE_NAME=$(basename "$DOCKERFILE_DIR"):$(sha1sum "$DOCKERFILE_DIR/Dockerfile" | cut -f1 -d\ )
  31. if [ "$DOCKERHUB_ORGANIZATION" != "" ]
  32. then
  33. DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME
  34. time docker pull "$DOCKER_IMAGE_NAME"
  35. else
  36. # Make sure docker image has been built. Should be instantaneous if so.
  37. docker build -t "$DOCKER_IMAGE_NAME" "$DOCKERFILE_DIR"
  38. fi
  39. if [[ -t 0 ]]; then
  40. DOCKER_TTY_ARGS="-it"
  41. else
  42. # The input device on kokoro is not a TTY, so -it does not work.
  43. DOCKER_TTY_ARGS=
  44. fi
  45. # Git root as seen by the docker instance
  46. # TODO(jtattermusch): rename to a more descriptive directory name
  47. # currently that's nontrivial because the name is hardcoded in many places.
  48. EXTERNAL_GIT_ROOT=/var/local/jenkins/grpc
  49. # temporary directory that will be mounted to the docker container
  50. # as a way to persist output files.
  51. # use unique name for the output directory to prevent clash between concurrent
  52. # runs of multiple docker containers
  53. TEMP_REPORT_DIR="$(mktemp -d)"
  54. TEMP_OUTPUT_DIR="$(mktemp -d)"
  55. # Run tests inside docker
  56. DOCKER_EXIT_CODE=0
  57. # TODO: silence complaint about $DOCKER_TTY_ARGS expansion in some other way
  58. # shellcheck disable=SC2086,SC2154
  59. docker run \
  60. "$@" \
  61. ${DOCKER_TTY_ARGS} \
  62. ${EXTRA_DOCKER_ARGS} \
  63. --cap-add SYS_PTRACE \
  64. -e "DOCKER_RUN_SCRIPT_COMMAND=${DOCKER_RUN_SCRIPT_COMMAND}" \
  65. -e "EXTERNAL_GIT_ROOT=${EXTERNAL_GIT_ROOT}" \
  66. -e "OUTPUT_DIR=${OUTPUT_DIR}" \
  67. --env-file tools/run_tests/dockerize/docker_propagate_env.list \
  68. --rm \
  69. --sysctl net.ipv6.conf.all.disable_ipv6=0 \
  70. -v "${git_root}:${EXTERNAL_GIT_ROOT}" \
  71. -v "${TEMP_REPORT_DIR}:/var/local/report_dir" \
  72. -v "${TEMP_OUTPUT_DIR}:/var/local/output_dir" \
  73. "${DOCKER_IMAGE_NAME}" \
  74. bash -l "/var/local/jenkins/grpc/${DOCKER_RUN_SCRIPT}" || DOCKER_EXIT_CODE=$?
  75. # Copy reports stored by the container (if any)
  76. if [ "${GRPC_TEST_REPORT_BASE_DIR}" != "" ]
  77. then
  78. mkdir -p "${GRPC_TEST_REPORT_BASE_DIR}"
  79. cp -r "${TEMP_REPORT_DIR}"/* "${GRPC_TEST_REPORT_BASE_DIR}" || true
  80. else
  81. cp -r "${TEMP_REPORT_DIR}"/* "${git_root}" || true
  82. fi
  83. # Copy contents of OUTPUT_DIR back under the git repo root
  84. if [ "${OUTPUT_DIR}" != "" ]
  85. then
  86. # create the directory if it doesn't exist yet.
  87. mkdir -p "${TEMP_OUTPUT_DIR}/${OUTPUT_DIR}"
  88. cp -r "${TEMP_OUTPUT_DIR}/${OUTPUT_DIR}" "${git_root}" || DOCKER_EXIT_CODE=$?
  89. fi
  90. exit $DOCKER_EXIT_CODE