reupload #2
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
| name: Django Supabase App CI/CD | |
| on: | |
| push: | |
| branches: [main, staging] | |
| pull_request: | |
| branches: [main, staging] | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| services: | |
| redis: | |
| image: redis:7-alpine | |
| ports: | |
| - 6379:6379 | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.11' | |
| cache: 'pipenv' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install pipenv | |
| pipenv install --dev | |
| - name: Run tests | |
| run: | | |
| cd backend | |
| pipenv run python manage.py test apps.supabase | |
| env: | |
| DJANGO_DEBUG: "true" | |
| DJANGO_SECRET_KEY: "test-secret-key-for-ci" | |
| SUPABASE_URL: ${{ secrets.SUPABASE_URL }} | |
| SUPABASE_ANON_KEY: ${{ secrets.SUPABASE_ANON_KEY }} | |
| build: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Set environment based on branch | |
| id: set-env | |
| run: | | |
| if [[ ${{ github.ref }} == 'refs/heads/main' ]]; then | |
| echo "env=production" >> $GITHUB_OUTPUT | |
| else | |
| echo "env=staging" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| file: ./docker/Dockerfile.prod | |
| push: true | |
| tags: | | |
| ghcr.io/${{ github.repository }}:${{ steps.set-env.outputs.env }} | |
| ghcr.io/${{ github.repository }}:${{ github.sha }} | |
| cache-from: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache | |
| cache-to: type=registry,ref=ghcr.io/${{ github.repository }}:buildcache,mode=max | |
| env: | |
| DOCKER_BUILDKIT: 1 | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' | |
| steps: | |
| - name: Set environment based on branch | |
| id: set-env | |
| run: | | |
| if [[ ${{ github.ref }} == 'refs/heads/main' ]]; then | |
| echo "env=production" >> $GITHUB_OUTPUT | |
| echo "deploy_url=${{ secrets.PRODUCTION_DEPLOY_URL }}" >> $GITHUB_OUTPUT | |
| else | |
| echo "env=staging" >> $GITHUB_OUTPUT | |
| echo "deploy_url=${{ secrets.STAGING_DEPLOY_URL }}" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Create database backup | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.DEPLOY_HOST }} | |
| username: ${{ secrets.DEPLOY_USER }} | |
| key: ${{ secrets.DEPLOY_SSH_KEY }} | |
| script: | | |
| cd ${{ secrets.DEPLOY_PATH }} | |
| docker-compose exec -T postgres pg_dump -U postgres -d postgres -f /backups/backup-$(date +%Y%m%d%H%M%S).sql | |
| - name: Deploy to server | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.DEPLOY_HOST }} | |
| username: ${{ secrets.DEPLOY_USER }} | |
| key: ${{ secrets.DEPLOY_SSH_KEY }} | |
| script: | | |
| cd ${{ secrets.DEPLOY_PATH }} | |
| docker pull ghcr.io/${{ github.repository }}:${{ steps.set-env.outputs.env }} | |
| docker-compose -f docker-compose.${{ steps.set-env.outputs.env }}.yml up -d | |
| docker image prune -f | |
| - name: Run database migrations | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.DEPLOY_HOST }} | |
| username: ${{ secrets.DEPLOY_USER }} | |
| key: ${{ secrets.DEPLOY_SSH_KEY }} | |
| script: | | |
| cd ${{ secrets.DEPLOY_PATH }} | |
| docker-compose -f docker-compose.${{ steps.set-env.outputs.env }}.yml exec -T backend python manage.py migrate | |
| - name: Send deployment status notification | |
| if: always() | |
| uses: slackapi/[email protected] | |
| with: | |
| payload: | | |
| { | |
| "text": "Deployment to ${{ steps.set-env.outputs.env }} ${{ job.status == 'success' && 'succeeded' || 'failed' }}!", | |
| "blocks": [ | |
| { | |
| "type": "section", | |
| "text": { | |
| "type": "mrkdwn", | |
| "text": "*Deployment to ${{ steps.set-env.outputs.env }} ${{ job.status == 'success' && 'succeeded' || 'failed' }}!*\n${{ steps.set-env.outputs.deploy_url }}" | |
| } | |
| }, | |
| { | |
| "type": "context", | |
| "elements": [ | |
| { | |
| "type": "mrkdwn", | |
| "text": "*Commit:* <${{ github.event.repository.html_url }}/commit/${{ github.sha }}|${{ github.sha }}>" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| env: | |
| SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
| SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK |