build_and_run_docker.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. #
  3. # Builds docker image and runs a command under it.
  4. # This is a generic script that is configured with the following variables:
  5. #
  6. # DOCKERHUB_ORGANIZATION - The organization on docker hub storing the
  7. # Dockerfile.
  8. # DOCKERFILE_DIR - Directory in which Dockerfile file is located.
  9. # DOCKER_RUN_SCRIPT - Script to run under docker (relative to protobuf repo root)
  10. # OUTPUT_DIR - Directory that will be copied from inside docker after finishing.
  11. # $@ - Extra args to pass to docker run
  12. set -ex
  13. cd $(dirname $0)/../..
  14. git_root=$(pwd)
  15. cd -
  16. # Use image name based on Dockerfile sha1
  17. if [ -z "$DOCKERHUB_ORGANIZATION" ]
  18. then
  19. DOCKERHUB_ORGANIZATION=grpctesting/protobuf
  20. DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
  21. else
  22. # TODO(teboring): Remove this when all tests have been migrated to separate
  23. # docker images.
  24. DOCKERFILE_PREFIX=$(basename $DOCKERFILE_DIR)
  25. DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}/${DOCKERFILE_PREFIX}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
  26. fi
  27. # Pull dockerimage from Dockerhub
  28. docker pull $DOCKER_IMAGE_NAME
  29. # Ensure existence of ccache directory
  30. CCACHE_DIR=/tmp/protobuf-ccache
  31. mkdir -p $CCACHE_DIR
  32. # Choose random name for docker container
  33. CONTAINER_NAME="build_and_run_docker_$(uuidgen)"
  34. echo $git_root
  35. # Run command inside docker
  36. docker run \
  37. "$@" \
  38. -e CCACHE_DIR=$CCACHE_DIR \
  39. -e KOKORO_BUILD_NUMBER=$KOKORO_BUILD_NUMBER \
  40. -e KOKORO_BUILD_ID=$KOKORO_BUILD_ID \
  41. -e EXTERNAL_GIT_ROOT="/var/local/kokoro/protobuf" \
  42. -e TEST_SET="$TEST_SET" \
  43. -v "$git_root:/var/local/kokoro/protobuf:ro" \
  44. -v $CCACHE_DIR:$CCACHE_DIR \
  45. -w /var/local/git/protobuf \
  46. --name=$CONTAINER_NAME \
  47. $DOCKER_IMAGE_NAME \
  48. bash -l "/var/local/kokoro/protobuf/$DOCKER_RUN_SCRIPT" || FAILED="true"
  49. # remove the container, possibly killing it first
  50. docker rm -f $CONTAINER_NAME || true
  51. [ -z "$FAILED" ] || {
  52. exit 1
  53. }