Skip to content

fix(ci): ensure build.sh runs coerce_logs, flakybot, and teardown on failure - #15305

Open
igorbernstein2 wants to merge 3 commits into
mainfrom
ci/kokoro-fix-failure-exit-path
Open

igorbernstein2 wants to merge 3 commits into
mainfrom
ci/kokoro-fix-failure-exit-path

Conversation

@igorbernstein2

@igorbernstein2 igorbernstein2 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a pre-existing set -eo pipefail bug in .kokoro/build.sh where failing mvn invocations exit immediately before running .kokoro/coerce_logs.sh or flakybot.

(Split out from #15301 so this bugfix can be reviewed independently of CLEANUP_TARGET_DIRS.)

Changes

  • Move post-build codecov.sh (on passing builds when REPORT_COVERAGE=true), coerce_logs.sh, flakybot (when ENABLE_FLAKYBOT=true), and setup_cloud() teardown into an EXIT trap (finalize_build) in .kokoro/build.sh.
  • Rely naturally on set -e fail-fast across all JOB_TYPE branches (removing dead RETURN_CODE variables) — bash passes the failing command's exit code directly into finalize_build (local exit_code=$?).
  • Log warnings on coerce_logs.sh / flakybot errors so reporting hook failures never skip subsequent teardown or mask the build's exit code; codecov.sh and destroy_cloud failures still fail passing builds (while preserving the original build exit code on failing builds).
  • Run destroy_cloud inside ( set -eo pipefail; destroy_cloud ) as a standalone statement (not on the LHS of ||) so Bash does not suppress set -e inside .cloud/helpers/destroy.sh, and destroy.sh's set -eo pipefail / exit calls cannot leak into finalize_build.
  • Extract destroy_cloud() from setup_cloud() in .kokoro/common.sh (reusing commonScriptDir) and gate trap destroy EXIT behind SETUP_CLOUD_DEFER_TRAP=true so .kokoro/build.sh's EXIT trap is never clobbered by setup_cloud(), while external callers (e.g., sdk-platform-java/.kokoro/presubmit/downstream-build.sh) retain identical standalone EXIT trap behavior.
  • Add test_build_sh_failure_exit_path in .kokoro/common_test.sh.

Testing

  • bash -n .kokoro/common.sh .kokoro/build.sh .kokoro/common_test.sh
  • test_build_sh_failure_exit_path in .kokoro/common_test.sh verifying:
    • Failing mvn verify (exit 42) skips codecov.sh, runs coerce_logs.sh and flakybot (verifying build.sh's chmod +x), and preserves exit 42.
    • Passing mvn verify + failing codecov.sh (exit 17) exits 17 while still running coerce_logs.sh and flakybot.
    • Failing mvn verify (exit 42) + failing coerce_logs.sh still runs flakybot and preserves exit 42.
    • JOB_TYPE=integration + setup_cloud() with a realistic destroy.sh (set -eo pipefail + failing command + trailing popd) logs the warning and preserves exit 42 on failing builds, and promotes exit 9 on passing builds instead of swallowing teardown failure as 0.
    • Standalone setup_cloud() under set -eo pipefail (after cd into a subdir) still runs destroy.sh and preserves exit 3.

…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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Kokoro build scripts to ensure that post-build operations—such as log coercion, flakybot reporting, and cloud resource teardown—always execute on both successful and failed builds by introducing an EXIT trap (finalize_build). It also updates Maven command executions to capture exit codes instead of exiting immediately, and adds corresponding integration tests. The review feedback suggests guarding subsequent steps in .kokoro/build.sh if a prior Maven command fails, and registering the cleanup trap at the beginning of setup_cloud() in .kokoro/common.sh to prevent resource leaks if the setup itself fails.

Comment thread .kokoro/build.sh Resolved
Comment thread .kokoro/common.sh Resolved
igorbernstein2 added a commit that referenced this pull request Dec 1, 2026
@igorbernstein2
igorbernstein2 marked this pull request as ready for review December 1, 2026 19:20
@igorbernstein2
igorbernstein2 requested a review from a team as a code owner December 1, 2026 19:20
Comment thread .kokoro/build.sh
Comment on lines +50 to +62
if [[ "${exit_code}" -eq 0 && "${REPORT_COVERAGE}" == "true" ]]; then
bash "${KOKORO_GFILE_DIR}/codecov.sh"
exit_code=$?
fi

# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC: @jinseopkim0

do you have any thoughts on either codecov or flakybots as part of the CIs? I assume that we don't really have/ use either one of these right now. Doesn't seem like there is any harm in keep them as it seems like they need to be opt in via env vars.

@lqiu96 lqiu96 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for finding this. I think we can proceed and we can do any follow up changes to either code cov/ flakybot in a follow up PR

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants