fix: Exit non-zero when build run finishes with a failed, cancelled or timed out build #2
Discarded
Robert Navarro opened 2 days ago

I run tod build run from scripts and coding agents against my OneDev 16.9.1 server, and a failed build returned exit 0. The log ends with the red "Build #N is failed" line, but tod build run --local ci && next-step carried on to the next step anyway.

In streamBuildLog, the FAILED / CANCELLED / TIMED_OUT branch prints the red status and then breaks out of the read loop, so the function returns nil the same way it does for SUCCESSFUL. With this change that branch returns a sentinel error, errBuildNotSuccessful. main exits 1 for it but skips printing the error, since the red status line already says what happened. Successful builds still exit 0 and their output is unchanged.

streamBuildLog is only reached through runBuildJobCommand, so this covers --branch, --tag and --local, plus the hidden tod run alias. I did not find another command that streams and waits on a build.

Heads up: pressing Ctrl-C during tod build run cancels the build, the server then sends CANCELLED, and the command now exits 1 where it used to exit 0.

Exit codes I measured with tod build run --local <job> against my server, using a scratch buildspec with one job that runs exit 0, one that runs exit 1, one that sleeps past a 10 second job timeout, and one I cancelled with SIGINT while it was running:

Build resulttod 4.3.4 (installed)current main (4.3.5)this branch
successful000
failed001
timed_out001
cancelled (SIGINT)0not run1

On this branch the failed and timed out runs print no extra error line.

I added TestStreamBuildLogReturnsErrorForUnsuccessfulBuild in build_commands_test.go. It serves a single status frame from an httptest server and checks that SUCCESSFUL returns nil and the other three return errBuildNotSuccessful. gofmt, go vet ./..., go test ./... and go build pass. I only ran the live test with --local. --branch and --tag go through the same function, but I did not run them against a server. I also only tested on Linux.

I searched the onedev/tod issues and pull requests, and the onedev/server issues, for exit code and build run reports before putting this up and did not find one.

I used an AI coding assistant to help write this change, and I reviewed and tested it myself.

This pull request has been discarded
  • fix: Exit non-zero when build run finishes with a failed, cancelled or timed out build
    tod build run printed "Build #N is failed" (or cancelled / timed_out) in red but still exited 0, so scripts and agents chaining on the exit status carried on after a failed build. streamBuildLog now returns a sentinel error for those terminal statuses, and main exits 1 for it without printing a second error line. Successful builds keep exit 0 and the same output.
    2 days ago
  • Robin Shen commented 2 days ago

    It is expected that tod exits with 0 even if build is failed, as tod executes the command itself successfully. You will need to get status of the build to check its result.

  • Robin Shen discarded 2 days ago
1/1
Merge Strategy
Create Merge Commit
Watchers (2)
Reference
PR TOD-2
Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover