Back to Blog
DevOpsdjangogithub-actionspython

Mastering Django Migrations Automation with GitHub Actions: The Ultimate Guide

Stop running Django migrations manually. This guide shows you how to automate safe, reversible database migrations as part of your CI/CD pipeline using GitHub Actions.

Athar Shah9 min read20 July 2024

Manual Django migrations are a ticking time bomb. You forget to run them, you run them in the wrong order, or someone runs them on production before the code is deployed. Here's how to eliminate all of that.

The Problem with Manual Migrations

  • Migrations run before code is deployed (or after, causing errors)
  • No audit trail of when migrations ran
  • Rollback requires manual intervention
  • Works on local, breaks on production because environment differs

The Solution: Migration as a Pipeline Step

yaml
- name: Run Django migrations
  run: |
    python manage.py migrate --check  # Fail if unapplied migrations exist
    python manage.py migrate --noinput
  env:
    DATABASE_URL: ${{ secrets.PROD_DATABASE_URL }}

The Safe Deployment Order

  1. Run migrate --check in CI — fail the build if there are unapplied migrations on the target branch
  2. Deploy the new code
  3. Run migrate --noinput as a pre-start hook in your container
  4. Health check passes only after migrations complete

Handling Backward-Incompatible Migrations

For migrations that drop columns or change types, use a two-phase approach:

  • Phase 1: Deploy code that works with both old and new schema
  • Phase 2: Apply the migration
  • Phase 3: Deploy code that uses only the new schema

Read the full guide with complete workflow YAML on Medium.

Need a team that can actually ship this?

NexForge combines AI development, product engineering, cloud delivery, and startup execution so ideas turn into production systems.