Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Continuous Integration with Django

Introduction to Continuous Integration

Continuous Integration (CI) is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests. CI aims to detect problems early, improve the quality of software, and reduce the time taken to deliver updates.

Setting Up a Django Project

Before we dive into Continuous Integration, let's set up a simple Django project. If you don't have Django installed, you can install it using pip:

pip install django

Next, create a new Django project:

django-admin startproject myproject

Navigate into the project directory:

cd myproject

Create a new app within your project:

python manage.py startapp myapp

Writing Tests in Django

Django comes with a test framework built-in. You can create tests by adding them to the tests.py file in your app directory. Here's an example:

from django.test import TestCase
from myapp.models import MyModel

class MyModelTest(TestCase):
    def test_str(self):
        my_model = MyModel(name="test")
        self.assertEqual(str(my_model), "test")

Run your tests using the following command:

python manage.py test

Setting Up Continuous Integration with GitHub Actions

GitHub Actions is a CI/CD service provided by GitHub. You can create workflows to build, test, and deploy your code directly from your GitHub repository.

Create a new directory named .github/workflows in the root of your repository. Inside this directory, create a file named ci.yml with the following content:

name: Django CI

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

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:latest
        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:
    - 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
      env:
        DATABASE_URL: postgres://postgres:postgres@localhost:5432/test_db
      run: |
        python manage.py migrate
        python manage.py test

This workflow sets up a PostgreSQL database and runs your Django tests whenever there is a push or a pull request to the main branch.

Conclusion

Continuous Integration is an essential practice for modern software development. By integrating regularly and using automated tests, you can detect problems early and ensure your codebase remains in a deployable state. With tools like GitHub Actions, setting up CI for your Django projects is straightforward and highly customizable.