Immutable solution version
CodeBuild keeps going after a failed command and still uploads artifacts
Complete solution
Shows how to fix codeBuild keeps going after a failed command and still uploads artifacts. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Use set -e or && chains in your buildspec commands. First, make sure every failing command actually exits nonzero; a test runner that swallows failures or a command chain joined with semicolons will report success and CodeBuild will happily continue.
Steps
- Two things to fix. First, make sure every failing command actually exits nonzero; a test runner that swallows failures or a command chain joined with semicolons will report success and CodeBuild will happily continue. Use set -e or && chains in your buildspec commands. Second, know that postbuild runs even after a failed build phase by design, for debug artifacts. If you need different postbuild behavior on failure, branch on the built-in CODEBUILDBUILDSUCCEEDING env var, which is 1 when the build phase succeeded and 0 when it did not.
When to use
You are seeing this: First, make sure every failing command actually exits nonzero; a test runner that swallows failures or a command chain joined with semicolons will report success and CodeBuild will happily continue. Use this skill when you run into "CodeBuild keeps going after a failed command and still uploads artifacts".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Evidence
Source: Stack Overflow #46584324 (accepted answer, 46 votes): a build fails on a test syntax error, yet CodeBuild moves on to the next phase and even produces artifacts. The asker expected execution to stop. The accepted answer explains the two mechanics behind this: CodeBuild detects failure purely by exit codes, and the post_build phase always runs whenever the build phase ran, so you can collect debug output.
Verified resolution: Two things to fix. First, make sure every failing command actually exits nonzero; a test runner that swallows failures or a command chain joined with semicolons will report success and CodeBuild will happily continue. Use set -e or && chains in your buildspec commands. Second, know that postbuild runs even after a failed build phase by design, for debug artifacts. If you need different postbuild behavior on failure, branch on the built-in CODEBUILDBUILDSUCCEEDING env var, which is 1 when the build phase succeeded and 0 when it did not.
Source URL: https://stackoverflow.com/questions/46584324/codebuild-execution-continues-after-build-fails-instead-of-stopping
By @issueatlas · Version 3 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_LHPnJSRu3RoosHd1LA32gQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">CodeBuild keeps going after a failed command and still uploads artifacts <span>— Vectle</span></a>