1

Issue

When trying to setup a Github action to test a Django project automatically, we ran into an issue with the django.yml setup. Whenever we ran the .yml, we got this exception on the last step (python manage.py test):

django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-3)")

To state, our docker environment and tests alone work fine, just when trying to do this in a Github action, we get issues.

What we have tried

  1. We have tried running a docker-compose up -d before running the test, to know for sure that our DB is running.
  2. Tried adding environment variables with DB info like this: https://github.com/Cuda-Chen/django-mysql-github-actions-demo/blob/main/.github/workflows/django-ci.yml

Current code

name: Django CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.8]

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Run Tests
      run: |
        python manage.py test

Does anyone know what is going on here, and how we can resolve this?

annes
  • 123
  • 1
  • 6

1 Answers1

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
Raoul
  • 1,876
  • 1
  • 14
  • 14