It has been a while since my last post about CDK – we’ve had a few challenging months: we had to self-isolate several times, the whole family have been ill with a stomach bug, and our son is going through the terrible twos. So blogging, talks, and working on professional development had to be put on the backburner.
I finally had some time to continue the blog post about CDK Pipelines I had been working on probably since the beginning of the year. I had been trying to figure out how to make CodeBuild test reports work with CDK Pipelines. Last week when I got back to it and started looking at it again, I saw that the API that was used in Developer Preview has been updated (more information on it here). And now it looks like it is easier to plug in the reports to be used with this high level construct. While the old API is still in use, I will focus on the new API.
The purpose of this blog post is to demonstrate the set-up of CodeBuild test reports in CDK Pipelines for C#.
I have written a simple .NET Core application which returns the day of the week when you pass in a date in the query string. There are also a couple of XUnit tests:
The file tree looks like this:
For the task of creating CodeBuild test reports only without actually deploying the app, we will only work with CdkPipelinesPipelineStack.cs. In my case this was the file created automatically on cdk init, and it will contain the main pipeline.
Firstly, before we build the pipeline, we need to create a connection to our Github repo and get its ARN. I wrote a post about it a while back – AWS CDK Adventure Part 2: CDK Pipelines and GitHub fun – Oksana Horlock’s Dev Blog
We then create a report group:
We then use the CodePipeline construct in the Amazon.CDK.Pipelines namespace to create the pipeline. If we didn’t want to have any CodeBuild reports, we would set up the pipeline like so:
One of the CodePipelineProps is SelfMutation: when set to false, it’s quite handy when doing development work – you can just run cdk deploy and your local changes to the pipeline will be deployed bypassing the GitHub repo.
Synth property is used to set up the pipeline to pull changes from the GitHub repo, and also run the commands needed to produce the cloud assembly.
In order to set up the reports, we need to customize the CodeBuild project, and it can be done by using CodeBuildStep class instead of ShellStep. CodeBuildStepProps class, in turn, has a PartialBuildSpec property, which we can use to specify the reports. The reports part of the buildspec.yml file usually looks like this:
In CDK for C# the value of PartialBuildSpec has to be created using Dictionary<string, object>:
Another thing that needs to be added to be able to work with CodeBuild reports is a policy, otherwise you might see an authorization error like this when you try to deploy the stack:
The policy we need allows several report-related actions on the report group we have created:
And then we can define the necessary CodeBuildStepProps to set up reports:
Now, what is left to do is to use the CodeBuildStep as the value of Synth property:
After that we can commit the changes, run cdk deploy and check the CodeBuild test reports in the console: