|
#!/usr/bin/env bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -euo pipefail |
|
|
|
TRAMPOLINE_VERSION="2.0.5" |
|
|
|
if command -v tput >/dev/null && [[ -n "${TERM:-}" ]]; then |
|
readonly IO_COLOR_RED="$(tput setaf 1)" |
|
readonly IO_COLOR_GREEN="$(tput setaf 2)" |
|
readonly IO_COLOR_YELLOW="$(tput setaf 3)" |
|
readonly IO_COLOR_RESET="$(tput sgr0)" |
|
else |
|
readonly IO_COLOR_RED="" |
|
readonly IO_COLOR_GREEN="" |
|
readonly IO_COLOR_YELLOW="" |
|
readonly IO_COLOR_RESET="" |
|
fi |
|
|
|
function function_exists { |
|
[ $(LC_ALL=C type -t $1)"" == "function" ] |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function log_impl() { |
|
local color="$1" |
|
shift |
|
local timestamp="$(date -u "+%Y-%m-%dT%H:%M:%SZ")" |
|
echo "================================================================" |
|
echo "${color}${timestamp}:" "$@" "${IO_COLOR_RESET}" |
|
echo "================================================================" |
|
} |
|
|
|
|
|
function log() { |
|
log_impl "${IO_COLOR_RESET}" "$@" |
|
} |
|
|
|
|
|
function log_green() { |
|
log_impl "${IO_COLOR_GREEN}" "$@" |
|
} |
|
|
|
|
|
function log_yellow() { |
|
log_impl "${IO_COLOR_YELLOW}" "$@" |
|
} |
|
|
|
|
|
function log_red() { |
|
log_impl "${IO_COLOR_RED}" "$@" |
|
} |
|
|
|
readonly tmpdir=$(mktemp -d -t ci-XXXXXXXX) |
|
readonly tmphome="${tmpdir}/h" |
|
mkdir -p "${tmphome}" |
|
|
|
function cleanup() { |
|
rm -rf "${tmpdir}" |
|
} |
|
trap cleanup EXIT |
|
|
|
RUNNING_IN_CI="${RUNNING_IN_CI:-false}" |
|
|
|
|
|
TRAMPOLINE_WORKSPACE="${TRAMPOLINE_WORKSPACE:-/workspace}" |
|
|
|
pass_down_envvars=( |
|
|
|
|
|
"RUNNING_IN_CI" |
|
|
|
"TRAMPOLINE_CI" |
|
|
|
"TRAMPOLINE_VERSION" |
|
) |
|
|
|
log_yellow "Building with Trampoline ${TRAMPOLINE_VERSION}" |
|
|
|
|
|
|
|
|
|
|
|
if [[ -n "${KOKORO_BUILD_ID:-}" ]]; then |
|
|
|
RUNNING_IN_CI="true" |
|
TRAMPOLINE_CI="kokoro" |
|
if [[ "${TRAMPOLINE_USE_LEGACY_SERVICE_ACCOUNT:-}" == "true" ]]; then |
|
if [[ ! -f "${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json" ]]; then |
|
log_red "${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json does not exist. Did you forget to mount cloud-devrel-kokoro-resources/trampoline? Aborting." |
|
exit 1 |
|
fi |
|
|
|
TRAMPOLINE_SERVICE_ACCOUNT="${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json" |
|
else |
|
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then |
|
gcloud auth list |
|
fi |
|
log_yellow "Configuring Container Registry access" |
|
gcloud auth configure-docker --quiet |
|
fi |
|
pass_down_envvars+=( |
|
|
|
"KOKORO_BUILD_NUMBER" |
|
"KOKORO_BUILD_ID" |
|
"KOKORO_JOB_NAME" |
|
"KOKORO_GIT_COMMIT" |
|
"KOKORO_GITHUB_COMMIT" |
|
"KOKORO_GITHUB_PULL_REQUEST_NUMBER" |
|
"KOKORO_GITHUB_PULL_REQUEST_COMMIT" |
|
|
|
"KOKORO_GITHUB_COMMIT_URL" |
|
"KOKORO_GITHUB_PULL_REQUEST_URL" |
|
) |
|
elif [[ "${TRAVIS:-}" == "true" ]]; then |
|
RUNNING_IN_CI="true" |
|
TRAMPOLINE_CI="travis" |
|
pass_down_envvars+=( |
|
"TRAVIS_BRANCH" |
|
"TRAVIS_BUILD_ID" |
|
"TRAVIS_BUILD_NUMBER" |
|
"TRAVIS_BUILD_WEB_URL" |
|
"TRAVIS_COMMIT" |
|
"TRAVIS_COMMIT_MESSAGE" |
|
"TRAVIS_COMMIT_RANGE" |
|
"TRAVIS_JOB_NAME" |
|
"TRAVIS_JOB_NUMBER" |
|
"TRAVIS_JOB_WEB_URL" |
|
"TRAVIS_PULL_REQUEST" |
|
"TRAVIS_PULL_REQUEST_BRANCH" |
|
"TRAVIS_PULL_REQUEST_SHA" |
|
"TRAVIS_PULL_REQUEST_SLUG" |
|
"TRAVIS_REPO_SLUG" |
|
"TRAVIS_SECURE_ENV_VARS" |
|
"TRAVIS_TAG" |
|
) |
|
elif [[ -n "${GITHUB_RUN_ID:-}" ]]; then |
|
RUNNING_IN_CI="true" |
|
TRAMPOLINE_CI="github-workflow" |
|
pass_down_envvars+=( |
|
"GITHUB_WORKFLOW" |
|
"GITHUB_RUN_ID" |
|
"GITHUB_RUN_NUMBER" |
|
"GITHUB_ACTION" |
|
"GITHUB_ACTIONS" |
|
"GITHUB_ACTOR" |
|
"GITHUB_REPOSITORY" |
|
"GITHUB_EVENT_NAME" |
|
"GITHUB_EVENT_PATH" |
|
"GITHUB_SHA" |
|
"GITHUB_REF" |
|
"GITHUB_HEAD_REF" |
|
"GITHUB_BASE_REF" |
|
) |
|
elif [[ "${CIRCLECI:-}" == "true" ]]; then |
|
RUNNING_IN_CI="true" |
|
TRAMPOLINE_CI="circleci" |
|
pass_down_envvars+=( |
|
"CIRCLE_BRANCH" |
|
"CIRCLE_BUILD_NUM" |
|
"CIRCLE_BUILD_URL" |
|
"CIRCLE_COMPARE_URL" |
|
"CIRCLE_JOB" |
|
"CIRCLE_NODE_INDEX" |
|
"CIRCLE_NODE_TOTAL" |
|
"CIRCLE_PREVIOUS_BUILD_NUM" |
|
"CIRCLE_PROJECT_REPONAME" |
|
"CIRCLE_PROJECT_USERNAME" |
|
"CIRCLE_REPOSITORY_URL" |
|
"CIRCLE_SHA1" |
|
"CIRCLE_STAGE" |
|
"CIRCLE_USERNAME" |
|
"CIRCLE_WORKFLOW_ID" |
|
"CIRCLE_WORKFLOW_JOB_ID" |
|
"CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS" |
|
"CIRCLE_WORKFLOW_WORKSPACE_ID" |
|
) |
|
fi |
|
|
|
|
|
function repo_root() { |
|
local dir="$1" |
|
while [[ ! -d "${dir}/.git" ]]; do |
|
dir="$(dirname "$dir")" |
|
done |
|
echo "${dir}" |
|
} |
|
|
|
|
|
|
|
|
|
if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then |
|
PROGRAM_PATH="$(realpath "$0")" |
|
PROGRAM_DIR="$(dirname "${PROGRAM_PATH}")" |
|
PROJECT_ROOT="$(repo_root "${PROGRAM_DIR}")" |
|
else |
|
PROJECT_ROOT="$(repo_root $(pwd))" |
|
fi |
|
|
|
log_yellow "Changing to the project root: ${PROJECT_ROOT}." |
|
cd "${PROJECT_ROOT}" |
|
|
|
|
|
|
|
if [[ -n "${TRAMPOLINE_SERVICE_ACCOUNT:-}" ]]; then |
|
|
|
mkdir -p "${tmpdir}/gcloud" |
|
gcloud_config_dir="${tmpdir}/gcloud" |
|
|
|
log_yellow "Using isolated gcloud config: ${gcloud_config_dir}." |
|
export CLOUDSDK_CONFIG="${gcloud_config_dir}" |
|
|
|
log_yellow "Using ${TRAMPOLINE_SERVICE_ACCOUNT} for authentication." |
|
gcloud auth activate-service-account \ |
|
--key-file "${TRAMPOLINE_SERVICE_ACCOUNT}" |
|
log_yellow "Configuring Container Registry access" |
|
gcloud auth configure-docker --quiet |
|
fi |
|
|
|
required_envvars=( |
|
|
|
"TRAMPOLINE_IMAGE" |
|
"TRAMPOLINE_BUILD_FILE" |
|
) |
|
|
|
if [[ -f "${PROJECT_ROOT}/.trampolinerc" ]]; then |
|
source "${PROJECT_ROOT}/.trampolinerc" |
|
fi |
|
|
|
log_yellow "Checking environment variables." |
|
for e in "${required_envvars[@]}" |
|
do |
|
if [[ -z "${!e:-}" ]]; then |
|
log "Missing ${e} env var. Aborting." |
|
exit 1 |
|
fi |
|
done |
|
|
|
|
|
|
|
TRAMPOLINE_BUILD_FILE="${TRAMPOLINE_BUILD_FILE#github/*/}" |
|
log_yellow "Using TRAMPOLINE_BUILD_FILE: ${TRAMPOLINE_BUILD_FILE}" |
|
|
|
|
|
set +e |
|
|
|
log_yellow "Preparing Docker image." |
|
|
|
if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then |
|
|
|
|
|
|
|
|
|
log_yellow "Start pulling the Docker image: ${TRAMPOLINE_IMAGE}." |
|
if docker pull "${TRAMPOLINE_IMAGE}"; then |
|
log_green "Finished pulling the Docker image: ${TRAMPOLINE_IMAGE}." |
|
has_image="true" |
|
else |
|
log_red "Failed pulling the Docker image: ${TRAMPOLINE_IMAGE}." |
|
has_image="false" |
|
fi |
|
else |
|
|
|
if docker images "${TRAMPOLINE_IMAGE}:latest" | grep "${TRAMPOLINE_IMAGE}"; then |
|
has_image="true" |
|
else |
|
has_image="false" |
|
fi |
|
fi |
|
|
|
|
|
|
|
|
|
|
|
user_uid="$(id -u)" |
|
user_gid="$(id -g)" |
|
user_name="$(id -un)" |
|
|
|
|
|
|
|
docker_gid=$(cut -d: -f3 < <(getent group docker)) |
|
|
|
update_cache="false" |
|
if [[ "${TRAMPOLINE_DOCKERFILE:-none}" != "none" ]]; then |
|
|
|
context_dir=$(dirname "${TRAMPOLINE_DOCKERFILE}") |
|
docker_build_flags=( |
|
"-f" "${TRAMPOLINE_DOCKERFILE}" |
|
"-t" "${TRAMPOLINE_IMAGE}" |
|
"--build-arg" "UID=${user_uid}" |
|
"--build-arg" "USERNAME=${user_name}" |
|
) |
|
if [[ "${has_image}" == "true" ]]; then |
|
docker_build_flags+=("--cache-from" "${TRAMPOLINE_IMAGE}") |
|
fi |
|
|
|
log_yellow "Start building the docker image." |
|
if [[ "${TRAMPOLINE_VERBOSE:-false}" == "true" ]]; then |
|
echo "docker build" "${docker_build_flags[@]}" "${context_dir}" |
|
fi |
|
|
|
|
|
|
|
if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then |
|
if docker build "${docker_build_flags[@]}" "${context_dir}" \ |
|
> "${tmpdir}/docker_build.log" 2>&1; then |
|
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then |
|
cat "${tmpdir}/docker_build.log" |
|
fi |
|
|
|
log_green "Finished building the docker image." |
|
update_cache="true" |
|
else |
|
log_red "Failed to build the Docker image, aborting." |
|
log_yellow "Dumping the build logs:" |
|
cat "${tmpdir}/docker_build.log" |
|
exit 1 |
|
fi |
|
else |
|
if docker build "${docker_build_flags[@]}" "${context_dir}"; then |
|
log_green "Finished building the docker image." |
|
update_cache="true" |
|
else |
|
log_red "Failed to build the Docker image, aborting." |
|
exit 1 |
|
fi |
|
fi |
|
else |
|
if [[ "${has_image}" != "true" ]]; then |
|
log_red "We do not have ${TRAMPOLINE_IMAGE} locally, aborting." |
|
exit 1 |
|
fi |
|
fi |
|
|
|
|
|
docker_flags=( |
|
|
|
"--rm" |
|
|
|
|
|
"--network=host" |
|
|
|
|
|
|
|
"--privileged" |
|
|
|
|
|
|
|
|
|
"--user" "${user_uid}:${docker_gid}" |
|
|
|
|
|
"--env" "USER=${user_name}" |
|
|
|
|
|
"--volume" "${PROJECT_ROOT}:${TRAMPOLINE_WORKSPACE}" |
|
"--workdir" "${TRAMPOLINE_WORKSPACE}" |
|
"--env" "PROJECT_ROOT=${TRAMPOLINE_WORKSPACE}" |
|
|
|
|
|
"--volume" "${tmphome}:/h" |
|
"--env" "HOME=/h" |
|
|
|
|
|
"--volume" "/var/run/docker.sock:/var/run/docker.sock" |
|
|
|
|
|
|
|
"--volume" "/tmp:/tmp" |
|
|
|
|
|
"--env" "TRAMPOLINE_SECRET_DIR=/secrets" |
|
"--volume" "${KOKORO_GFILE_DIR:-/dev/shm}:/secrets/gfile" |
|
"--env" "KOKORO_GFILE_DIR=/secrets/gfile" |
|
"--volume" "${KOKORO_KEYSTORE_DIR:-/dev/shm}:/secrets/keystore" |
|
"--env" "KOKORO_KEYSTORE_DIR=/secrets/keystore" |
|
) |
|
|
|
|
|
if [[ -t 0 ]]; then |
|
docker_flags+=("-it") |
|
fi |
|
|
|
|
|
for e in "${pass_down_envvars[@]}" |
|
do |
|
if [[ -n "${!e:-}" ]]; then |
|
docker_flags+=("--env" "${e}=${!e}") |
|
fi |
|
done |
|
|
|
|
|
|
|
if [[ $# -ge 1 ]]; then |
|
log_yellow "Running the given commands '" "${@:1}" "' in the container." |
|
readonly commands=("${@:1}") |
|
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then |
|
echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}" |
|
fi |
|
docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}" |
|
else |
|
log_yellow "Running the tests in a Docker container." |
|
docker_flags+=("--entrypoint=${TRAMPOLINE_BUILD_FILE}") |
|
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then |
|
echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" |
|
fi |
|
docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" |
|
fi |
|
|
|
|
|
test_retval=$? |
|
|
|
if [[ ${test_retval} -eq 0 ]]; then |
|
log_green "Build finished with ${test_retval}" |
|
else |
|
log_red "Build finished with ${test_retval}" |
|
fi |
|
|
|
|
|
if [[ "${update_cache}" == "true" ]] && \ |
|
[[ $test_retval == 0 ]] && \ |
|
[[ "${TRAMPOLINE_IMAGE_UPLOAD:-false}" == "true" ]]; then |
|
log_yellow "Uploading the Docker image." |
|
if docker push "${TRAMPOLINE_IMAGE}"; then |
|
log_green "Finished uploading the Docker image." |
|
else |
|
log_red "Failed uploading the Docker image." |
|
fi |
|
|
|
if function_exists trampoline_after_upload_hook; then |
|
trampoline_after_upload_hook |
|
fi |
|
|
|
fi |
|
|
|
exit "${test_retval}" |
|
|