How to Use Github Workflow for Django Projects

Designing a GitHub workflow for CI/CD (Continuous Integration and Continuous Deployment) for a Django project involves creating a YAML configuration file that defines the steps to build, test, and deploy your Django application.
In this tutorial we will explore a step-by-step guide on how to set up a GitHub workflow for a Django project:

  1. Create a .github/workflows directory in your Django project’s root directory.
  2. Create a new YAML file in the .github/workflows directory, e.g., django_ci_cd.yml.
  3. Define the workflow triggers. You can trigger the workflow on push and pull request events:
name: Django CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

Next you will have to define the jobs for the workflow. Start with setting up the environment, installing dependencies, and running tests:

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: 3.8

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Run tests
      run: |
        python manage.py test

If your Django project requires a database for testing, you can set up a database service using the services key:

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:13
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test_db
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

Make sure to update your Django settings.py or settings_test.py file to use the correct database configuration for testing.
If your tests pass, you can proceed with the deployment process. Depending on your deployment target (e.g., Heroku, AWS, GCP, or Azure), you can use the appropriate GitHub Actions to deploy your Django application. For example, to deploy to Heroku, you can use the akhileshns/heroku-deploy action:

    - name: Deploy to Heroku
      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
      uses: akhileshns/heroku-deploy@v3.12.12
      with:
        heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
        heroku_app_name: 'your-heroku-app-name'
        heroku_email: 'your-email@example.com'

Make sure to replace ‘your-heroku-app-name’ and ‘your-email@example.com’ with your actual Heroku app name and email. Also, add your Heroku API key to your GitHub repository’s secrets.
The complete django_ci_cd.yml file should look like this:

name: Django CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:13
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test_db
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: 3.8

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Run tests
      run: |
        python manage.py test

    - name: Deploy to Heroku
      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
      uses: akhileshns/heroku-deploy@v3.12.12
      with:
        heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
        heroku_app_name: 'your-heroku-app-name'
        heroku_email: 'your-email@example.com'

By following these steps, you can set up a GitHub workflow for CI/CD for your Django project, which will automatically build, test, and deploy your application whenever you push changes to the main branch or create a pull request.
I hope you this was helpful

Happy Coding

Thanks for your attention

Jesus Saves

By Jesse Agbe(JCharis)

Leave a Comment

Your email address will not be published. Required fields are marked *