Merge pull request #1 from Elektrokot/develop #39
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 CI | |
| on: [push, pull_request] | |
| jobs: | |
| lint: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.10' | |
| - name: Install flake8 | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install flake8 | |
| - name: Run Flake8 | |
| run: flake8 . | |
| test: | |
| runs-on: ubuntu-latest | |
| needs: lint | |
| services: | |
| postgres: | |
| image: postgres:16 | |
| env: | |
| POSTGRES_DB: testdb | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| options: >- | |
| --health-cmd pg_isready | |
| --health-interval 10s | |
| --health-timeout 5s | |
| --health-retries 5 | |
| ports: | |
| - 5432:5432 | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.10' | |
| - name: Cache pip | |
| uses: actions/cache@v3 | |
| with: | |
| path: ~/.cache/pip | |
| key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} | |
| restore-keys: ${{ runner.os }}-pip- | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| - name: Run migrations | |
| env: | |
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb | |
| run: python manage.py migrate | |
| - name: Run tests | |
| env: | |
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/testdb | |
| run: python manage.py test | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: test | |
| steps: | |
| - name: Check out code | |
| uses: actions/checkout@v3 | |
| - name: Log in to Docker Hub | |
| run: echo ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin | |
| - name: Build Docker image | |
| run: docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }} . | |
| - name: Push Docker image to Docker Hub | |
| run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }} | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| steps: | |
| - name: Set up SSH | |
| uses: webfactory/ssh-agent@v0.8.0 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_KEY_SERVER }} | |
| - name: Deploy to server | |
| run: | | |
| ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF' | |
| docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }} | |
| docker stop library || true | |
| docker rm library || true | |
| docker run -d --name library -p 80:8000 ${{ secrets.DOCKER_HUB_USERNAME }}/library:${{ github.sha }} | |
| EOF |