Django Github CI无法连接到mysql 'db'

2vuwiymt  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(147)

问题

在尝试设置Github动作来自动测试Django项目时,我们遇到了django.yml设置的问题。每当我们运行. yml时,我们在最后一步(python manage.py test)遇到了这个异常:

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

要说明的是,我们的Docker环境和测试本身工作得很好,只是当试图在Github操作中这样做时,我们遇到了问题。
∮我们所尝试的∮
1.在运行测试之前,我们尝试运行一个docker-compose up -d,以确定我们的DB正在运行。
1.尝试添加具有如下DB信息的环境变量:https://github.com/Cuda-Chen/django-mysql-github-actions-demo/blob/main/.github/workflows/django-ci.yml

当前代码

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

有人知道这是怎么回事吗?我们如何解决这个问题?

ewm0tg9j

ewm0tg9j1#

Django正在寻找一个主机名为db的数据库(在您的设置文件中),但它找不到,因为在Github Actions环境中没有响应该名称的主机或服务。
为了在Github Actions中运行,你需要设置一个数据库服务容器,创建一个带密码的root用户,然后使用这些设置运行Django,这将允许Django连接到数据库容器,创建一个数据库,运行迁移,然后使用该testdatabase进行测试。
下面是一个工作示例(您可以根据需要添加自己的项目详细信息):

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

相关问题