Merge pull request #12 from newfatto/feature/hw_34_2 #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: DRF CI/CD | |
| on: | |
| push: | |
| branches: | |
| - develop | |
| pull_request: | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| services: | |
| postgres: | |
| image: postgres:16 | |
| env: | |
| POSTGRES_DB: learning_machine | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| ports: | |
| - 5432:5432 | |
| options: >- | |
| --health-cmd="pg_isready -U postgres -d learning_machine" | |
| --health-interval=10s | |
| --health-timeout=5s | |
| --health-retries=5 | |
| redis: | |
| image: redis:7 | |
| ports: | |
| - 6379:6379 | |
| env: | |
| SECRET_KEY: test-secret-key | |
| DEBUG: "True" | |
| ALLOWED_HOSTS: localhost,127.0.0.1 | |
| DATABASE_NAME: learning_machine | |
| DATABASE_USER: postgres | |
| DATABASE_PASSWORD: postgres | |
| DATABASE_HOST: localhost | |
| DATABASE_PORT: 5432 | |
| STRIPE_API_KEY: test-stripe-key | |
| CELERY_BROKER_URL: redis://localhost:6379/0 | |
| CELERY_RESULT_BACKEND: redis://localhost:6379/0 | |
| CELERY_TASK_ALWAYS_EAGER: "False" | |
| CELERY_TASK_EAGER_PROPAGATES: "False" | |
| EMAIL_HOST: smtp.example.com | |
| EMAIL_PORT: 465 | |
| EMAIL_HOST_USER: test@example.com | |
| EMAIL_HOST_PASSWORD: test-password | |
| EMAIL_USE_TLS: "False" | |
| EMAIL_USE_SSL: "True" | |
| DEFAULT_FROM_EMAIL: test@example.com | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v4 | |
| - name: Install Poetry | |
| run: pipx install poetry | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.10" | |
| cache: "poetry" | |
| - name: Install dependencies | |
| run: poetry install --no-interaction --no-root | |
| - name: Run migrations | |
| run: poetry run python manage.py migrate | |
| - name: Run tests | |
| run: poetry run python manage.py test | |
| lint: | |
| runs-on: ubuntu-latest | |
| needs: test | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v4 | |
| - name: Install Poetry | |
| run: pipx install poetry | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: "3.10" | |
| cache: "poetry" | |
| - name: Install dependencies | |
| run: poetry install --no-interaction --no-root | |
| - name: Run flake8 | |
| run: poetry run flake8 . --exclude=.venv,venv,migrations,htmlcov --max-line-length=119 | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: lint | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v4 | |
| - name: Build Docker image | |
| run: docker build -t learning-machine:test . | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/develop' | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v4 | |
| - name: Set up SSH | |
| uses: webfactory/ssh-agent@v0.9.0 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_KEY }} | |
| - name: Add server to known hosts | |
| run: | | |
| mkdir -p ~/.ssh | |
| ssh-keyscan ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts | |
| - name: Create deploy directory on server | |
| run: | | |
| ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} "mkdir -p ${{ secrets.DEPLOY_DIR }}" | |
| - name: Copy project files to server | |
| run: | | |
| rsync -az --delete \ | |
| --exclude '.git' \ | |
| --exclude '.github' \ | |
| --exclude '.env' \ | |
| --exclude '.venv' \ | |
| --exclude 'venv' \ | |
| --exclude '__pycache__' \ | |
| --exclude '.pytest_cache' \ | |
| --exclude 'htmlcov' \ | |
| --exclude 'media' \ | |
| --exclude 'staticfiles' \ | |
| ./ ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }}:${{ secrets.DEPLOY_DIR }}/ | |
| - name: Create .env on server | |
| run: | | |
| ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << EOF | |
| cd ${{ secrets.DEPLOY_DIR }} | |
| cat > .env << 'ENVEOF' | |
| ${{ secrets.ENV_FILE }} | |
| ENVEOF | |
| EOF | |
| - name: Deploy with Docker Compose | |
| run: | | |
| ssh ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF' | |
| cd ${{ secrets.DEPLOY_DIR }} | |
| docker compose -f docker-compose.prod.yml up -d db redis | |
| docker compose -f docker-compose.prod.yml build | |
| docker compose -f docker-compose.prod.yml run --rm web python manage.py migrate | |
| docker compose -f docker-compose.prod.yml run --rm web python manage.py collectstatic --noinput | |
| docker compose -f docker-compose.prod.yml up -d | |
| docker image prune -f | |
| EOF |