Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | # Copyright 2016 gRPC authors. |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 3 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 7 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 9 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 15 | # |
| 16 | # Builds docker image and runs a command under it. |
| 17 | # You should never need to call this script on your own. |
| 18 | |
| 19 | set -ex |
| 20 | |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 21 | cd "$(dirname "$0")/../../.." |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 22 | git_root=$(pwd) |
| 23 | cd - |
| 24 | |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 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) |
Jan Tattermusch | 38b519a | 2016-01-27 18:32:42 -0800 | [diff] [blame] | 28 | # OUTPUT_DIR - Directory that will be copied from inside docker after finishing. |
Jan Tattermusch | b04a0cc | 2016-11-30 12:57:20 +0100 | [diff] [blame] | 29 | # DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org. |
Ken Payson | 626fb45 | 2017-02-17 22:52:27 -0800 | [diff] [blame] | 30 | # DOCKER_BASE_IMAGE - If set, pull the latest base image. |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 31 | # $@ - Extra args to pass to docker run |
| 32 | |
| 33 | # Use image name based on Dockerfile location checksum |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 34 | DOCKER_IMAGE_NAME=$(basename "$DOCKERFILE_DIR")_$(sha1sum "$DOCKERFILE_DIR/Dockerfile" | cut -f1 -d\ ) |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 35 | |
Ken Payson | 626fb45 | 2017-02-17 22:52:27 -0800 | [diff] [blame] | 36 | # Pull the base image to force an update |
| 37 | if [ "$DOCKER_BASE_IMAGE" != "" ] |
| 38 | then |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 39 | time docker pull "$DOCKER_BASE_IMAGE" |
Ken Payson | 626fb45 | 2017-02-17 22:52:27 -0800 | [diff] [blame] | 40 | fi |
| 41 | |
Jan Tattermusch | b04a0cc | 2016-11-30 12:57:20 +0100 | [diff] [blame] | 42 | if [ "$DOCKERHUB_ORGANIZATION" != "" ] |
| 43 | then |
| 44 | DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 45 | time docker pull "$DOCKER_IMAGE_NAME" |
Jan Tattermusch | b04a0cc | 2016-11-30 12:57:20 +0100 | [diff] [blame] | 46 | else |
| 47 | # Make sure docker image has been built. Should be instantaneous if so. |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 48 | docker build -t "$DOCKER_IMAGE_NAME" "$DOCKERFILE_DIR" |
Jan Tattermusch | b04a0cc | 2016-11-30 12:57:20 +0100 | [diff] [blame] | 49 | fi |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 50 | |
| 51 | # Choose random name for docker container |
| 52 | CONTAINER_NAME="build_and_run_docker_$(uuidgen)" |
| 53 | |
| 54 | # Run command inside docker |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 55 | # TODO: use a proper array instead of $EXTRA_DOCKER_ARGS |
| 56 | # shellcheck disable=SC2086 |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 57 | docker run \ |
| 58 | "$@" \ |
Jan Tattermusch | 38b519a | 2016-01-27 18:32:42 -0800 | [diff] [blame] | 59 | -e EXTERNAL_GIT_ROOT="/var/local/jenkins/grpc" \ |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 60 | -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \ |
| 61 | -v "$git_root:/var/local/jenkins/grpc:ro" \ |
| 62 | -w /var/local/git/grpc \ |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 63 | --name="$CONTAINER_NAME" \ |
Ken Payson | 7ff21a6 | 2017-05-10 12:24:13 -0700 | [diff] [blame] | 64 | $EXTRA_DOCKER_ARGS \ |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 65 | "$DOCKER_IMAGE_NAME" \ |
Ken Payson | 7ff21a6 | 2017-05-10 12:24:13 -0700 | [diff] [blame] | 66 | /bin/bash -l "/var/local/jenkins/grpc/$DOCKER_RUN_SCRIPT" || FAILED="true" |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 67 | |
| 68 | # Copy output artifacts |
| 69 | if [ "$OUTPUT_DIR" != "" ] |
| 70 | then |
| 71 | docker cp "$CONTAINER_NAME:/var/local/git/grpc/$OUTPUT_DIR" "$git_root" || FAILED="true" |
| 72 | fi |
| 73 | |
| 74 | # remove the container, possibly killing it first |
Mehrdad Afshari | b7f88cf | 2018-01-18 10:46:59 -0800 | [diff] [blame] | 75 | docker rm -f "$CONTAINER_NAME" || true |
Jan Tattermusch | 7eba172 | 2016-01-19 08:43:00 -0800 | [diff] [blame] | 76 | |
| 77 | if [ "$FAILED" != "" ] |
| 78 | then |
| 79 | exit 1 |
| 80 | fi |