1#!/bin/bash 2# Copyright 2016 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# Builds docker image and runs a command under it. 17# You should never need to call this script on your own. 18 19set -ex 20 21cd "$(dirname "$0")/../../.." 22git_root=$(pwd) 23cd - 24 25# Inputs 26# DOCKERFILE_DIR - Directory in which Dockerfile file is located. 27# DOCKER_RUN_SCRIPT - Script to run under docker (relative to grpc repo root) 28# OUTPUT_DIR - Directory that will be copied from inside docker after finishing. 29# DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org. 30# DOCKER_BASE_IMAGE - If set, pull the latest base image. 31# $@ - Extra args to pass to docker run 32 33# Use image name based on Dockerfile location checksum 34DOCKER_IMAGE_NAME=$(basename "$DOCKERFILE_DIR")_$(sha1sum "$DOCKERFILE_DIR/Dockerfile" | cut -f1 -d\ ) 35 36# Pull the base image to force an update 37if [ "$DOCKER_BASE_IMAGE" != "" ] 38then 39 time docker pull "$DOCKER_BASE_IMAGE" 40fi 41 42if [ "$DOCKERHUB_ORGANIZATION" != "" ] 43then 44 DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME 45 time docker pull "$DOCKER_IMAGE_NAME" 46else 47 # Make sure docker image has been built. Should be instantaneous if so. 48 docker build -t "$DOCKER_IMAGE_NAME" "$DOCKERFILE_DIR" 49fi 50 51# Choose random name for docker container 52CONTAINER_NAME="build_and_run_docker_$(uuidgen)" 53 54# Run command inside docker 55# TODO: use a proper array instead of $EXTRA_DOCKER_ARGS 56# shellcheck disable=SC2086 57docker run \ 58 "$@" \ 59 --cap-add SYS_PTRACE \ 60 -e EXTERNAL_GIT_ROOT="/var/local/jenkins/grpc" \ 61 -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \ 62 -e "KOKORO_BUILD_ID=$KOKORO_BUILD_ID" \ 63 -e "KOKORO_BUILD_NUMBER=$KOKORO_BUILD_NUMBER" \ 64 -e "KOKORO_BUILD_URL=$KOKORO_BUILD_URL" \ 65 -e "KOKORO_JOB_NAME=$KOKORO_JOB_NAME" \ 66 -v "$git_root:/var/local/jenkins/grpc:ro" \ 67 -w /var/local/git/grpc \ 68 --name="$CONTAINER_NAME" \ 69 $EXTRA_DOCKER_ARGS \ 70 "$DOCKER_IMAGE_NAME" \ 71 /bin/bash -l "/var/local/jenkins/grpc/$DOCKER_RUN_SCRIPT" || FAILED="true" 72 73# Copy output artifacts 74if [ "$OUTPUT_DIR" != "" ] 75then 76 # Create the artifact directory in advance to avoid a race in "docker cp" if tasks 77 # that were running in parallel finish at the same time. 78 # see https://github.com/grpc/grpc/issues/16155 79 mkdir -p "$git_root/$OUTPUT_DIR" 80 docker cp "$CONTAINER_NAME:/var/local/git/grpc/$OUTPUT_DIR" "$git_root" || FAILED="true" 81fi 82 83# remove the container, possibly killing it first 84docker rm -f "$CONTAINER_NAME" || true 85 86if [ "$FAILED" != "" ] 87then 88 exit 1 89fi 90