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 13set -ex 14 15cd $(dirname $0)/../.. 16git_root=$(pwd) 17cd - 18 19# Use image name based on Dockerfile sha1 20if [ -z "$DOCKERHUB_ORGANIZATION" ] 21then 22 DOCKERHUB_ORGANIZATION=grpctesting/protobuf 23 DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) 24else 25 # TODO(teboring): Remove this when all tests have been migrated to separate 26 # docker images. 27 DOCKERFILE_PREFIX=$(basename $DOCKERFILE_DIR) 28 DOCKER_IMAGE_NAME=${DOCKERHUB_ORGANIZATION}/${DOCKERFILE_PREFIX}_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) 29fi 30 31# Pull dockerimage from Dockerhub 32docker pull $DOCKER_IMAGE_NAME 33 34# Ensure existence of ccache directory 35CCACHE_DIR=/tmp/protobuf-ccache 36mkdir -p $CCACHE_DIR 37 38# Choose random name for docker container 39CONTAINER_NAME="build_and_run_docker_$(uuidgen)" 40 41echo $git_root 42 43# Run command inside docker 44docker run \ 45 "$@" \ 46 -e CCACHE_DIR=$CCACHE_DIR \ 47 -e KOKORO_BUILD_NUMBER=$KOKORO_BUILD_NUMBER \ 48 -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 -v $CCACHE_DIR:$CCACHE_DIR \ 53 -w /var/local/git/protobuf \ 54 --name=$CONTAINER_NAME \ 55 $DOCKER_IMAGE_NAME \ 56 bash -l "/var/local/kokoro/protobuf/$DOCKER_RUN_SCRIPT" || FAILED="true" 57 58# remove the container, possibly killing it first 59docker rm -f $CONTAINER_NAME || true 60 61[ -z "$FAILED" ] || { 62 exit 1 63} 64