Configure CodeBuild to use CloudFormation-created dynamic ReportGroup

How can I configure CodeBuild to use a ReportGroup created by CloudFormation?

AWS have recently (20/12/19) released support for shared CodeBuild Report Groups in CloudFormation.

I’d like to use a ReportGroup in my CloudFormation, but I can’t find a way to configure CodeBuild to use it.

It looks like reporting configuration is done in the buildspec, with an example of a hardcoded reference to a report group in this example buildspec snippet:

reports:
  arn:aws:codebuild:your-region:your-aws-account-id:report-group/report-group-name-1: #surefire junit reports
    files:
      - '**/*'
    base-directory: 'surefire/target/surefire-reports'
    discard-paths: false

If I’m creating my ReportGroup via CloudFormation, I don’t know the ARN to put into the buildspec.

So, my question is how can I inject a dynamically created ReportGroup ARN into the buildspec? Or is there a different/better way of configuring CodeBuild to use a ReportGroup created by CloudFormation?

You can use CloudFormation intrinsic functions to retrieve the ARN of the ReportGroup created by CloudFormation and pass it to the buildspec. Here’s an example of how to do it:

reports:
  !Sub 'arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:report-group/${ReportGroupName}':
    files:
      - '**/*'
    base-directory: 'surefire/target/surefire-reports'
    discard-paths: false

In this example, we’re using the !Sub function to substitute the ${ReportGroupName} variable with the name of the ReportGroup created by CloudFormation. The resulting ARN is then passed to the arn field in the buildspec.

Make sure to replace ${ReportGroupName} with the actual name of your ReportGroup in the CloudFormation template.