Plantilla Acceso Libre 20 Feb, 2026

Template: GitHub Actions CI/CD para Django

Pipeline de CI/CD completo con GitHub Actions. Tests, linting, coverage, build de Docker y deploy automático a Railway/Render.

#github-actions #ci-cd #django #testing #devops

Contenido

CI/CD para Django con GitHub Actions

Pipeline completo que ejecuta tests, linting y deploy automático cada vez que haces push. Configúralo una vez y olvídate de deployar manualmente.

¿Qué hace este pipeline?

  • En cada PR: Ejecuta tests + linting (ruff) + coverage
  • En push a main: Todo lo anterior + deploy automático
  • PostgreSQL como servicio para tests realistas
  • Falla el PR si coverage baja de 80%

.github/workflows/ci.yml

name: CI/CD

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

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:16
        env:
          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@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: "pip"

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

      - name: Lint with ruff
        run: ruff check . --output-format=github

      - name: Run tests with coverage
        env:
          DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_db
          SECRET_KEY: test-secret-key-not-for-production
          DEBUG: "True"
          ALLOWED_HOSTS: "*"
        run: |
          coverage run manage.py test --verbosity=2
          coverage report --fail-under=80
          coverage xml

      - name: Upload coverage report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage.xml

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Railway
        uses: bervProject/railway-deploy@main
        with:
          railway_token: ${{ secrets.RAILWAY_TOKEN }}
          service: web

Configurar los secrets

En tu repo de GitHub: Settings → Secrets and variables → Actions

  • RAILWAY_TOKEN: Token de deploy de Railway (o tu plataforma)
  • Los secrets de la app (SECRET_KEY, DATABASE_URL, etc.) se configuran en Railway, no en GitHub Actions

Personalización común

# Agregar cache de pip para builds más rápidos (ya incluido arriba)
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: "pip"  # Cachea dependencias entre runs

# Ejecutar tests en paralelo (para suites grandes)
      - name: Run tests
        run: |
          pip install pytest-xdist
          pytest -n auto  # Usa todos los cores disponibles

# Notificación por Slack al fallar
      - name: Notify Slack on failure
        if: failure()
        uses: slackapi/slack-github-action@v1
        with:
          payload: |
            {"text": "❌ CI falló en ${{ github.repository }} - ${{ github.event.head_commit.message }}"}
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

Tips

  • Cache de pip reduce el tiempo de install de 45s a 5s
  • PostgreSQL service es mejor que SQLite para tests — descubre bugs de SQL que SQLite ignora
  • ruff es 10-100x más rápido que flake8 + isort + black combinados
  • Fail-under 80% en coverage evita que el equipo deje de escribir tests
  • Agrega --parallel a Django test si tienes >100 tests para acelerar

Recurso Externo

Este recurso incluye un enlace externo. Regístrate para acceder.

Inicia Sesión para Acceder

Únete a la Comunidad

Regístrate gratis para descargar archivos, guardar recursos en favoritos, ganar XP y acceder a cursos y el foro de la comunidad.

¿Ya tienes cuenta? Inicia sesión

Erik Taveras

Autor

Erik Taveras

Recursos Relacionados