Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Jan Tattermusch | 29dadb2 | 2016-01-19 12:56:16 -0800 | [diff] [blame] | 2 | # Copyright 2016, Google Inc. |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 3 | # 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 | |
| 34 | set -ex |
| 35 | |
Jan Tattermusch | 26ca776 | 2016-04-29 14:51:35 -0700 | [diff] [blame] | 36 | cd $(dirname $0)/../../.. |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 37 | git_root=$(pwd) |
| 38 | cd - |
| 39 | |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 40 | # 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 Tattermusch | 38b519a | 2016-01-27 18:32:42 -0800 | [diff] [blame] | 43 | # OUTPUT_DIR - Directory that will be copied from inside docker after finishing. |
Jan Tattermusch | b04a0cc | 2016-11-30 12:57:20 +0100 | [diff] [blame] | 44 | # DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org. |
Ken Payson | 626fb45 | 2017-02-17 22:52:27 -0800 | [diff] [blame] | 45 | # DOCKER_BASE_IMAGE - If set, pull the latest base image. |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 46 | # $@ - Extra args to pass to docker run |
| 47 | |
| 48 | # Use image name based on Dockerfile location checksum |
| 49 | DOCKER_IMAGE_NAME=$(basename $DOCKERFILE_DIR)_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ ) |
| 50 | |
Ken Payson | 626fb45 | 2017-02-17 22:52:27 -0800 | [diff] [blame] | 51 | # Pull the base image to force an update |
| 52 | if [ "$DOCKER_BASE_IMAGE" != "" ] |
| 53 | then |
| 54 | docker pull $DOCKER_BASE_IMAGE |
| 55 | fi |
| 56 | |
Jan Tattermusch | b04a0cc | 2016-11-30 12:57:20 +0100 | [diff] [blame] | 57 | if [ "$DOCKERHUB_ORGANIZATION" != "" ] |
| 58 | then |
| 59 | DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME |
| 60 | docker pull $DOCKER_IMAGE_NAME |
| 61 | else |
| 62 | # Make sure docker image has been built. Should be instantaneous if so. |
| 63 | docker build -t $DOCKER_IMAGE_NAME $DOCKERFILE_DIR |
| 64 | fi |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 65 | |
| 66 | # Choose random name for docker container |
| 67 | CONTAINER_NAME="build_and_run_docker_$(uuidgen)" |
| 68 | |
| 69 | # Run command inside docker |
| 70 | docker run \ |
| 71 | "$@" \ |
Jan Tattermusch | 38b519a | 2016-01-27 18:32:42 -0800 | [diff] [blame] | 72 | -e EXTERNAL_GIT_ROOT="/var/local/jenkins/grpc" \ |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 73 | -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 Payson | 7ff21a6 | 2017-05-10 12:24:13 -0700 | [diff] [blame] | 77 | $EXTRA_DOCKER_ARGS \ |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 78 | $DOCKER_IMAGE_NAME \ |
Ken Payson | 7ff21a6 | 2017-05-10 12:24:13 -0700 | [diff] [blame] | 79 | /bin/bash -l "/var/local/jenkins/grpc/$DOCKER_RUN_SCRIPT" || FAILED="true" |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 80 | |
| 81 | # Copy output artifacts |
| 82 | if [ "$OUTPUT_DIR" != "" ] |
| 83 | then |
| 84 | docker cp "$CONTAINER_NAME:/var/local/git/grpc/$OUTPUT_DIR" "$git_root" || FAILED="true" |
| 85 | fi |
| 86 | |
| 87 | # remove the container, possibly killing it first |
| 88 | docker rm -f $CONTAINER_NAME || true |
| 89 | |
| 90 | if [ "$FAILED" != "" ] |
| 91 | then |
| 92 | exit 1 |
| 93 | fi |