blob: 80aec82c3d62341624fbcacd517f2e4a9e2a96a5 [file] [log] [blame]
Jan Tattermusch7eba1722016-01-19 08:43:00 -08001#!/bin/bash
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2016 gRPC authors.
Jan Tattermusch7eba1722016-01-19 08:43:00 -08003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# 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
Jan Tattermusch7eba1722016-01-19 08:43:00 -08007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch7eba1722016-01-19 08:43:00 -08009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# 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.
Jan Tattermusch7eba1722016-01-19 08:43:00 -080015#
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
Jan Tattermusch26ca7762016-04-29 14:51:35 -070021cd $(dirname $0)/../../..
Jan Tattermusch7eba1722016-01-19 08:43:00 -080022git_root=$(pwd)
23cd -
24
Jan Tattermusch7eba1722016-01-19 08:43:00 -080025# 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)
Jan Tattermusch38b519a2016-01-27 18:32:42 -080028# OUTPUT_DIR - Directory that will be copied from inside docker after finishing.
Jan Tattermuschb04a0cc2016-11-30 12:57:20 +010029# DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org.
Ken Payson626fb452017-02-17 22:52:27 -080030# DOCKER_BASE_IMAGE - If set, pull the latest base image.
Jan Tattermusch7eba1722016-01-19 08:43:00 -080031# $@ - 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
Ken Payson626fb452017-02-17 22:52:27 -080036# Pull the base image to force an update
37if [ "$DOCKER_BASE_IMAGE" != "" ]
38then
39 docker pull $DOCKER_BASE_IMAGE
40fi
41
Jan Tattermuschb04a0cc2016-11-30 12:57:20 +010042if [ "$DOCKERHUB_ORGANIZATION" != "" ]
43then
44 DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME
45 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
Jan Tattermusch7eba1722016-01-19 08:43:00 -080050
51# Choose random name for docker container
52CONTAINER_NAME="build_and_run_docker_$(uuidgen)"
53
54# Run command inside docker
55docker run \
56 "$@" \
Jan Tattermusch38b519a2016-01-27 18:32:42 -080057 -e EXTERNAL_GIT_ROOT="/var/local/jenkins/grpc" \
Jan Tattermusch7eba1722016-01-19 08:43:00 -080058 -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
59 -v "$git_root:/var/local/jenkins/grpc:ro" \
60 -w /var/local/git/grpc \
61 --name=$CONTAINER_NAME \
Ken Payson7ff21a62017-05-10 12:24:13 -070062 $EXTRA_DOCKER_ARGS \
Jan Tattermusch7eba1722016-01-19 08:43:00 -080063 $DOCKER_IMAGE_NAME \
Ken Payson7ff21a62017-05-10 12:24:13 -070064 /bin/bash -l "/var/local/jenkins/grpc/$DOCKER_RUN_SCRIPT" || FAILED="true"
Jan Tattermusch7eba1722016-01-19 08:43:00 -080065
66# Copy output artifacts
67if [ "$OUTPUT_DIR" != "" ]
68then
69 docker cp "$CONTAINER_NAME:/var/local/git/grpc/$OUTPUT_DIR" "$git_root" || FAILED="true"
70fi
71
72# remove the container, possibly killing it first
73docker rm -f $CONTAINER_NAME || true
74
75if [ "$FAILED" != "" ]
76then
77 exit 1
78fi