CodeBuild: Always run the 'artifacts' phase regardless of success or failure

388 Views Asked by At

I'm using an Jenkins pipeline to trigger AWS CodeBuild, and in my buildspec I run some tests that I wish to publish as artefacts so that they can be downloaded and read by Jenkins.

When all of my tests pass, this works just fine. However, when one or more tests fail, it seems as though the artifacts phase is ignored, so there are no artefacts for Jenkins to download.

Though it's not what I require, I have also attempted to use the reports phase, but that behaves in the exact same way, which I find confusing as it seems crazy to fail on a test and then not publish the reports.

Is it possible to make CodeBuild execute the artifacts phase regardless of success or failure?

version: 0.2

env:
  shell: bash

phases:
  install:
    runtime-versions:
      python: latest
    commands:
      - pip install cfn-lint checkov
      - ...

  pre_build:
    commands:
      - cd myproj
      - cfn-lint --template cloudformation/template.cfn.yaml --format junit > cfn-lint.xml
      - checkov --directory cloudformation --framework cloudformation secrets --output=junitxml > checkov.xml

  build:
    commands:
      - ...

  post_build:
    commands:
      - ...

artifacts:
  base-directory: myproj
  files:
    - cfn-lint.xml
    - checkov.xml

1

There are 1 best solutions below

0
On

The answer here is that the artefacts (and reports) phase is not run if the pre_build phase fails.

While I've technically answered my question, this means that I've had to move tests into the build phase, which feels wrong because the post_build phase is run regardless of success or failure, so publishing of my artefact (outside of AWS) also fails.