djangocontinuous-integrationgithub-actions

In Django CI, githubactions try to another database for testing


This configuration refers to portfolio_db when running the server normally, and refers to test_portfolio_db during testing.

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "portfolio_db",
        "USER": "python",
        "PASSWORD": "python123",
        "HOST": "localhost",
        "PORT": "3306",
        "TEST": {"NAME": "test_portfolio_db"},
    }
}

However, when I actually run it through CI, for some reason it refers to portfolio_db. This is because githubactions assumes test_portfolio_db. Does anyone know why it refarences at portfolio_db and how to properly run the test? In addition, the test in the local environment passes

https://github.com/duri0214/portfolio/actions/runs/10016990263/job/27690629902?pr=42


Solution

  • before: https://github.com/duri0214/portfolio/actions/runs/10016990263/job/27690629902?pr=42

    after: https://github.com/duri0214/portfolio/actions/runs/10019680997/job/27696499042

    I had created test_portfolio_db in advance, but it seems that migrate is not necessary in the first place. This is because test_portfolio_db is created in manage.py test.

    In other words, the test passed after I removed the migrate command execution.

    django's test command uses an existing migration file without creating one (it works locally because there are files that are ignored). If you have .ignore the migrations file, remove it.

    name: Django CI
    
    on:
      push:
        branches: [ "master" ]
      pull_request:
        branches: [ "master" ]
    
    jobs:
      build:
        runs-on: ubuntu-latest
        strategy:
          max-parallel: 1
          matrix:
            python-version: [ 3.12 ]
    
        steps:
          - name: Checkout
            uses: actions/checkout@v3
    
          - name: Set up Python ${{ matrix.python-version }}
            uses: actions/setup-python@v3
            with:
              python-version: ${{ matrix.python-version }}
    
          - name: Set up
            run: |
              sudo systemctl start mysql
              mysql -u root -proot -e "CREATE USER 'python'@'localhost';"
              mysql -u root -proot -e "GRANT ALL ON test_portfolio_db.* TO 'python'@'localhost';"
              
              python -m pip install setuptools
              python -m pip install -r requirements.txt
    
          - name: Run tests
            run: |
              export DJANGO_SECRET_KEY="$(base64 <<< "$RANDOM|TeStiNg|$RANDOM" | tr -d '\n')"
              python manage.py test