build_and_run_docker.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. # DOCKERFILE_DIR - Directory in which Dockerfile file is located.
  7. # DOCKER_RUN_SCRIPT - Script to run under docker (relative to protobuf repo root)
  8. # OUTPUT_DIR - Directory that will be copied from inside docker after finishing.
  9. # $@ - Extra args to pass to docker run
  10. set -ex
  11. cd $(dirname $0)/../..
  12. git_root=$(pwd)
  13. cd -
  14. # Use image name based on Dockerfile sha1
  15. DOCKERHUB_ORGANIZATION=grpctesting/protobuf
  16. DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
  17. # Pull dockerimage from Dockerhub
  18. docker pull $DOCKER_IMAGE_NAME
  19. # Ensure existence of ccache directory
  20. CCACHE_DIR=/tmp/protobuf-ccache
  21. mkdir -p $CCACHE_DIR
  22. # Choose random name for docker container
  23. CONTAINER_NAME="build_and_run_docker_$(uuidgen)"
  24. echo $git_root
  25. # Run command inside docker
  26. docker run \
  27. "$@" \
  28. -e CCACHE_DIR=$CCACHE_DIR \
  29. -e EXTERNAL_GIT_ROOT="/var/local/kokoro/protobuf" \
  30. -e TEST_SET="$TEST_SET" \
  31. -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
  32. -v "$git_root:/var/local/kokoro/protobuf:ro" \
  33. -v $CCACHE_DIR:$CCACHE_DIR \
  34. -w /var/local/git/protobuf \
  35. --name=$CONTAINER_NAME \
  36. $DOCKER_IMAGE_NAME \
  37. bash -l "/var/local/kokoro/protobuf/$DOCKER_RUN_SCRIPT" || FAILED="true"
  38. # Copy output artifacts
  39. if [ "$OUTPUT_DIR" != "" ]
  40. then
  41. docker cp "$CONTAINER_NAME:/var/local/git/protobuf/$OUTPUT_DIR" "${git_root}/kokoro" || FAILED="true"
  42. fi
  43. # remove the container, possibly killing it first
  44. docker rm -f $CONTAINER_NAME || true
  45. if [ "$FAILED" != "" ]
  46. then
  47. exit 1
  48. fi