1👍
Django is looking for a database with hostname db
(from your settings file), which it can’t find, because there’s no host or service that responds to that name in the Github Actions environment.
In order for this to work in Github Actions, you need to setup a database service container, create a root user with password, and then run Django with those settings, which will allow Django to connect to the database container, create a database, run migrations, and then test with that testdatabase.
Here’s a working sample (for you to expand with your own project details where needed):
jobs:
tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
# The MySQL docker container requires these environment variables to be set
# so we can create and migrate the test database.
# See: https://hub.docker.com/_/mysql
MYSQL_DATABASE: testdb
MYSQL_ROOT_PASSWORD: testrootpass
ports:
# Opens port 3306 on service container and host
# https://docs.github.com/en/actions/using-containerized-services/about-service-containers
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v3
- name: Install Ubuntu dependencies
run: |
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev libmysqlclient-dev libgirepository1.0-dev
- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: 3.8
- name: Install Python dependencies
run: |
pip install -r requirements.txt
- name: Run tests
# These environment variables will take precedence over the ones in the Django settings file
env:
# Set the DJANGO_SECRET_KEY in the Github repo settings: Go to Secrets and variables > Actions > New repository secret
DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }}
DATABASE_NAME: testdb
DATABASE_USERNAME: root
DATABASE_PASSWORD: testrootpass
DATABASE_ENDPOINT: 127.0.0.1 # Will not work with 'localhost', since that will try a Unix socket connection (!)
run: |
python manage.py migrate
python manage.py test
Source:stackexchange.com