Manually Create Coverage Badges Using GitHub Actions
Published on April 9, 2023
Adding a coverage badge to your repository is an excellent way to demonstrate your project's test coverage and maintain a high level of code quality. By the end of this guide, you will be able to generate a dynamic coverage badge that updates automatically with each new push to your repository.
##Prerequisites
- A Node.js project with test coverage reports generated by a tool like c8 or Istanbul.
- A GitHub repository for your project.
- GitHub deploy keys configured for your repository.
Let's get started!
##Step 1: Install Cheerio
Before anything else, make sure you have a coverage report generated by your testing tool. In our case, we will use the HTML report generated by c8. This report will look something like this:
./coverage/index.html
We will use cheerio, a jQuery-like library for server-side to extract the percentage data from the HTML. Install it to your devDependencies using the package manager of your choice:
##Step 2: Create a Script to Extract Coverage Data
Next, we need to create the actual script that will extract the coverage data from the HTML report. Create a new file called ./scripts/generate-badges.js and add the following code:
./scripts/generate-badges.js
This is a fairly straightforward script that uses Cheerio to parse the HTML report and extract the coverage data. It then uses the data to generate the shields.io URLs for each badge type. Finally, it downloads each badge and saves it to the ./coverage/badges directory.
Update your package.json to add a new script that will run the generate-badges.js script:
package.json
Run the script to generate the badges:
##Step 3: Configure GitHub Actions
NOTE: For this guide, we will use pnpm, but the commands should be similar for other package managers.
NOTE: This requires that you have an SSH Deploy Key configured for your repository. See Create SSH Deploy Key for more information.
In your repository, create a .github/workflows directory if it doesn't already exist. Then, create a new YAML file inside the workflows directory called coverage-badge.yml. This file will define the GitHub Actions workflow that generates the coverage badge.
Add the following content to coverage-badge.yml:
.github/workflows/coverage-badge.yml
Let's break down the workflow into its various sections:
The workflow is triggered when there's a push to the main branch or can be triggered manually (workflow_dispatch).
We're using a matrix strategy to run the workflow on multiple operating systems, Node.js versions, and pnpm versions. For our purpose, we will only use one version of each, but this is a good practice to follow.
This step checks out your repository using the actions/checkout action. Then, it sets up Node.js using the actions/setup-node action.
This step installs pnpm using the pnpm/action-setup action and the specified pnpm version from the matrix. We're not installing any dependencies yet, so we don't need to run pnpm install.
These steps will use the cache from actions/cache and install the project's dependencies using pnpm. We're using the cache to reduce installation time and to avoid downloading the same dependencies multiple times.
This step runs the coverage script, which should generate a coverage report. Then, it runs the generate-badges script, which should generate the coverage badges.
Lastly, this step pushes the coverage artifacts, including the generated badges and the coverage report, to a separate coverage branch in your repository using the peaceiris/actions-gh-pages action.
Options:
- The deploy_keyoption is set to theCOVERAGE_DEPLOY_KEYsecret, which is a Deploy Key stored in your repository settings. This deploy key should have write access to the repository. Follow the instructions from Create SSH Deploy Key to generate it and add it to your repository.
- The publish_diris set to./coverage, which is the directory containing the coverage report and badges.
- The allow_empty_commitoption is set to true to allow empty commits if there are no changes in the coverage artifacts.
- The publish_branchis the name of the branch you want to push the coverage artifacts to. It's currently set tocoverage, but you can change it to whatever you want.
##Step 4: Add Badge to README
To display the coverage badge in your README file, add the following line at the top of your README.md file:
README.md
These lines will render the badges as an image. It should point to the latest badges in your <PUBLISH_BRANCH_NAME> branch.
##Conclusion
Congratulations! You have successfully created a dynamic coverage badge for your Node.js project. This badge will help you showcase your project's test coverage and encourage you to maintain a high level of code quality. Don't hesitate to customize this workflow to suit your needs and adapt it to different coverage tools and badge styles.
Happy coding!