Automation is really important for performing mundane and redundant tasks like testing. When someone raises a new PR for your project, it's nice to have some tests running automatically. This saves a lot of your time in reviewing the code.
Testing can be done for detecting issues like:
build fails
linting/formatting errors
unexpected results
dependency errors...and more
If your project is maintained on GitHub then GitHub Actions can prove useful in automating things like these. With the use of GHA (abbrev. for GitHub Actions), you can easily automate tasks like:
Releases
Publishing package
Tests
Preview deployments...and a lot more!
So let's take an example to understand how you can also achieve the same for your project.
Let's say you have a Python project in which you have implemented tests using some testing framework like pytest. Now you want to run these tests automatically on every new PR that's raised for this project.
So first of all, create a file like this at the root of your project: .github/workflows/tests.yml
Now inside the tests.yml file, you can paste some code like this:
name: tests
on:
push:
branches:
- master
paths-ignore:
- '.gitignore'
- '**.md'
pull_request:
branches:
- master
paths-ignore:
- '.gitignore'
- '**.md'
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python_version: [
'3.7',
'3.11'
]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}
- name: Install dependencies
run: pip install -r requirements.txt
- name: pytest
run: pytest -v
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Let's break the code step by step:
name: tests
define the name of this GHAon
tell us that on which event should this GHA trigger. In this case, it would run on every new PR or code push that is made to the master branch. Also, notice thepaths-ignore
config which tells us that changes made to these files should not trigger the tests GHA.Now we can finally explain the job. We can tell it to run using which server: like in this case it's going to run on an Ubuntu machine.
Using
strategy -> matrix
we can tell it to run the tests for different versions of Python. This is useful when you want to see if your project is compatible with the older versions or not.Now inside
steps
you need to tell the step-by-step procedure to follow before you can run the tests first, you need to check out the current code changes as described in the PR, then set up Python and later on install some dependencies before you can finally go ahead and run the tests.Each step has a
name
, a command to execute usingrun
and some other essential config like maybe it needs some env variable. Note that the secrets.GITHUB_TOKEN is not meant to be supplied by you.If everything is fine then the tests would run successfully and you can see the logs by going to the
Actions
tab in your GitHub project.
There are many configurations that you can make for your own GHA based on your project. This is just to give you a headstart and show you how easy it's to set up a GHA.
Now you can start using GitHub Actions in your open source projects to level up your automation game! Thank for reading till the end...๐