Skip to content

Commit 8b9d718

Browse files
author
Takashi Matsuo
authored
chore: upgrade to trampoline 2.0.3 (GoogleCloudPlatform#4274)
1 parent 4a10ba2 commit 8b9d718

File tree

2 files changed

+153
-58
lines changed

2 files changed

+153
-58
lines changed

.kokoro/trampoline_v2.sh

Lines changed: 153 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@
4343
# TRAMPOLINE_BUILD_FILE: The script to run in the docker container.
4444
# TRAMPOLINE_WORKSPACE: The workspace path in the docker container.
4545
# Defaults to /workspace.
46-
# TRAMPOLINE_SKIP_DOWNLOAD_IMAGE: Skip downloading the image when you
47-
# know you have the image locally.
48-
#
4946
# Potentially there are some repo specific envvars in .trampolinerc in
5047
# the project root.
5148

5249

5350
set -euo pipefail
5451

52+
TRAMPOLINE_VERSION="2.0.3"
53+
5554
if command -v tput >/dev/null && [[ -n "${TERM:-}" ]]; then
5655
readonly IO_COLOR_RED="$(tput setaf 1)"
5756
readonly IO_COLOR_GREEN="$(tput setaf 2)"
@@ -64,6 +63,10 @@ else
6463
readonly IO_COLOR_RESET=""
6564
fi
6665

66+
function function_exists {
67+
[ $(LC_ALL=C type -t $1)"" == "function" ]
68+
}
69+
6770
# Logs a message using the given color. The first argument must be one
6871
# of the IO_COLOR_* variables defined above, such as
6972
# "${IO_COLOR_YELLOW}". The remaining arguments will be logged in the
@@ -108,24 +111,23 @@ function cleanup() {
108111
}
109112
trap cleanup EXIT
110113

111-
function repo_root() {
112-
local dir="$1"
113-
while [[ ! -d "${dir}/.git" ]]; do
114-
dir="$(dirname "$dir")"
115-
done
116-
echo "${dir}"
117-
}
118-
119-
PROGRAM_PATH="$(realpath "$0")"
120-
PROGRAM_DIR="$(dirname "${PROGRAM_PATH}")"
121-
PROJECT_ROOT="$(repo_root "${PROGRAM_DIR}")"
122-
123114
RUNNING_IN_CI="${RUNNING_IN_CI:-false}"
124-
TRAMPOLINE_VERSION="2.0.0"
125115

126116
# The workspace in the container, defaults to /workspace.
127117
TRAMPOLINE_WORKSPACE="${TRAMPOLINE_WORKSPACE:-/workspace}"
128118

119+
pass_down_envvars=(
120+
# TRAMPOLINE_V2 variables.
121+
# Tells scripts whether they are running as part of CI or not.
122+
"RUNNING_IN_CI"
123+
# Indicates which CI system we're in.
124+
"TRAMPOLINE_CI"
125+
# Indicates the version of the script.
126+
"TRAMPOLINE_VERSION"
127+
)
128+
129+
log_yellow "Building with Trampoline ${TRAMPOLINE_VERSION}"
130+
129131
# Detect which CI systems we're in. If we're in any of the CI systems
130132
# we support, `RUNNING_IN_CI` will be true and `TRAMPOLINE_CI` will be
131133
# the name of the CI system. Both envvars will be passing down to the
@@ -134,13 +136,96 @@ if [[ -n "${KOKORO_BUILD_ID:-}" ]]; then
134136
# descriptive env var for indicating it's on CI.
135137
RUNNING_IN_CI="true"
136138
TRAMPOLINE_CI="kokoro"
137-
# We should be able to use the default service account.
138-
log_yellow "Configuring Container Registry access"
139-
gcloud auth list
140-
gcloud auth configure-docker --quiet
139+
if [[ "${TRAMPOLINE_USE_LEGACY_SERVICE_ACCOUNT:-}" == "true" ]]; then
140+
if [[ ! -f "${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json" ]]; then
141+
log_red "${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json does not exist. Did you forget to mount cloud-devrel-kokoro-resources/trampoline? Aborting."
142+
exit 1
143+
fi
144+
# This service account will be activated later.
145+
TRAMPOLINE_SERVICE_ACCOUNT="${KOKORO_GFILE_DIR}/kokoro-trampoline.service-account.json"
146+
else
147+
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then
148+
gcloud auth list
149+
fi
150+
log_yellow "Configuring Container Registry access"
151+
gcloud auth configure-docker --quiet
152+
fi
153+
pass_down_envvars+=(
154+
# KOKORO dynamic variables.
155+
"KOKORO_BUILD_NUMBER"
156+
"KOKORO_BUILD_ID"
157+
"KOKORO_JOB_NAME"
158+
"KOKORO_GIT_COMMIT"
159+
"KOKORO_GITHUB_COMMIT"
160+
"KOKORO_GITHUB_PULL_REQUEST_NUMBER"
161+
"KOKORO_GITHUB_PULL_REQUEST_COMMIT"
162+
# For Build Cop Bot
163+
"KOKORO_GITHUB_COMMIT_URL"
164+
"KOKORO_GITHUB_PULL_REQUEST_URL"
165+
)
141166
elif [[ "${TRAVIS:-}" == "true" ]]; then
142167
RUNNING_IN_CI="true"
143168
TRAMPOLINE_CI="travis"
169+
pass_down_envvars+=(
170+
"TRAVIS_BRANCH"
171+
"TRAVIS_BUILD_ID"
172+
"TRAVIS_BUILD_NUMBER"
173+
"TRAVIS_BUILD_WEB_URL"
174+
"TRAVIS_COMMIT"
175+
"TRAVIS_COMMIT_MESSAGE"
176+
"TRAVIS_COMMIT_RANGE"
177+
"TRAVIS_JOB_NAME"
178+
"TRAVIS_JOB_NUMBER"
179+
"TRAVIS_JOB_WEB_URL"
180+
"TRAVIS_PULL_REQUEST"
181+
"TRAVIS_PULL_REQUEST_BRANCH"
182+
"TRAVIS_PULL_REQUEST_SHA"
183+
"TRAVIS_PULL_REQUEST_SLUG"
184+
"TRAVIS_REPO_SLUG"
185+
"TRAVIS_SECURE_ENV_VARS"
186+
"TRAVIS_TAG"
187+
)
188+
elif [[ -n "${GITHUB_RUN_ID:-}" ]]; then
189+
RUNNING_IN_CI="true"
190+
TRAMPOLINE_CI="github-workflow"
191+
pass_down_envvars+=(
192+
"GITHUB_WORKFLOW"
193+
"GITHUB_RUN_ID"
194+
"GITHUB_RUN_NUMBER"
195+
"GITHUB_ACTION"
196+
"GITHUB_ACTIONS"
197+
"GITHUB_ACTOR"
198+
"GITHUB_REPOSITORY"
199+
"GITHUB_EVENT_NAME"
200+
"GITHUB_EVENT_PATH"
201+
"GITHUB_SHA"
202+
"GITHUB_REF"
203+
"GITHUB_HEAD_REF"
204+
"GITHUB_BASE_REF"
205+
)
206+
elif [[ "${CIRCLECI:-}" == "true" ]]; then
207+
RUNNING_IN_CI="true"
208+
TRAMPOLINE_CI="circleci"
209+
pass_down_envvars+=(
210+
"CIRCLE_BRANCH"
211+
"CIRCLE_BUILD_NUM"
212+
"CIRCLE_BUILD_URL"
213+
"CIRCLE_COMPARE_URL"
214+
"CIRCLE_JOB"
215+
"CIRCLE_NODE_INDEX"
216+
"CIRCLE_NODE_TOTAL"
217+
"CIRCLE_PREVIOUS_BUILD_NUM"
218+
"CIRCLE_PROJECT_REPONAME"
219+
"CIRCLE_PROJECT_USERNAME"
220+
"CIRCLE_REPOSITORY_URL"
221+
"CIRCLE_SHA1"
222+
"CIRCLE_STAGE"
223+
"CIRCLE_USERNAME"
224+
"CIRCLE_WORKFLOW_ID"
225+
"CIRCLE_WORKFLOW_JOB_ID"
226+
"CIRCLE_WORKFLOW_UPSTREAM_JOB_IDS"
227+
"CIRCLE_WORKFLOW_WORKSPACE_ID"
228+
)
144229
fi
145230

146231
# Configure the service account for pulling the docker image.
@@ -159,6 +244,24 @@ if [[ -n "${TRAMPOLINE_SERVICE_ACCOUNT:-}" ]]; then
159244
gcloud auth configure-docker --quiet
160245
fi
161246

247+
function repo_root() {
248+
local dir="$1"
249+
while [[ ! -d "${dir}/.git" ]]; do
250+
dir="$(dirname "$dir")"
251+
done
252+
echo "${dir}"
253+
}
254+
255+
# Detect the project root. In CI builds, we assume the script is in
256+
# the git tree and traverse from there, otherwise, traverse from `pwd`
257+
# to find `.git` directory.
258+
if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then
259+
PROGRAM_PATH="$(realpath "$0")"
260+
PROGRAM_DIR="$(dirname "${PROGRAM_PATH}")"
261+
PROJECT_ROOT="$(repo_root "${PROGRAM_DIR}")"
262+
else
263+
PROJECT_ROOT="$(repo_root $(pwd))"
264+
fi
162265

163266
log_yellow "Changing to the project root: ${PROJECT_ROOT}."
164267
cd "${PROJECT_ROOT}"
@@ -169,27 +272,6 @@ required_envvars=(
169272
"TRAMPOLINE_BUILD_FILE"
170273
)
171274

172-
pass_down_envvars=(
173-
# TRAMPOLINE_V2 variables.
174-
# Tells scripts whether they are running as part of CI or not.
175-
"RUNNING_IN_CI"
176-
# Indicates which CI system we're in.
177-
"TRAMPOLINE_CI"
178-
# Indicates the version of the script.
179-
"TRAMPOLINE_VERSION"
180-
# KOKORO dynamic variables.
181-
"KOKORO_BUILD_NUMBER"
182-
"KOKORO_BUILD_ID"
183-
"KOKORO_JOB_NAME"
184-
"KOKORO_GIT_COMMIT"
185-
"KOKORO_GITHUB_COMMIT"
186-
"KOKORO_GITHUB_PULL_REQUEST_NUMBER"
187-
"KOKORO_GITHUB_PULL_REQUEST_COMMIT"
188-
# For Build Cop Bot
189-
"KOKORO_GITHUB_COMMIT_URL"
190-
"KOKORO_GITHUB_PULL_REQUEST_URL"
191-
)
192-
193275
if [[ -f "${PROJECT_ROOT}/.trampolinerc" ]]; then
194276
source "${PROJECT_ROOT}/.trampolinerc"
195277
fi
@@ -203,23 +285,30 @@ do
203285
fi
204286
done
205287

206-
if [[ "${TRAMPOLINE_SKIP_DOWNLOAD_IMAGE:-false}" == "true" ]]; then
207-
log_yellow "Re-using the local Docker image."
208-
has_cache="true"
209-
else
210-
log_yellow "Preparing Docker image."
288+
# ignore error on docker operations and test execution
289+
set +e
290+
291+
log_yellow "Preparing Docker image."
292+
# We only download the docker image in CI builds.
293+
if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then
211294
# Download the docker image specified by `TRAMPOLINE_IMAGE`
212295

213-
set +e # ignore error on docker operations
214296
# We may want to add --max-concurrent-downloads flag.
215297

216298
log_yellow "Start pulling the Docker image: ${TRAMPOLINE_IMAGE}."
217299
if docker pull "${TRAMPOLINE_IMAGE}"; then
218300
log_green "Finished pulling the Docker image: ${TRAMPOLINE_IMAGE}."
219-
has_cache="true"
301+
has_image="true"
220302
else
221303
log_red "Failed pulling the Docker image: ${TRAMPOLINE_IMAGE}."
222-
has_cache="false"
304+
has_image="false"
305+
fi
306+
else
307+
# For local run, check if we have the image.
308+
if docker images "${TRAMPOLINE_IMAGE}:latest" | grep "${TRAMPOLINE_IMAGE}"; then
309+
has_image="true"
310+
else
311+
has_image="false"
223312
fi
224313
fi
225314

@@ -245,12 +334,12 @@ if [[ "${TRAMPOLINE_DOCKERFILE:-none}" != "none" ]]; then
245334
"--build-arg" "UID=${user_uid}"
246335
"--build-arg" "USERNAME=${user_name}"
247336
)
248-
if [[ "${has_cache}" == "true" ]]; then
337+
if [[ "${has_image}" == "true" ]]; then
249338
docker_build_flags+=("--cache-from" "${TRAMPOLINE_IMAGE}")
250339
fi
251340

252341
log_yellow "Start building the docker image."
253-
if [[ "${TRAMPOLINE_SHOW_COMMAND:-false}" == "true" ]]; then
342+
if [[ "${TRAMPOLINE_VERBOSE:-false}" == "true" ]]; then
254343
echo "docker build" "${docker_build_flags[@]}" "${context_dir}"
255344
fi
256345

@@ -259,6 +348,10 @@ if [[ "${TRAMPOLINE_DOCKERFILE:-none}" != "none" ]]; then
259348
if [[ "${RUNNING_IN_CI:-}" == "true" ]]; then
260349
if docker build "${docker_build_flags[@]}" "${context_dir}" \
261350
> "${tmpdir}/docker_build.log" 2>&1; then
351+
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then
352+
cat "${tmpdir}/docker_build.log"
353+
fi
354+
262355
log_green "Finished building the docker image."
263356
update_cache="true"
264357
else
@@ -277,8 +370,8 @@ if [[ "${TRAMPOLINE_DOCKERFILE:-none}" != "none" ]]; then
277370
fi
278371
fi
279372
else
280-
if [[ "${has_cache}" != "true" ]]; then
281-
log_red "failed to download the image ${TRAMPOLINE_IMAGE}, aborting."
373+
if [[ "${has_image}" != "true" ]]; then
374+
log_red "We do not have ${TRAMPOLINE_IMAGE} locally, aborting."
282375
exit 1
283376
fi
284377
fi
@@ -345,14 +438,14 @@ done
345438
if [[ $# -ge 1 ]]; then
346439
log_yellow "Running the given commands '" "${@:1}" "' in the container."
347440
readonly commands=("${@:1}")
348-
if [[ "${TRAMPOLINE_SHOW_COMMAND:-false}" == "true" ]]; then
441+
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then
349442
echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}"
350443
fi
351444
docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}" "${commands[@]}"
352445
else
353446
log_yellow "Running the tests in a Docker container."
354447
docker_flags+=("--entrypoint=${TRAMPOLINE_BUILD_FILE}")
355-
if [[ "${TRAMPOLINE_SHOW_COMMAND:-false}" == "true" ]]; then
448+
if [[ "${TRAMPOLINE_VERBOSE:-}" == "true" ]]; then
356449
echo docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}"
357450
fi
358451
docker run "${docker_flags[@]}" "${TRAMPOLINE_IMAGE}"
@@ -377,6 +470,11 @@ if [[ "${update_cache}" == "true" ]] && \
377470
else
378471
log_red "Failed uploading the Docker image."
379472
fi
473+
# Call trampoline_after_upload_hook if it's defined.
474+
if function_exists trampoline_after_upload_hook; then
475+
trampoline_after_upload_hook
476+
fi
477+
380478
fi
381479

382480
exit "${test_retval}"

scripts/run_tests_local.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,4 @@ for session in "${sessions[@]}"
7070
do
7171
export RUN_TESTS_SESSION="${session}"
7272
"${PROJECT_ROOT}/.kokoro/trampoline_v2.sh"
73-
# We can re-use the image after the first iteration.
74-
export TRAMPOLINE_SKIP_DOWNLOAD_IMAGE="true"
75-
export TRAMPOLINE_DOCKERFILE="none"
7673
done

0 commit comments

Comments
 (0)