build_and_run_docker.sh 1.88 KB
Newer Older
1 2 3 4 5
#!/bin/bash
#
# Builds docker image and runs a command under it.
# This is a generic script that is configured with the following variables:
#
Paul Yang's avatar
Paul Yang committed
6 7
# DOCKERHUB_ORGANIZATION - The organization on docker hub storing the
# Dockerfile.
8 9 10 11 12 13 14
# DOCKERFILE_DIR - Directory in which Dockerfile file is located.
# DOCKER_RUN_SCRIPT - Script to run under docker (relative to protobuf repo root)
# OUTPUT_DIR - Directory that will be copied from inside docker after finishing.
# $@ - Extra args to pass to docker run

set -ex

15
cd $(dirname $0)/../..
16 17 18
git_root=$(pwd)
cd -

19
# Use image name based on Dockerfile sha1
Paul Yang's avatar
Paul Yang committed
20 21 22 23 24 25 26 27 28 29
if [ -z "$DOCKERHUB_ORGANIZATION" ]
then
  DOCKERHUB_ORGANIZATION=grpctesting/protobuf
  DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
else
  # TODO(teboring): Remove this when all tests have been migrated to separate
  # docker images.
  DOCKERFILE_PREFIX=$(basename $DOCKERFILE_DIR)
  DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}/${DOCKERFILE_PREFIX}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
fi
30

31 32
# Pull dockerimage from Dockerhub
docker pull $DOCKER_IMAGE_NAME
33 34 35 36 37 38 39 40

# Ensure existence of ccache directory
CCACHE_DIR=/tmp/protobuf-ccache
mkdir -p $CCACHE_DIR

# Choose random name for docker container
CONTAINER_NAME="build_and_run_docker_$(uuidgen)"

41 42
echo $git_root

43 44 45 46
# Run command inside docker
docker run \
  "$@" \
  -e CCACHE_DIR=$CCACHE_DIR \
47 48
  -e KOKORO_BUILD_NUMBER=$KOKORO_BUILD_NUMBER \
  -e KOKORO_BUILD_ID=$KOKORO_BUILD_ID \
49
  -e EXTERNAL_GIT_ROOT="/var/local/kokoro/protobuf" \
50
  -e TEST_SET="$TEST_SET" \
51
  -v "$git_root:/var/local/kokoro/protobuf:ro" \
52 53 54 55
  -v $CCACHE_DIR:$CCACHE_DIR \
  -w /var/local/git/protobuf \
  --name=$CONTAINER_NAME \
  $DOCKER_IMAGE_NAME \
56
  bash -l "/var/local/kokoro/protobuf/$DOCKER_RUN_SCRIPT" || FAILED="true"
57 58 59 60

# remove the container, possibly killing it first
docker rm -f $CONTAINER_NAME || true

Feng Xiao's avatar
Feng Xiao committed
61
[ -z "$FAILED" ] || {
62
  exit 1
Feng Xiao's avatar
Feng Xiao committed
63
}