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