'CodeBuild succeeds while Node build command has failed

Let's assume that we have AWS CodeBuild project which buildspec file contains the following:

"phases": {
"install": {
  "runtime-versions": {
    "nodejs": 14
  }
},
"build": {
  "commands": [
    "echo Build started on `date`",
    "npm ci && npm audit --audit-level=critical",
    "node -r esm index.js --env=CLOUDTEST"
  ]
},
"post_build": {
  "commands": [
    "echo Build completed on `date`"
  ]
}

It runs successfully, but if, for example, env value is wrong, node command will fail, but the build still succeeds.

Question: what should I do to make CodeBuild project to fail when node command fails?



Solution 1:[1]

The build phase transitions to post_build by default, regardless whether the build succeeds or fails. To override this behaviour, explicitly set the phase's on-failure behaviour:

"build": {
  "on-failure": "ABORT",
  "commands": ["node BOOM"]
},

The bad node BOOM command causes the execution to fail immediately. Logs tail:

[Container] 2022/04/29 11:21:10 Command did not exit successfully node BOOM exit status 1
[Container] 2022/04/29 11:21:10 Phase complete: BUILD State: FAILED_WITH_ABORT
[Container] 2022/04/29 11:21:10 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: node BOOM. Reason: exit status 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1