build_interop_image.sh 4.5 KB

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