name: CI/CD Pipeline on: push: branches: [ main, develop ] pull_request: branches: [ main ] env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: test: runs-on: ubuntu-latest services: postgres: image: postgres:15 env: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: test_stiftung ports: - 5432:5432 options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 redis: image: redis:7-alpine ports: - 6379:6379 options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5 steps: - uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.10' - name: Cache pip dependencies uses: actions/cache@v4 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles('app/requirements.txt') }} restore-keys: | ${{ runner.os }}-pip- - name: Install dependencies run: | sudo apt-get update sudo apt-get install -y postgresql-client python -m pip install --upgrade pip pip install -r app/requirements.txt - name: Set up environment run: | cp env-template.txt app/.env cd app echo "DJANGO_DEBUG=1" >> .env echo "DJANGO_SECRET_KEY=test-secret-key-for-ci" >> .env echo "POSTGRES_DB=test_stiftung" >> .env echo "POSTGRES_USER=postgres" >> .env echo "POSTGRES_PASSWORD=postgres" >> .env echo "DB_HOST=localhost" >> .env echo "DB_PORT=5432" >> .env echo "REDIS_URL=redis://localhost:6379/0" >> .env - name: Wait for PostgreSQL run: | while ! pg_isready -h localhost -p 5432 -U postgres; do echo "Waiting for PostgreSQL..." sleep 2 done echo "PostgreSQL is ready!" - name: Run migrations working-directory: ./app env: DJANGO_DEBUG: "1" DJANGO_SECRET_KEY: "test-secret-key-for-ci" POSTGRES_DB: "test_stiftung" POSTGRES_USER: "postgres" POSTGRES_PASSWORD: "postgres" DB_HOST: "localhost" DB_PORT: "5432" run: | python manage.py migrate - name: Run tests working-directory: ./app env: DJANGO_DEBUG: "1" DJANGO_SECRET_KEY: "test-secret-key-for-ci" POSTGRES_DB: "test_stiftung" POSTGRES_USER: "postgres" POSTGRES_PASSWORD: "postgres" DB_HOST: "localhost" DB_PORT: "5432" run: | python manage.py test - name: Check Django configuration working-directory: ./app env: DJANGO_DEBUG: "1" DJANGO_SECRET_KEY: "test-secret-key-for-ci" POSTGRES_DB: "test_stiftung" POSTGRES_USER: "postgres" POSTGRES_PASSWORD: "postgres" DB_HOST: "localhost" DB_PORT: "5432" run: | python manage.py check --deploy - name: Collect static files working-directory: ./app env: DJANGO_DEBUG: "1" DJANGO_SECRET_KEY: "test-secret-key-for-ci" POSTGRES_DB: "test_stiftung" POSTGRES_USER: "postgres" POSTGRES_PASSWORD: "postgres" DB_HOST: "localhost" DB_PORT: "5432" run: | python manage.py collectstatic --noinput build: needs: test runs-on: ubuntu-latest if: github.event_name == 'push' permissions: contents: read packages: write steps: - name: Checkout repository uses: actions/checkout@v4 - name: Log in to Container Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=ref,event=pr type=sha,prefix={{branch}}- - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: ./app push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} deploy: needs: build runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' && false # Disabled until production server is set up environment: production steps: - name: Deploy to production uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.PROD_HOST }} username: ${{ secrets.PROD_USERNAME }} key: ${{ secrets.PROD_SSH_KEY }} script: | cd /opt/stiftung git pull origin main docker compose -f docker-compose.prod.yml pull docker compose -f docker-compose.prod.yml up -d docker compose -f docker-compose.prod.yml exec web python manage.py migrate docker compose -f docker-compose.prod.yml exec web python manage.py collectstatic --noinput