1

I have a buildspec.yml consumed by AWS CodeBuild with build and post_build tasks. I was a bit surprised when I found out that post_build tasks are executed even if any of build commands fail.

How is possible to force quit of execution if any of build commands fail?

Thanks!

version: 0.2

env:
    variables:
        S3_BUCKET: "..."
        BUILD_ENV : "prod"

phases:
  install:
    commands:
      - echo Installing source NPM dependencies...
      - npm install
      - npm install -g @angular/cli
  build:
    commands:
      - echo Build started on `date`
      - ng build --prod --aot --source-map=false
  post_build:
    commands:
      - echo Running post_build commands on `date`

      - aws s3 sync dist s3://${S3_BUCKET} --recursive

      - aws cloudfront create-invalidation --distribution-id XXXXXXXXXXXXXX --paths '/index.html'
Luke1988
  • 1,850
  • 2
  • 24
  • 42
  • 1
    Okay I found out that this is a standard behaviour, see [this post](https://stackoverflow.com/questions/46584324/code-build-continues-after-build-fails) – Luke1988 Jun 14 '19 at 11:48
  • Possible duplicate of [Code build continues after build fails](https://stackoverflow.com/questions/46584324/code-build-continues-after-build-fails) – Unsigned Jun 20 '19 at 22:45

1 Answers1

4

add this in build or any section you want

build:
on-failure: ABORT

Also this may be helpful it shows transaction state

Seifolah
  • 341
  • 3
  • 14
  • Per chance does anybody know the Powershell equivelant? I'm stuck using a Windows image. – Matthew Allen Jul 20 '22 at 13:16
  • FYI: if ($CODEBUILD_BUILD_SUCCEEDING -eq 0) { exit 1 } will exit the phase, but the artifacts still runs. Is that how the ABORT works in ssh? I suppose I can repeat the check down in artifacts. Or maybe issue the AWS CLI command to kill the build job altogether if it is that important. – Matthew Allen Jul 20 '22 at 16:17
  • docs: https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-syntax – AndyTheEntity Nov 16 '22 at 18:11