Home Automate Test-Coverage Badge status on Github
Post
Cancel
Preview Image

Automate Test-Coverage Badge status on Github

Cliargs.NET Coverage When I decided to share the source code of Cliargs.NET library on Github, I wanted to show some statistics badges on the README file, such as the count of downloads, the build status… and finally the test coverage.

Background

I am using ReportGenerator from Daniel Palme which is really a great tool to have a full report about the test coverage for lines / branches. So I set up the Github Action to get the test coverage report generated after each commit. This action uploads the artifacts to the build results (which is good actually), however these artifacts are uploaded in a zip file (Limitation from Github 😏).

The limitation I faced here, is there is no way to get a direct link to the generated badge svg file to get it displayed on the readme file. So I had to find a way to upload the badge file automatically after the report is generated, to another remote Git repository in order to ensure having a direct link to the badge from the readme file.

Step by step

If you are familiar with Git, Github workflows, you may jump to the last section and copy the code without reading the detailed step by step explanation.

Choice of remote git repository

Clone Github Wiki Github Wiki Git Url

In my case I have created a Wiki to add a documentation of Cliargs.NET. A Wiki of Github reposiotry is another Git repository with a different address, usually https://github.com/[Username]/[Repository Name].wiki.git.

Nevertheless, there are tons of other ways to get a public Git repository, for me the easiest way is Gist!

Clone Github Wiki Gist repository url

Each Gist is a Git repository, with an address https://gist.github.com/[Unique Id].git, so you just clone the repository and start commiting changes.

Create a Personal Access Token

Of course it is not a good idea to write your password in a Workflow file, so you need to generate a new personal access token from Github Settings > Developer settings > Personal access tokens then click generate new token.

Each token has a defined scopes, chose the ones you prefer. For this example, we need a commit / push permissions. Once the token is generated, make sure to copy it and save it somewhere immediately as it will not be visible again!

Create an encrypted secret

At this step we need to create a Github secret to be able to call the generated personal access token from the Workflow using the secret name instead of pasting the token into the Workflow file for security reasons.

Navigate to your repository settings (The repository from where the action will be executed), https://github.com/[Username]/[Repository name]/settings then navigate on the left sidebar to Secrets > Actions.

Clone Github Wiki Github Repository new secret

Choose a good name and save it somewhere because you’ll need it later.

Add file to a repository

In my case, I had to run first the job of ReportGenerator, then recover the generated badge before pushing. But in the following example, to make it easier, I choose to download the Cliargs.NET icon using curl then push it back to a remote repository.

Checkout a remote repository

Here we need to checkout the remote repository and change the working directory to the repository local folder.

1
2
git clone https://github.com/[Github Username]/[Repository Name].git
cd [Repository Name]

Add file to local repository directory

As explained above, I download the file from web named Cliargs.png, but in your case you may need to move the file from another location to the local repo directory.

1
curl https://raw.githubusercontent.com/YounesCheikh/Cliargs.NET/main/Cliargs.png --output Cliargs.png

Configure git credentials

Here is very important to don’t set the password, only set the username and email to identify the author to git server when pushing changes.

If your repository is public, better don’t set the email, otherwise, you will get spammed, In my case i just keep it empty. 😉

1
2
3
git config credential.helper store
git config user.name "Your name"
git config user.email "<>"

Set the git url

The tricky here is to use the git set-url in order to use your personal access token instead of your password! At this step you’re supposed to get the personal access token generated and the repository secret saved.

The call to your saved secret is called in format ${{ secrets.[Github Secret] }}

1
git remote set-url origin https://${{ secrets.[Github Secret] }}@github.com/[Github Username]/[Remote  name].git

Commit the changes and push

1
2
3
git add `Cliargs.png`
git commit -m "updated file"
git push

Implement all in your Workflow

Now you followed the step by step above, it is time to append the global code in your Workflow yml file.

1
2
3
4
5
6
7
8
9
10
11
12
    - name: Upload file to remote git repo
      run: |
        git clone https://github.com/[Github username]/[Respository name].git
        cd [Respository name]
        git config credential.helper store
        git config user.name "[Your name]"
        git config user.email "<>"
        git remote set-url origin https://${{ secrets.[Secret name] }}@github.com/[Github username]/[Respository name].git
        curl https://raw.githubusercontent.com/YounesCheikh/Cliargs.NET/main/Cliargs.png --output Cliargs.png
        git add Cliargs.png
        git commit -m "updated file"
        git push

Conclusion

For me, I have reached the target with the several lines above, and the test coverage badge is well shown on the repository home page (readme file). May be in the future, I will try to share something more advanced, such as create a Github Action that automate all the steps above.

This post is licensed under CC BY-NC-SA 4.0 by the author.

First release of Cliargs.NET

-