From e46325285879dd09e255af7c651af0f746c6a856 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Mon, 30 Nov 2026 22:47:28 +0000 Subject: [PATCH 1/3] fix(ci): ensure build.sh runs coerce_logs, flakybot, and teardown on failure Fix latent set -eo pipefail early-exit bug in .kokoro/build.sh and .kokoro/common.sh where failing mvn invocations exited before RETURN_CODE=$?, coerce_logs.sh, or flakybot could run, while composing cleanly with setup_cloud() teardown. --- .kokoro/build.sh | 89 ++++++++++++++++++++++---------- .kokoro/common.sh | 38 +++++++++----- .kokoro/common_test.sh | 113 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 41 deletions(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index fe7d5e767d6f..f6d84b98d172 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -35,6 +35,46 @@ fi RETURN_CODE=0 +# Ensure post-build log coercion, flakybot reporting, and setup_cloud() +# teardown always run on both passing and failing builds (including set -e +# exits), while preserving the build's exact exit code. +FINALIZE_RAN="false" +SETUP_CLOUD_DEFER_TRAP="true" +function finalize_build() { + local exit_code=$? + if [[ "${FINALIZE_RAN}" == "true" ]]; then + return "${exit_code}" + fi + FINALIZE_RAN="true" + + if [[ "${exit_code}" -eq 0 && "${RETURN_CODE:-0}" -ne 0 ]]; then + exit_code="${RETURN_CODE}" + fi + + # Prevent a post-build reporting/teardown error from skipping subsequent + # steps or masking exit_code. + set +e + + cd "${scriptDir}/.." || true + + # fix output location of logs + bash .kokoro/coerce_logs.sh || echo "Warning: coerce_logs.sh failed with exit code $?" >&2 + + if [[ "${ENABLE_FLAKYBOT}" == "true" ]]; then + chmod +x "${KOKORO_GFILE_DIR}/linux_amd64/flakybot" && + "${KOKORO_GFILE_DIR}/linux_amd64/flakybot" -repo=googleapis/google-cloud-java || + echo "Warning: flakybot failed with exit code $?" >&2 + fi + + if [[ "${SETUP_CLOUD_DESTROY_REGISTERED:-false}" == "true" ]]; then + destroy_cloud || echo "Warning: destroy_cloud failed with exit code $?" >&2 + fi + + echo "exiting with ${exit_code}" + exit "${exit_code}" +} +trap finalize_build EXIT + case ${JOB_TYPE} in test) MAVEN_GOAL="test" @@ -70,8 +110,7 @@ case ${JOB_TYPE} in -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS \ -Dmaven.wagon.http.retryHandler.count=5 \ "${EXCLUDE_PROJECTS_OPTS[@]}" \ - -T 1C - RETURN_CODE=$? + -T 1C || RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then @@ -97,7 +136,7 @@ case ${JOB_TYPE} in -Dmaven.wagon.http.retryHandler.count=5 \ -DskipTests=true \ --also-make \ - -T 1C + -T 1C || RETURN_CODE=$? fi ;; integration) @@ -162,9 +201,7 @@ case ${JOB_TYPE} in -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS \ -DskipUnitTests=true \ -Dmaven.wagon.http.retryHandler.count=5 \ - ${SUREFIRE_JVM_OPT} - - RETURN_CODE=$? + ${SUREFIRE_JVM_OPT} || RETURN_CODE=$? popd printf "Finished integration tests for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -223,9 +260,7 @@ case ${JOB_TYPE} in -B -ntp -fae \ -DtrimStackTrace=false \ -Dorg.slf4j.simpleLogger.showDateTime=true \ - -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS - - RETURN_CODE=$? + -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS || RETURN_CODE=$? popd printf "Finished GraalVM ITs for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -328,12 +363,14 @@ case ${JOB_TYPE} in -T 1C \ ${MODULE_FILTER} \ -Dlint \ - com.spotify.fmt:fmt-maven-plugin:check - mvn -B -ntp \ - -T 1C \ - ${MODULE_FILTER} \ - -Dlint \ - checkstyle:check@checkstyle + com.spotify.fmt:fmt-maven-plugin:check || RETURN_CODE=$? + if [[ "${RETURN_CODE}" -eq 0 ]]; then + mvn -B -ntp \ + -T 1C \ + ${MODULE_FILTER} \ + -Dlint \ + checkstyle:check@checkstyle || RETURN_CODE=$? + fi if [[ -n "${BUILD_SUBDIR}" ]] then @@ -356,8 +393,7 @@ case ${JOB_TYPE} in -Dfmt.skip=true \ -Denforcer.skip=true \ -Dcheckstyle.skip=true \ - clirr:check - RETURN_CODE=$? + clirr:check || RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then echo "restoring directory" @@ -369,16 +405,13 @@ case ${JOB_TYPE} in esac if [ "${REPORT_COVERAGE}" == "true" ]; then - bash ${KOKORO_GFILE_DIR}/codecov.sh -fi - -# fix output location of logs -bash .kokoro/coerce_logs.sh - -if [[ "${ENABLE_FLAKYBOT}" == "true" ]]; then - chmod +x ${KOKORO_GFILE_DIR}/linux_amd64/flakybot - ${KOKORO_GFILE_DIR}/linux_amd64/flakybot -repo=googleapis/google-cloud-java + codecov_exit=0 + bash "${KOKORO_GFILE_DIR}/codecov.sh" || codecov_exit=$? + if [[ "${RETURN_CODE}" -eq 0 && "${codecov_exit}" -ne 0 ]]; then + RETURN_CODE="${codecov_exit}" + fi fi -echo "exiting with ${RETURN_CODE}" -exit ${RETURN_CODE} \ No newline at end of file +# Post-build log coercion, flakybot reporting, and setup_cloud() teardown +# execute via the EXIT trap (finalize_build). +exit "${RETURN_CODE}" \ No newline at end of file diff --git a/.kokoro/common.sh b/.kokoro/common.sh index ab67171d1137..2d9a12f452d7 100644 --- a/.kokoro/common.sh +++ b/.kokoro/common.sh @@ -195,6 +195,18 @@ function setup_application_credentials() { fi } +function destroy_cloud() { + echo "Exiting via destroy()" + + ## Get the directory of the build script + local destroyScriptDir + destroyScriptDir=$(realpath "$(dirname "${BASH_SOURCE[0]}")") + ## cd to the parent directory, i.e. the root of the git repo + cd "${destroyScriptDir}/.." + + time source ./.cloud/helpers/destroy.sh +} + function setup_cloud() { gcloud config set project "$GOOGLE_CLOUD_PROJECT" @@ -206,17 +218,17 @@ function setup_cloud() { destroy() { arguments=$? - echo "Exiting via destroy()" - - ## Get the directory of the build script - scriptDir=$(realpath $(dirname "${BASH_SOURCE[0]}")) - ## cd to the parent directory, i.e. the root of the git repo - cd ${scriptDir}/.. - - time source ./.cloud/helpers/destroy.sh + destroy_cloud exit $arguments } - trap destroy EXIT + # When a caller (such as .kokoro/build.sh) manages its own EXIT trap and sets + # SETUP_CLOUD_DEFER_TRAP=true, record that destroy_cloud must run instead of + # overwriting the caller's EXIT trap. + if [[ "${SETUP_CLOUD_DEFER_TRAP:-false}" == "true" ]]; then + SETUP_CLOUD_DESTROY_REGISTERED="true" + else + trap destroy EXIT + fi } # Prints "true" if this pull pull request is made by Release Please @@ -448,6 +460,7 @@ function run_integration_tests() { parse_all_submodules "$1" printf "Running integration tests for submodules:\n%s\n" "$all_submodules" + RETURN_CODE=0 mvn verify -Penable-integration-tests -Pquick-build --projects "$all_submodules" \ ${INTEGRATION_TEST_ARGS} \ -B -ntp -fae \ @@ -458,9 +471,8 @@ function run_integration_tests() { -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS \ -DskipUnitTests=true \ -Dmaven.wagon.http.retryHandler.count=5 \ - -T 1C + -T 1C || RETURN_CODE=$? - RETURN_CODE=$? printf "Finished integration tests for modules:\n%s\n" "$1" } @@ -469,14 +481,14 @@ function run_graalvm_tests() { parse_all_submodules "$1" printf "Running GraalVM ITs for submodules:\n%s\n" "$all_submodules" + RETURN_CODE=0 mvn test -Pnative -Pquick-build --projects "$all_submodules" \ ${INTEGRATION_TEST_ARGS} \ -B -ntp -fae \ -DtrimStackTrace=false \ -Dorg.slf4j.simpleLogger.showDateTime=true \ - -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS + -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS || RETURN_CODE=$? - RETURN_CODE=$? printf "Finished GraalVM ITs for modules:\n%s\n" "$1" } diff --git a/.kokoro/common_test.sh b/.kokoro/common_test.sh index 6ad90bbc9830..43769410c38e 100755 --- a/.kokoro/common_test.sh +++ b/.kokoro/common_test.sh @@ -461,6 +461,118 @@ function test_graalvm_nightly_modules { popd >/dev/null } +# Test that .kokoro/build.sh runs coerce_logs.sh, flakybot, codecov.sh, and +# setup_cloud() teardown even when mvn exits non-zero under set -eo pipefail, +# while preserving the exact exit code and standalone setup_cloud() behavior. +function test_build_sh_failure_exit_path { + local tmp_repo exit_code + tmp_repo=$(mktemp -d) + mkdir -p \ + "${tmp_repo}/.kokoro" \ + "${tmp_repo}/.cloud/helpers" \ + "${tmp_repo}/mod-a/src/test/java" \ + "${tmp_repo}/mock-bin" \ + "${tmp_repo}/fake-home" \ + "${tmp_repo}/gfile/linux_amd64" + + cp "${scriptDir}/build.sh" "${scriptDir}/common.sh" "${scriptDir}/coerce_logs.sh" "${tmp_repo}/.kokoro/" + echo "" > "${tmp_repo}/settings.xml" + printf '\n \n mod-a\n \n\n' > "${tmp_repo}/pom.xml" + printf '\n mod-a\n\n' > "${tmp_repo}/mod-a/pom.xml" + touch "${tmp_repo}/mod-a/src/test/java/MyIT.java" + + cat > "${tmp_repo}/mock-bin/git" <<'EOF' +#!/bin/bash +[[ "$1" == "diff" && "$2" == "--name-only" ]] && echo "mod-a/src/test/java/MyIT.java" +exit 0 +EOF + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/sleep" + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/gcloud" + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/terraform" + cat > "${tmp_repo}/mock-bin/mvn" <<'EOF' +#!/bin/bash +if [[ "$1" == "verify" ]]; then + mkdir -p "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports" + touch "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/TEST-MyIT-sponge_log.xml" \ + "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/MyIT-sponge_log.txt" + exit "${MOCK_MVN_VERIFY_EXIT:-0}" +fi +exit "${MOCK_MVN_INSTALL_EXIT:-0}" +EOF + chmod +x "${tmp_repo}/mock-bin/"* + + local helper + for helper in init plan apply populate-env; do + echo ":" > "${tmp_repo}/.cloud/helpers/${helper}.sh" + done + echo "touch \"${tmp_repo}/destroy.ran\"" > "${tmp_repo}/.cloud/helpers/destroy.sh" + echo "touch \"${tmp_repo}/codecov.ran\"" > "${tmp_repo}/gfile/codecov.sh" + cat > "${tmp_repo}/gfile/linux_amd64/flakybot" </dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 1 ]] || \ + [[ ! -f "${tmp_repo}/codecov.ran" ]] || \ + [[ ! -f "${tmp_repo}/flakybot.ran" ]] || \ + [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]]; then + echo "build.sh failing run did not preserve exit code or run post-build hooks (exit=${exit_code})" + rm -rf "${tmp_repo}" + exit 1 + fi + + # 2. JOB_TYPE=integration with setup_cloud() runs both coerce_logs and destroy.sh on failure. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/destroy.ran" + exit_code=0 + ( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT \ + ENABLE_FLAKYBOT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export GOOGLE_CLOUD_PROJECT="test-project" JOB_TYPE="integration" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="1" + bash "${tmp_repo}/.kokoro/build.sh" + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 1 ]] || \ + [[ ! -f "${tmp_repo}/destroy.ran" ]] || \ + [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]]; then + echo "build.sh integration + setup_cloud failure missed destroy.sh or coerce_logs (exit=${exit_code})" + rm -rf "${tmp_repo}" + exit 1 + fi + + # 3. Standalone setup_cloud() without SETUP_CLOUD_DEFER_TRAP still installs EXIT trap. + rm -f "${tmp_repo}/destroy.ran" + exit_code=0 + ( + unset SETUP_CLOUD_DEFER_TRAP SETUP_CLOUD_DESTROY_REGISTERED + export PATH="${tmp_repo}/mock-bin:${PATH}" GOOGLE_CLOUD_PROJECT="test-project" + cd "${tmp_repo}" && source "${tmp_repo}/.kokoro/common.sh" && setup_cloud "mod-a" && exit 3 + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 3 ]] || [[ ! -f "${tmp_repo}/destroy.ran" ]]; then + echo "standalone setup_cloud() failed to run destroy.sh or preserve exit code (exit=${exit_code})" + rm -rf "${tmp_repo}" + exit 1 + fi + rm -rf "${tmp_repo}" +} + test_find_all_poms_with_versioned_dependency test_update_pom_dependency test_parse_pom_version @@ -472,3 +584,4 @@ test_is_module_modified test_is_upstream_module_modified test_generate_modified_modules_list test_graalvm_nightly_modules +test_build_sh_failure_exit_path From 0e76ada44bd68b86863caeb65db0f393e0e070d4 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Tue, 1 Dec 2026 18:53:45 +0000 Subject: [PATCH 2/3] fix(ci): rely on set -e + EXIT trap instead of || RETURN_CODE=$? --- .kokoro/build.sh | 60 +++++++++++++++++++++++------------------------ .kokoro/common.sh | 8 +++---- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index f6d84b98d172..942da6ac6ab3 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -35,9 +35,9 @@ fi RETURN_CODE=0 -# Ensure post-build log coercion, flakybot reporting, and setup_cloud() -# teardown always run on both passing and failing builds (including set -e -# exits), while preserving the build's exact exit code. +# Ensure post-build coverage reporting, log coercion, flakybot reporting, and +# setup_cloud() teardown always run on both passing and failing builds (relying +# on set -e for fail-fast behavior), while preserving the exact exit code. FINALIZE_RAN="false" SETUP_CLOUD_DEFER_TRAP="true" function finalize_build() { @@ -47,16 +47,20 @@ function finalize_build() { fi FINALIZE_RAN="true" - if [[ "${exit_code}" -eq 0 && "${RETURN_CODE:-0}" -ne 0 ]]; then - exit_code="${RETURN_CODE}" - fi - # Prevent a post-build reporting/teardown error from skipping subsequent # steps or masking exit_code. set +e cd "${scriptDir}/.." || true + if [[ "${REPORT_COVERAGE}" == "true" ]]; then + local codecov_exit=0 + bash "${KOKORO_GFILE_DIR}/codecov.sh" || codecov_exit=$? + if [[ "${exit_code}" -eq 0 && "${codecov_exit}" -ne 0 ]]; then + exit_code="${codecov_exit}" + fi + fi + # fix output location of logs bash .kokoro/coerce_logs.sh || echo "Warning: coerce_logs.sh failed with exit code $?" >&2 @@ -110,7 +114,8 @@ case ${JOB_TYPE} in -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS \ -Dmaven.wagon.http.retryHandler.count=5 \ "${EXCLUDE_PROJECTS_OPTS[@]}" \ - -T 1C || RETURN_CODE=$? + -T 1C + RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then @@ -136,7 +141,7 @@ case ${JOB_TYPE} in -Dmaven.wagon.http.retryHandler.count=5 \ -DskipTests=true \ --also-make \ - -T 1C || RETURN_CODE=$? + -T 1C fi ;; integration) @@ -201,7 +206,9 @@ case ${JOB_TYPE} in -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS \ -DskipUnitTests=true \ -Dmaven.wagon.http.retryHandler.count=5 \ - ${SUREFIRE_JVM_OPT} || RETURN_CODE=$? + ${SUREFIRE_JVM_OPT} + + RETURN_CODE=$? popd printf "Finished integration tests for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -260,7 +267,9 @@ case ${JOB_TYPE} in -B -ntp -fae \ -DtrimStackTrace=false \ -Dorg.slf4j.simpleLogger.showDateTime=true \ - -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS || RETURN_CODE=$? + -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS + + RETURN_CODE=$? popd printf "Finished GraalVM ITs for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -363,14 +372,12 @@ case ${JOB_TYPE} in -T 1C \ ${MODULE_FILTER} \ -Dlint \ - com.spotify.fmt:fmt-maven-plugin:check || RETURN_CODE=$? - if [[ "${RETURN_CODE}" -eq 0 ]]; then - mvn -B -ntp \ - -T 1C \ - ${MODULE_FILTER} \ - -Dlint \ - checkstyle:check@checkstyle || RETURN_CODE=$? - fi + com.spotify.fmt:fmt-maven-plugin:check + mvn -B -ntp \ + -T 1C \ + ${MODULE_FILTER} \ + -Dlint \ + checkstyle:check@checkstyle if [[ -n "${BUILD_SUBDIR}" ]] then @@ -393,7 +400,8 @@ case ${JOB_TYPE} in -Dfmt.skip=true \ -Denforcer.skip=true \ -Dcheckstyle.skip=true \ - clirr:check || RETURN_CODE=$? + clirr:check + RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then echo "restoring directory" @@ -404,14 +412,6 @@ case ${JOB_TYPE} in esac -if [ "${REPORT_COVERAGE}" == "true" ]; then - codecov_exit=0 - bash "${KOKORO_GFILE_DIR}/codecov.sh" || codecov_exit=$? - if [[ "${RETURN_CODE}" -eq 0 && "${codecov_exit}" -ne 0 ]]; then - RETURN_CODE="${codecov_exit}" - fi -fi - -# Post-build log coercion, flakybot reporting, and setup_cloud() teardown -# execute via the EXIT trap (finalize_build). +# Post-build coverage reporting, log coercion, flakybot reporting, and +# setup_cloud() teardown execute via the EXIT trap (finalize_build). exit "${RETURN_CODE}" \ No newline at end of file diff --git a/.kokoro/common.sh b/.kokoro/common.sh index 2d9a12f452d7..1decbec36c93 100644 --- a/.kokoro/common.sh +++ b/.kokoro/common.sh @@ -460,7 +460,6 @@ function run_integration_tests() { parse_all_submodules "$1" printf "Running integration tests for submodules:\n%s\n" "$all_submodules" - RETURN_CODE=0 mvn verify -Penable-integration-tests -Pquick-build --projects "$all_submodules" \ ${INTEGRATION_TEST_ARGS} \ -B -ntp -fae \ @@ -471,8 +470,9 @@ function run_integration_tests() { -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS \ -DskipUnitTests=true \ -Dmaven.wagon.http.retryHandler.count=5 \ - -T 1C || RETURN_CODE=$? + -T 1C + RETURN_CODE=$? printf "Finished integration tests for modules:\n%s\n" "$1" } @@ -481,14 +481,14 @@ function run_graalvm_tests() { parse_all_submodules "$1" printf "Running GraalVM ITs for submodules:\n%s\n" "$all_submodules" - RETURN_CODE=0 mvn test -Pnative -Pquick-build --projects "$all_submodules" \ ${INTEGRATION_TEST_ARGS} \ -B -ntp -fae \ -DtrimStackTrace=false \ -Dorg.slf4j.simpleLogger.showDateTime=true \ - -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS || RETURN_CODE=$? + -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS + RETURN_CODE=$? printf "Finished GraalVM ITs for modules:\n%s\n" "$1" } From 3817898bbbbc85e5a96cabf1d7276a2229664b59 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Tue, 1 Dec 2026 19:16:13 +0000 Subject: [PATCH 3/3] fix(ci): isolate destroy_cloud subshell, gate codecov on success, remove dead RETURN_CODE --- .kokoro/build.sh | 38 +++--- .kokoro/common.sh | 5 +- .kokoro/common_test.sh | 269 +++++++++++++++++++++++++++-------------- 3 files changed, 195 insertions(+), 117 deletions(-) diff --git a/.kokoro/build.sh b/.kokoro/build.sh index 942da6ac6ab3..56d5c7054cdd 100755 --- a/.kokoro/build.sh +++ b/.kokoro/build.sh @@ -33,19 +33,13 @@ if [ -f "${KOKORO_GFILE_DIR}/secret_manager/java-bigqueryconnection-samples-secr source "${KOKORO_GFILE_DIR}/secret_manager/java-bigqueryconnection-samples-secrets" fi -RETURN_CODE=0 - # Ensure post-build coverage reporting, log coercion, flakybot reporting, and # setup_cloud() teardown always run on both passing and failing builds (relying # on set -e for fail-fast behavior), while preserving the exact exit code. -FINALIZE_RAN="false" SETUP_CLOUD_DEFER_TRAP="true" +SETUP_CLOUD_DESTROY_REGISTERED="false" function finalize_build() { local exit_code=$? - if [[ "${FINALIZE_RAN}" == "true" ]]; then - return "${exit_code}" - fi - FINALIZE_RAN="true" # Prevent a post-build reporting/teardown error from skipping subsequent # steps or masking exit_code. @@ -53,12 +47,9 @@ function finalize_build() { cd "${scriptDir}/.." || true - if [[ "${REPORT_COVERAGE}" == "true" ]]; then - local codecov_exit=0 - bash "${KOKORO_GFILE_DIR}/codecov.sh" || codecov_exit=$? - if [[ "${exit_code}" -eq 0 && "${codecov_exit}" -ne 0 ]]; then - exit_code="${codecov_exit}" - fi + if [[ "${exit_code}" -eq 0 && "${REPORT_COVERAGE}" == "true" ]]; then + bash "${KOKORO_GFILE_DIR}/codecov.sh" + exit_code=$? fi # fix output location of logs @@ -70,8 +61,19 @@ function finalize_build() { echo "Warning: flakybot failed with exit code $?" >&2 fi - if [[ "${SETUP_CLOUD_DESTROY_REGISTERED:-false}" == "true" ]]; then - destroy_cloud || echo "Warning: destroy_cloud failed with exit code $?" >&2 + if [[ "${SETUP_CLOUD_DESTROY_REGISTERED}" == "true" ]]; then + # Run in a subshell outside || so bash does not suppress set -e inside + # .cloud/helpers/destroy.sh, and so destroy.sh's set -eo pipefail or + # explicit exit calls cannot leak into finalize_build. + local destroy_exit=0 + ( set -eo pipefail; destroy_cloud ) + destroy_exit=$? + if [[ "${destroy_exit}" -ne 0 ]]; then + echo "Warning: destroy_cloud failed with exit code ${destroy_exit}" >&2 + if [[ "${exit_code}" -eq 0 ]]; then + exit_code="${destroy_exit}" + fi + fi fi echo "exiting with ${exit_code}" @@ -115,7 +117,6 @@ case ${JOB_TYPE} in -Dmaven.wagon.http.retryHandler.count=5 \ "${EXCLUDE_PROJECTS_OPTS[@]}" \ -T 1C - RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then @@ -208,7 +209,6 @@ case ${JOB_TYPE} in -Dmaven.wagon.http.retryHandler.count=5 \ ${SUREFIRE_JVM_OPT} - RETURN_CODE=$? popd printf "Finished integration tests for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -269,7 +269,6 @@ case ${JOB_TYPE} in -Dorg.slf4j.simpleLogger.showDateTime=true \ -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS - RETURN_CODE=$? popd printf "Finished GraalVM ITs for modules:\n%s\n" "${BUILD_SUBDIR}" fi @@ -401,7 +400,6 @@ case ${JOB_TYPE} in -Denforcer.skip=true \ -Dcheckstyle.skip=true \ clirr:check - RETURN_CODE=$? if [[ -n "${BUILD_SUBDIR}" ]] then echo "restoring directory" @@ -414,4 +412,4 @@ esac # Post-build coverage reporting, log coercion, flakybot reporting, and # setup_cloud() teardown execute via the EXIT trap (finalize_build). -exit "${RETURN_CODE}" \ No newline at end of file +exit 0 \ No newline at end of file diff --git a/.kokoro/common.sh b/.kokoro/common.sh index 1decbec36c93..b57f54ee0507 100644 --- a/.kokoro/common.sh +++ b/.kokoro/common.sh @@ -198,11 +198,8 @@ function setup_application_credentials() { function destroy_cloud() { echo "Exiting via destroy()" - ## Get the directory of the build script - local destroyScriptDir - destroyScriptDir=$(realpath "$(dirname "${BASH_SOURCE[0]}")") ## cd to the parent directory, i.e. the root of the git repo - cd "${destroyScriptDir}/.." + cd "${commonScriptDir}/.." || return time source ./.cloud/helpers/destroy.sh } diff --git a/.kokoro/common_test.sh b/.kokoro/common_test.sh index 43769410c38e..4f803ae96577 100755 --- a/.kokoro/common_test.sh +++ b/.kokoro/common_test.sh @@ -461,116 +461,199 @@ function test_graalvm_nightly_modules { popd >/dev/null } -# Test that .kokoro/build.sh runs coerce_logs.sh, flakybot, codecov.sh, and -# setup_cloud() teardown even when mvn exits non-zero under set -eo pipefail, -# while preserving the exact exit code and standalone setup_cloud() behavior. +# Test that .kokoro/build.sh runs coerce_logs.sh, flakybot, codecov.sh (on +# passing builds only), and setup_cloud() teardown under set -eo pipefail, +# preserves exact exit codes even when hooks fail, does not swallow failing +# destroy.sh (set -eo pipefail + trailing popd), and preserves standalone +# setup_cloud() behavior. function test_build_sh_failure_exit_path { - local tmp_repo exit_code - tmp_repo=$(mktemp -d) - mkdir -p \ - "${tmp_repo}/.kokoro" \ - "${tmp_repo}/.cloud/helpers" \ - "${tmp_repo}/mod-a/src/test/java" \ - "${tmp_repo}/mock-bin" \ - "${tmp_repo}/fake-home" \ - "${tmp_repo}/gfile/linux_amd64" - - cp "${scriptDir}/build.sh" "${scriptDir}/common.sh" "${scriptDir}/coerce_logs.sh" "${tmp_repo}/.kokoro/" - echo "" > "${tmp_repo}/settings.xml" - printf '\n \n mod-a\n \n\n' > "${tmp_repo}/pom.xml" - printf '\n mod-a\n\n' > "${tmp_repo}/mod-a/pom.xml" - touch "${tmp_repo}/mod-a/src/test/java/MyIT.java" - - cat > "${tmp_repo}/mock-bin/git" <<'EOF' + ( + local tmp_repo exit_code output + tmp_repo=$(mktemp -d) + trap 'rm -rf "${tmp_repo}"' EXIT + + mkdir -p \ + "${tmp_repo}/.kokoro" \ + "${tmp_repo}/.cloud/helpers" \ + "${tmp_repo}/mod-a/src/test/java" \ + "${tmp_repo}/mock-bin" \ + "${tmp_repo}/fake-home" \ + "${tmp_repo}/gfile/linux_amd64" + + cp "${scriptDir}/build.sh" "${scriptDir}/common.sh" "${scriptDir}/coerce_logs.sh" "${tmp_repo}/.kokoro/" + echo "" > "${tmp_repo}/settings.xml" + printf '\n \n mod-a\n \n\n' > "${tmp_repo}/pom.xml" + printf '\n mod-a\n\n' > "${tmp_repo}/mod-a/pom.xml" + touch "${tmp_repo}/mod-a/src/test/java/MyIT.java" + + cat > "${tmp_repo}/mock-bin/git" <<'EOF' #!/bin/bash [[ "$1" == "diff" && "$2" == "--name-only" ]] && echo "mod-a/src/test/java/MyIT.java" exit 0 EOF - printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/sleep" - printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/gcloud" - printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/terraform" - cat > "${tmp_repo}/mock-bin/mvn" <<'EOF' + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/sleep" + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/gcloud" + printf '#!/bin/bash\nexit 0\n' > "${tmp_repo}/mock-bin/terraform" + cat > "${tmp_repo}/mock-bin/mvn" <<'EOF' #!/bin/bash if [[ "$1" == "verify" ]]; then mkdir -p "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports" - touch "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/TEST-MyIT-sponge_log.xml" \ - "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/MyIT-sponge_log.txt" + touch "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/TEST-MyIT-sponge_log.xml" + if [[ "${MOCK_OMIT_TXT_LOG:-false}" != "true" ]]; then + touch "${MOCK_REPO_ROOT}/mod-a/target/failsafe-reports/MyIT-sponge_log.txt" + fi exit "${MOCK_MVN_VERIFY_EXIT:-0}" fi exit "${MOCK_MVN_INSTALL_EXIT:-0}" EOF - chmod +x "${tmp_repo}/mock-bin/"* - - local helper - for helper in init plan apply populate-env; do - echo ":" > "${tmp_repo}/.cloud/helpers/${helper}.sh" - done - echo "touch \"${tmp_repo}/destroy.ran\"" > "${tmp_repo}/.cloud/helpers/destroy.sh" - echo "touch \"${tmp_repo}/codecov.ran\"" > "${tmp_repo}/gfile/codecov.sh" - cat > "${tmp_repo}/gfile/linux_amd64/flakybot" < "${tmp_repo}/.cloud/helpers/${helper}.sh" + done + # Mirrors real .cloud/helpers/destroy.sh (set -eo pipefail + failing cmd + trailing popd) + cat > "${tmp_repo}/.cloud/helpers/destroy.sh" </dev/null +touch "${tmp_repo}/destroy.ran" +if [[ "\${MOCK_DESTROY_EXIT:-0}" -ne 0 ]]; then + (exit "\${MOCK_DESTROY_EXIT}") +fi +popd >/dev/null +EOF + cat > "${tmp_repo}/gfile/codecov.sh" < "${tmp_repo}/gfile/linux_amd64/flakybot" </dev/null 2>&1 || exit_code=$? - - if [[ "${exit_code}" -ne 1 ]] || \ - [[ ! -f "${tmp_repo}/codecov.ran" ]] || \ - [[ ! -f "${tmp_repo}/flakybot.ran" ]] || \ - [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]]; then - echo "build.sh failing run did not preserve exit code or run post-build hooks (exit=${exit_code})" - rm -rf "${tmp_repo}" - exit 1 - fi - - # 2. JOB_TYPE=integration with setup_cloud() runs both coerce_logs and destroy.sh on failure. - rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/destroy.ran" - exit_code=0 - ( - unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT \ - ENABLE_FLAKYBOT REPORT_COVERAGE - export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" - export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" - export GOOGLE_CLOUD_PROJECT="test-project" JOB_TYPE="integration" - export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="1" - bash "${tmp_repo}/.kokoro/build.sh" - ) >/dev/null 2>&1 || exit_code=$? - - if [[ "${exit_code}" -ne 1 ]] || \ - [[ ! -f "${tmp_repo}/destroy.ran" ]] || \ - [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]]; then - echo "build.sh integration + setup_cloud failure missed destroy.sh or coerce_logs (exit=${exit_code})" - rm -rf "${tmp_repo}" - exit 1 - fi - - # 3. Standalone setup_cloud() without SETUP_CLOUD_DEFER_TRAP still installs EXIT trap. - rm -f "${tmp_repo}/destroy.ran" - exit_code=0 - ( - unset SETUP_CLOUD_DEFER_TRAP SETUP_CLOUD_DESTROY_REGISTERED - export PATH="${tmp_repo}/mock-bin:${PATH}" GOOGLE_CLOUD_PROJECT="test-project" - cd "${tmp_repo}" && source "${tmp_repo}/.kokoro/common.sh" && setup_cloud "mod-a" && exit 3 - ) >/dev/null 2>&1 || exit_code=$? + # 1. Failing mvn verify (exit 42): skips codecov.sh, runs coerce_logs + flakybot, preserves 42. + exit_code=0 + ( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export JOB_TYPE="integration-single" BUILD_SUBDIR="mod-a" + export ENABLE_FLAKYBOT="true" REPORT_COVERAGE="true" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="42" + bash "${tmp_repo}/.kokoro/build.sh" + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 42 ]] || \ + [[ -f "${tmp_repo}/codecov.ran" ]] || \ + [[ ! -f "${tmp_repo}/flakybot.ran" ]] || \ + [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]]; then + echo "build.sh failing run failed expectations (exit=${exit_code})" + exit 1 + fi - if [[ "${exit_code}" -ne 3 ]] || [[ ! -f "${tmp_repo}/destroy.ran" ]]; then - echo "standalone setup_cloud() failed to run destroy.sh or preserve exit code (exit=${exit_code})" - rm -rf "${tmp_repo}" - exit 1 - fi - rm -rf "${tmp_repo}" + # 2. Passing mvn verify + failing codecov.sh (exit 17): exits 17, still runs coerce_logs + flakybot. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/codecov.ran" "${tmp_repo}/flakybot.ran" + exit_code=0 + ( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export JOB_TYPE="integration-single" BUILD_SUBDIR="mod-a" + export ENABLE_FLAKYBOT="true" REPORT_COVERAGE="true" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="0" MOCK_CODECOV_EXIT="17" + bash "${tmp_repo}/.kokoro/build.sh" + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 17 ]] || \ + [[ ! -f "${tmp_repo}/codecov.ran" ]] || \ + [[ ! -f "${tmp_repo}/flakybot.ran" ]]; then + echo "build.sh passing run + failing codecov.sh failed expectations (exit=${exit_code})" + exit 1 + fi + + # 3. Failing mvn verify (exit 42) + failing coerce_logs.sh (missing .txt): still runs flakybot, preserves 42. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/flakybot.ran" + exit_code=0 + ( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export JOB_TYPE="integration-single" BUILD_SUBDIR="mod-a" ENABLE_FLAKYBOT="true" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="42" + export MOCK_OMIT_TXT_LOG="true" MOCK_REQUIRE_COERCED="false" + bash "${tmp_repo}/.kokoro/build.sh" + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 42 ]] || [[ ! -f "${tmp_repo}/flakybot.ran" ]]; then + echo "build.sh failing run + failing coerce_logs.sh failed expectations (exit=${exit_code})" + exit 1 + fi + + # 4. JOB_TYPE=integration + setup_cloud() + failing destroy.sh (exit 9, with trailing popd): + # 4a. On failing mvn (exit 42): warns about destroy failure (9) and preserves 42. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/destroy.ran" + exit_code=0 + output=$( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT \ + ENABLE_FLAKYBOT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export GOOGLE_CLOUD_PROJECT="test-project" JOB_TYPE="integration" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="42" MOCK_DESTROY_EXIT="9" + bash "${tmp_repo}/.kokoro/build.sh" 2>&1 + ) || exit_code=$? + + if [[ "${exit_code}" -ne 42 ]] || \ + [[ ! -f "${tmp_repo}/destroy.ran" ]] || \ + [[ ! -f "${tmp_repo}/mod-a/target/failsafe-reports/test-job/MyIT/sponge_log.xml" ]] || \ + [[ "${output}" != *"Warning: destroy_cloud failed with exit code 9"* ]]; then + echo "build.sh integration failure + failing destroy.sh failed expectations (exit=${exit_code})" + exit 1 + fi + + # 4b. On passing mvn (exit 0) + failing destroy.sh (exit 9): promotes 9 instead of exiting 0. + rm -rf "${tmp_repo}/mod-a/target" "${tmp_repo}/destroy.ran" + exit_code=0 + output=$( + unset GOOGLE_APPLICATION_CREDENTIALS TEST_ALL_MODULES INTEGRATION_TEST_ARGS SUREFIRE_JVM_OPT \ + ENABLE_FLAKYBOT REPORT_COVERAGE + export HOME="${tmp_repo}/fake-home" PATH="${tmp_repo}/mock-bin:${PATH}" + export KOKORO_GFILE_DIR="${tmp_repo}/gfile" KOKORO_JOB_NAME="prefix/test-job" + export GOOGLE_CLOUD_PROJECT="test-project" JOB_TYPE="integration" + export MOCK_REPO_ROOT="${tmp_repo}" MOCK_MVN_VERIFY_EXIT="0" MOCK_DESTROY_EXIT="9" + bash "${tmp_repo}/.kokoro/build.sh" 2>&1 + ) || exit_code=$? + + if [[ "${exit_code}" -ne 9 ]] || \ + [[ ! -f "${tmp_repo}/destroy.ran" ]] || \ + [[ "${output}" != *"Warning: destroy_cloud failed with exit code 9"* ]]; then + echo "build.sh passing run + failing destroy.sh should exit 9 (got exit=${exit_code})" + exit 1 + fi + + # 5. Standalone setup_cloud() under set -eo pipefail (simulating downstream-build.sh). + rm -f "${tmp_repo}/destroy.ran" + exit_code=0 + ( + set -eo pipefail + unset SETUP_CLOUD_DEFER_TRAP SETUP_CLOUD_DESTROY_REGISTERED + export PATH="${tmp_repo}/mock-bin:${PATH}" GOOGLE_CLOUD_PROJECT="test-project" + cd "${tmp_repo}" + source ./.kokoro/common.sh + setup_cloud "mod-a" + cd "${tmp_repo}/mod-a" + (exit 3) + ) >/dev/null 2>&1 || exit_code=$? + + if [[ "${exit_code}" -ne 3 ]] || [[ ! -f "${tmp_repo}/destroy.ran" ]]; then + echo "standalone setup_cloud() failed to run destroy.sh or preserve exit code (exit=${exit_code})" + exit 1 + fi + ) || exit 1 } test_find_all_poms_with_versioned_dependency