How to configure github actions for a python package
This is what a build action for running a test matrix in github actions looks like in python. The yaml file is saved in .github/workflows/<your_name>.yml
.
name: Running unittests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]
django: ["2.2", "3.0", "3.1"]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-tests.txt
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Linting
run: flake8
- name: Run unittests
env:
TOX_ENV: py${{ matrix.python-version}}-django${{ matrix.django }}
run: |
tox -e $TOX_ENV
In the matrix we define the python versions and in this case also the django versions. django
in this case will be set as an environment variable.
Further down I've set a TOX env to set exactly how I expect the tox environment variable to be set. So that is what I'm relying tox
to use in the end.
The python package tox-gh-actions may help mapping the python versions with the tox versions. In this case I chose to do it directly with github actions to keep things simpler. One less dependency, one less worry so to speak.
If you need a database to be able to run the tests you would need to include the following snippet. Include it so that it is a child of build
. The snippet is specifically for postgres, but other databases obviously follow a similar pattern.
services:
postgres:
image: postgres:12
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
For the full documentation of all the things you can do with github actions there is the official documentation.
Thanks for reading! If you need anyone to solve technical problems or build web applications in Django, send me an email and let's talk.
If you're interested in getting the latest blog posts directly to your inbox you can subscribe below.