aboutsummaryrefslogtreecommitdiffhomepage
path: root/kokoro/linux/build_and_run_docker.sh
diff options
context:
space:
mode:
authorGravatar Matt Kwong <mattkwong@google.com>2017-12-19 11:51:43 -0800
committerGravatar Matt Kwong <mattkwong@google.com>2017-12-19 13:06:09 -0800
commit8ac050fadd7d08eae525a5a227a1e87ea15d57fc (patch)
treebbfdbee24af39cba0b86c4ede603e0466e1b418e /kokoro/linux/build_and_run_docker.sh
parent860d693cf70f13d823ab8e6f9118d94485fa95bd (diff)
Migrate Jenkins jobs to Kokoro
Diffstat (limited to 'kokoro/linux/build_and_run_docker.sh')
-rwxr-xr-xkokoro/linux/build_and_run_docker.sh57
1 files changed, 57 insertions, 0 deletions
diff --git a/kokoro/linux/build_and_run_docker.sh b/kokoro/linux/build_and_run_docker.sh
new file mode 100755
index 00000000..50e1e8c6
--- /dev/null
+++ b/kokoro/linux/build_and_run_docker.sh
@@ -0,0 +1,57 @@
+#!/bin/bash
+#
+# Builds docker image and runs a command under it.
+# This is a generic script that is configured with the following variables:
+#
+# DOCKERFILE_DIR - Directory in which Dockerfile file is located.
+# DOCKER_RUN_SCRIPT - Script to run under docker (relative to protobuf repo root)
+# OUTPUT_DIR - Directory that will be copied from inside docker after finishing.
+# $@ - Extra args to pass to docker run
+
+
+set -ex
+
+cd $(dirname $0)/..
+git_root=$(pwd)
+cd -
+
+# Use image name based on Dockerfile location checksum
+DOCKER_IMAGE_NAME=$(basename $DOCKERFILE_DIR)_$(sha1sum $DOCKERFILE_DIR/Dockerfile | cut -f1 -d\ )
+
+# Make sure docker image has been built. Should be instantaneous if so.
+docker build -t $DOCKER_IMAGE_NAME $DOCKERFILE_DIR
+
+# Ensure existence of ccache directory
+CCACHE_DIR=/tmp/protobuf-ccache
+mkdir -p $CCACHE_DIR
+
+# Choose random name for docker container
+CONTAINER_NAME="build_and_run_docker_$(uuidgen)"
+
+# Run command inside docker
+docker run \
+ "$@" \
+ -e CCACHE_DIR=$CCACHE_DIR \
+ -e EXTERNAL_GIT_ROOT="/var/local/jenkins/protobuf" \
+ -e TEST_SET="$TEST_SET" \
+ -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
+ -v "$git_root:/var/local/jenkins/protobuf:ro" \
+ -v $CCACHE_DIR:$CCACHE_DIR \
+ -w /var/local/git/protobuf \
+ --name=$CONTAINER_NAME \
+ $DOCKER_IMAGE_NAME \
+ bash -l "/var/local/jenkins/protobuf/$DOCKER_RUN_SCRIPT" || FAILED="true"
+
+# Copy output artifacts
+if [ "$OUTPUT_DIR" != "" ]
+then
+ docker cp "$CONTAINER_NAME:/var/local/git/protobuf/$OUTPUT_DIR" "$git_root" || FAILED="true"
+fi
+
+# remove the container, possibly killing it first
+docker rm -f $CONTAINER_NAME || true
+
+if [ "$FAILED" != "" ]
+then
+ exit 1
+fi