Creating a GitHub Actions Workflow for Code Coverage Reporting in Golang Projects
Introduction
GitHub Actions is a feature provided by GitHub to automate software workflows. It can be used for continuous integration, continuous delivery, and continuous deployment. In this tutorial, we will be creating a GitHub Actions Workflow to check the code coverage of a Golang project and output a report.
Prerequisites
Before starting this tutorial, you should have the following prerequisites:
- A GitHub account
- A Golang project hosted on GitHub
- Knowledge of the Go programming language and its tooling
Step 1: Create a new GitHub repository for your action
To create a new repository, go to your GitHub profile and click on the “Repositories“ tab. Then click on the “New“ button on the right side of the screen. Give the repository a name and make sure the repository is public. You can initialize the repository with a README if you like.
Step 2: Create a new workflow file
Once you have created your repository, navigate to the “Actions“ tab. Here, you will see an option to create a new workflow. Select this option and a new file will be created.
Step 3: Add the code coverage action to your workflow
In the workflow file, you need to define the steps that should be executed when the action is run. Here's the code to check the code coverage of a Golang project:
name: Check code coverage
on: [push]
jobs:
check-coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Install dependencies
run: |
go get -t -v ./...
- name: Test with coverage
run: |
go test -coverprofile=coverage.out ./...
- name: Output code coverage report
run: |
go tool cover -func=coverage.out
In the above code, we are using the actions/checkout action to checkout the code from the repository. Then we use the actions/setup-go action to setup the Go environment. The go get command is used to install all the dependencies required by the project. The go test command is then used to run the tests and generate a code coverage report. Finally, the go tool cover command is used to output the code coverage report.
Step 4: Push the changes to GitHub
Once you have added the code, commit the changes and push them to GitHub. You can do this using the command line or using the GitHub web interface.
Step 5: Verify the action
To verify that the action is working as expected, go to the “Actions“ tab in your repository. You should see your workflow being executed. Once it is complete, you can click on the workflow to see the code coverage report.
Conclusion
In this tutorial, we have learned how to create a custom GitHub Action to check the code coverage of a Golang project and output a report. By automating this process, you can save time and effort and ensure that your code coverage stays high. Pickering Technologies can help you with the adoption and optimisation of this process and help your business succeed.