For the longest time I have been ignoring GitHub Actions despite all the good things said about them. I’ve been a long-time user of CircleCI [1] - it is a solid CI/CD platform, and it is perfectly sufficient for my (minimal) needs. However, a few days ago I decided to give GitHub Actions a shot - having CI/CD steps defined on the same platform where the code resides seemed appealing.
As an initial test, I set up a simple pytest
flow for a Django (Python) project. I was expecting to struggle a bit with setting it up the first time, but it turned out to be a straightforward process. However, I did have to google a bit to arrive at the setup that I wanted. Thus, I decided to share a short instruction set on how to do it to have something to refer back to in the future. And who knows - perhaps some weary wanderer on the Internet will find it useful, too.
Setting Up Pytest GitHub Action
- Go to your project on GitHub
- Click on "Actions" tab
- Select or search for “Python Application”
- Click “Configure”.
- You’ll get the default config that has three steps: setup Python, install dependencies, and lint with
flake8
- Change the file name to
pytest
(or any other descriptive name) - Change
to
- Remove unnecessary commands and dependencies, such as
flake8
orif [ -f requirements.txt ]
condition - Add cache [2][3] right after setting up Python, but before dependency installation:
This will cache the downloaded assets (tarballs, wheels, etc). Thus, pip
will skip re-downloading them on every action if it finds them present in the cache. Note that it will still install dependencies rather than retrieve them from cache, because this setup will not cache the installed packages (which is possible [4]).
- Finally, add
pytest
step:
- Click “Start commit” and commit the changes - action will start running automatically
Final Configuration
The final output should be similar to the following:
With this setup GitHub will run pytest on every push to the main
branch, as well as on any pull request that wishes to merge changes into the main
branch. It will also make sure to cache downloaded dependency-related assets.