Home > Software engineering >  CodeBuild succeeds while Node build command has failed
CodeBuild succeeds while Node build command has failed

Time:04-29

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?

CodePudding user response:

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": []
},

CodePudding user response:

Use bash as the default terminal, and use the -e builtin to stop the build phase as soon as you run into an error.

Then, configure the on-failure setting of the build phase to ABORT, so that the entire execution will terminate if the build phase errors out.


It should look as follows:

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