Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeBuild execution continues after build fails instead of stopping

I'm building a CI/CD pipeline using git, codebuild and elastic beanstalk.

During codebuild execution when build fails due to syntax error of a test case, I see codebuild progress to next stage and ultimately go on to produce the artifacts.

My understanding was if the build fails, execution should stop. is this a correct behavior ?

Please see the buildspec below.

    version: 0.2      phases:       install:         commands:           - echo Installing package.json..           - npm install           - echo Installing Mocha...           - npm install -g mocha       pre_build:         commands:           - echo Installing source NPM placeholder dependencies...       build:         commands:           - echo Build started on `date`           - echo Compiling the Node.js code           - mocha modules/**/tests/*.js       post_build:         commands:           - echo Build completed on `date`     artifacts:       files:         - modules/*         - node_modules/*         - package.json         - config/*         - server.js 
like image 777
pkpk Avatar asked Oct 05 '17 11:10

pkpk


People also ask

Does CodeBuild run as root?

Currently you cannot run as a non-root user in CodeBuild, I have passed it to the team for further review. Your feedback is very much appreciated. Is this still the case? There's now a checkbox in the console...

How can I speed up CodeBuild?

You can speed up subsequent builds by using local caching. This is a good option for large intermediate build artifacts because the cache is immediately available on the build host. Local caching increases build performance for: Projects with a large, monolithic source code repository.

What does Buildspec stand for?

A buildspec is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build. You can include a buildspec as part of the source code or you can define a buildspec when you create a build project. For information about how a build spec works, see How CodeBuild works.


2 Answers

CodeBuild detects build failures by exit codes. You should ensure that your test execution returns a non-zero exit code on failure.

POST_BUILD will always run as long as BUILD was also run (regardless of BUILD's success or failure.) The same goes for UPLOAD_ARTIFACTS. This is so you can retrieve debug information/artifacts.

If you want to do something different in POST_BUILD depending on the success or failure of BUILD, you can test the builtin environment variable CODEBUILD_BUILD_SUCCEEDING, which is set to 1 if BUILD succeeded, and 0 if it failed.

like image 144
Unsigned Avatar answered Sep 22 '22 01:09

Unsigned


CodeBuild uses the environment variable CODEBUILD_BUILD_SUCCEEDING to show if the build process seems to go right.

the best way I found right now is to create a small script in the install secion and then alway use this like:

phases:   install:     commands:       - echo '#!/bin/bash' > /usr/local/bin/ok; echo 'if [[ "$CODEBUILD_BUILD_SUCCEEDING" == "0" ]]; then exit 1; else exit 0; fi' >> /usr/local/bin/ok; chmod +x /usr/local/bin/ok   post_build:     commands:       - ok && echo Build completed on `date` 
like image 23
wodka Avatar answered Sep 24 '22 01:09

wodka