As it turns out. The way to integrate code coverage into your build pipeline with GitHub actions is to use a third-party solution, like codcov.io and others. Those solutions are fantastic but can cost up to 20$ / month per user. And they come with many advanced features that not everybody needs. For a basic code coverage check on pull requests and a code coverage badge in the README.md I don’t want to pay a monthly subscription.
After digging the Internet for a free solution I found nothing useful. And decided to hack my way around.
In this post, ill show how to use GitHub actions and some cloud storage ( like s3 ) to create a code coverage badge for your repository, and a GitHub status which can be used to protect the master branch, so if a pull request dropped the code coverage it will be blocked for merging.
A GitHub badge is simply a small SVG embedded in the repo README.md. The simplest way to create one is to use shields.io API.
curl https://img.shields.io/badge/coavrege-$total%-$COLOR > badge.svg
Where $COLOR is a bash variable containing a CSS color like red, green, orange. And $total is another bash variable with the percentile between 0 and 100. shields.io are awesome for providing this free utility.
This simple one-liner can create a badge, what’s left is to upload it to a public storage with cache disabled and embed it in a README.md
example with GCP cloud storage
A GitHub actions workflow can run the above on a push/merge to master branch and upload the badge, notice the “Cache-Control” header, this is important because at least in GCP but probably also on AWS if you upload an object to a public storage its cached by default and then the README.md of your project will always contain an outdated version.
#github-actions #code-coverage #golang #ci #testing