xref: /aosp_15_r20/external/grpc-grpc/tools/run_tests/dockerize/build_interop_image.sh (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/bin/bash
2# Copyright 2015 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# This script is invoked by run_interop_tests.py to build the docker image
17# for interop testing. You should never need to call this script on your own.
18
19set -ex
20
21# Params:
22#  INTEROP_IMAGE - name of tag of the final interop image
23#  BASE_NAME - base name used to locate the base Dockerfile and build script
24#  BUILD_INTEROP_DOCKER_EXTRA_ARGS - optional args to be passed to the
25#    docker run command
26#  GRPC_ROOT - grpc base directory, default to top of this tree.
27#  GRPC_GO_ROOT - grpc-go base directory, default to '$GRPC_ROOT/../grpc-go'
28#  GRPC_JAVA_ROOT - grpc-java base directory, default to '$GRPC_ROOT/../grpc-java'
29
30cd "$(dirname "$0")/../../.."
31echo "GRPC_ROOT: ${GRPC_ROOT:=$(pwd)}"
32MOUNT_ARGS="-v $GRPC_ROOT:/var/local/jenkins/grpc:ro"
33
34echo "GRPC_JAVA_ROOT: ${GRPC_JAVA_ROOT:=$(cd ../grpc-java && pwd)}"
35if [ -n "$GRPC_JAVA_ROOT" ]
36then
37  MOUNT_ARGS+=" -v $GRPC_JAVA_ROOT:/var/local/jenkins/grpc-java:ro"
38else
39  echo "WARNING: grpc-java not found, it won't be mounted to the docker container."
40fi
41
42echo "GRPC_GO_ROOT: ${GRPC_GO_ROOT:=$(cd ../grpc-go && pwd)}"
43if [ -n "$GRPC_GO_ROOT" ]
44then
45  MOUNT_ARGS+=" -v $GRPC_GO_ROOT:/var/local/jenkins/grpc-go:ro"
46else
47  echo "WARNING: grpc-go not found, it won't be mounted to the docker container."
48fi
49
50echo "GRPC_DART_ROOT: ${GRPC_DART_ROOT:=$(cd ../grpc-dart && pwd)}"
51if [ -n "$GRPC_DART_ROOT" ]
52then
53  MOUNT_ARGS+=" -v $GRPC_DART_ROOT:/var/local/jenkins/grpc-dart:ro"
54else
55  echo "WARNING: grpc-dart not found, it won't be mounted to the docker container."
56fi
57
58echo "GRPC_NODE_ROOT: ${GRPC_NODE_ROOT:=$(cd ../grpc-node && pwd)}"
59if [ -n "$GRPC_NODE_ROOT" ]
60then
61  MOUNT_ARGS+=" -v $GRPC_NODE_ROOT:/var/local/jenkins/grpc-node:ro"
62else
63  echo "WARNING: grpc-node not found, it won't be mounted to the docker container."
64fi
65
66echo "GRPC_DOTNET_ROOT: ${GRPC_DOTNET_ROOT:=$(cd ../grpc-dotnet && pwd)}"
67if [ -n "$GRPC_DOTNET_ROOT" ]
68then
69  MOUNT_ARGS+=" -v $GRPC_DOTNET_ROOT:/var/local/jenkins/grpc-dotnet:ro"
70else
71  echo "WARNING: grpc-dotnet not found, it won't be mounted to the docker container."
72fi
73
74# Mount service account dir if available.
75# If service_directory does not contain the service account JSON file,
76# some of the tests will fail.
77if [ -e "$HOME/service_account" ]
78then
79  MOUNT_ARGS+=" -v $HOME/service_account:/var/local/jenkins/service_account:ro"
80fi
81
82BASE_IMAGE_DIR="tools/dockerfile/interoptest/$BASE_NAME"
83# The exact base docker image to use and its version is determined by the corresponding .current_version file
84BASE_IMAGE="$(cat "${BASE_IMAGE_DIR}.current_version")"
85
86# If TTY is available, the running container can be conveniently terminated with Ctrl+C.
87if [[ -t 0 ]]; then
88  DOCKER_TTY_ARGS=("-it")
89else
90  # The input device on kokoro is not a TTY, so -it does not work.
91  DOCKER_TTY_ARGS=()
92fi
93
94CONTAINER_NAME="build_${BASE_NAME}_$(uuidgen)"
95
96# Prepare image for interop tests, commit it on success.
97# TODO: Figure out if is safe to eliminate the suppression. It's currently here
98# because $MOUNT_ARGS and $BUILD_INTEROP_DOCKER_EXTRA_ARGS can have legitimate
99# spaces, but the "correct" way to do this is to utilize proper arrays.
100# shellcheck disable=SC2086
101(docker run \
102  --cap-add SYS_PTRACE \
103  --env-file "tools/run_tests/dockerize/docker_propagate_env.list" \
104  "${DOCKER_TTY_ARGS[@]}" \
105  $MOUNT_ARGS \
106  $BUILD_INTEROP_DOCKER_EXTRA_ARGS \
107  --name="$CONTAINER_NAME" \
108  "$BASE_IMAGE" \
109  bash -l "/var/local/jenkins/grpc/tools/dockerfile/interoptest/$BASE_NAME/build_interop.sh" \
110  && docker commit "$CONTAINER_NAME" "$INTEROP_IMAGE" \
111  && echo "Successfully built image $INTEROP_IMAGE")
112EXITCODE=$?
113
114# remove intermediate container, possibly killing it first
115docker rm -f "$CONTAINER_NAME"
116
117exit $EXITCODE
118