blob: 6189e9a5c0276391d6b3eab58d32da5b7138f388 [file] [log] [blame]
Jan Tattermusch7eba1722016-01-19 08:43:00 -08001#!/bin/bash
Jan Tattermusch29dadb22016-01-19 12:56:16 -08002# Copyright 2016, Google Inc.
Jan Tattermusch7eba1722016-01-19 08:43:00 -08003# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31# Builds docker image and runs a command under it.
32# You should never need to call this script on your own.
33
34set -ex
35
Jan Tattermusch26ca7762016-04-29 14:51:35 -070036cd $(dirname $0)/../../..
Jan Tattermusch7eba1722016-01-19 08:43:00 -080037git_root=$(pwd)
38cd -
39
Jan Tattermusch7eba1722016-01-19 08:43:00 -080040# Inputs
41# DOCKERFILE_DIR - Directory in which Dockerfile file is located.
42# DOCKER_RUN_SCRIPT - Script to run under docker (relative to grpc repo root)
Jan Tattermusch38b519a2016-01-27 18:32:42 -080043# OUTPUT_DIR - Directory that will be copied from inside docker after finishing.
Jan Tattermuschb04a0cc2016-11-30 12:57:20 +010044# DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org.
Ken Payson626fb452017-02-17 22:52:27 -080045# DOCKER_BASE_IMAGE - If set, pull the latest base image.
Jan Tattermusch7eba1722016-01-19 08:43:00 -080046# $@ - Extra args to pass to docker run
47
48# Use image name based on Dockerfile location checksum
49DOCKER_IMAGE_NAME=$(basename $DOCKERFILE_DIR)_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
50
Ken Payson626fb452017-02-17 22:52:27 -080051# Pull the base image to force an update
52if [ "$DOCKER_BASE_IMAGE" != "" ]
53then
54 docker pull $DOCKER_BASE_IMAGE
55fi
56
Jan Tattermuschb04a0cc2016-11-30 12:57:20 +010057if [ "$DOCKERHUB_ORGANIZATION" != "" ]
58then
59 DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME
60 docker pull $DOCKER_IMAGE_NAME
61else
62 # Make sure docker image has been built. Should be instantaneous if so.
63 docker build -t $DOCKER_IMAGE_NAME $DOCKERFILE_DIR
64fi
Jan Tattermusch7eba1722016-01-19 08:43:00 -080065
66# Choose random name for docker container
67CONTAINER_NAME="build_and_run_docker_$(uuidgen)"
68
69# Run command inside docker
70docker run \
71 "$@" \
Jan Tattermusch38b519a2016-01-27 18:32:42 -080072 -e EXTERNAL_GIT_ROOT="/var/local/jenkins/grpc" \
Jan Tattermusch7eba1722016-01-19 08:43:00 -080073 -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
74 -v "$git_root:/var/local/jenkins/grpc:ro" \
75 -w /var/local/git/grpc \
76 --name=$CONTAINER_NAME \
Ken Payson7ff21a62017-05-10 12:24:13 -070077 $EXTRA_DOCKER_ARGS \
Jan Tattermusch7eba1722016-01-19 08:43:00 -080078 $DOCKER_IMAGE_NAME \
Ken Payson7ff21a62017-05-10 12:24:13 -070079 /bin/bash -l "/var/local/jenkins/grpc/$DOCKER_RUN_SCRIPT" || FAILED="true"
Jan Tattermusch7eba1722016-01-19 08:43:00 -080080
81# Copy output artifacts
82if [ "$OUTPUT_DIR" != "" ]
83then
84 docker cp "$CONTAINER_NAME:/var/local/git/grpc/$OUTPUT_DIR" "$git_root" || FAILED="true"
85fi
86
87# remove the container, possibly killing it first
88docker rm -f $CONTAINER_NAME || true
89
90if [ "$FAILED" != "" ]
91then
92 exit 1
93fi