Django CI Tests #321
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # .github/workflows/django-ci.yml | |
| name: Django CI Tests | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| schedule: | |
| - cron: '0 3 * * *' | |
| jobs: | |
| build-and-test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository code | |
| uses: actions/checkout@v4 | |
| - name: Log in to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create .env file for testing | |
| run: | | |
| echo "POSTGRES_DB=testdb" > .env | |
| echo "POSTGRES_USER=testuser" >> .env | |
| echo "POSTGRES_PASSWORD=testpass" >> .env | |
| echo "POSTGRES_HOST=db" >> .env | |
| echo "POSTGRES_PORT=5432" >> .env | |
| echo "DJANGO_SECRET_KEY=dummy-secret-key-for-ci" >> .env | |
| echo "DJANGO_DEBUG=1" >> .env | |
| echo "DJANGO_ALLOWED_HOSTS=*" >> .env | |
| - name: Create Docker Compose override file for CI | |
| run: | | |
| cat <<EOF > docker-compose.override.yml | |
| networks: | |
| proxy: | |
| external: false | |
| EOF | |
| - name: Build and start services | |
| run: docker compose up --build -d | |
| - name: Wait for database to be ready | |
| run: | | |
| echo "Waiting for PostgreSQL to start..." | |
| for i in {1..20}; do | |
| if docker compose exec db pg_isready -U "testuser" -d "testdb" -h "db" -q; then | |
| echo "✅ PostgreSQL is ready." | |
| exit 0 | |
| fi | |
| echo "Attempt $i: PostgreSQL not ready yet. Waiting..." | |
| sleep 3 | |
| done | |
| echo "❌ PostgreSQL did not become ready in time." | |
| exit 1 | |
| - name: Run Django tests | |
| run: docker compose exec app python manage.py test |