· 4 years ago · Mar 19, 2021, 09:02 AM
1name: YaMDB
2
3on: [push]
4
5jobs:
6 tests:
7 name: Test with flake8 and pytest
8 runs-on: ubuntu-latest
9
10 steps:
11 - uses: actions/checkout@v2
12 - name: Python setup
13 uses: actions/setup-python@v2
14 with:
15 python-version: 3.8
16 - name: Install requirements.txt
17 run: pip install -r requirements.txt
18 - name: Test with flake8
19 run: flake8 .
20 - name: Test with pytest
21 run: pytest
22
23 build_and_push_to_docker_hub:
24 name: Push Docker image to Docker Hub
25 runs-on: ubuntu-latest
26 needs: tests
27 steps:
28 - name: Check out the repo
29 uses: actions/checkout@v2
30 - name: Set up Docker Buildx
31 uses: docker/setup-buildx-action@v1
32 - name: Login to Docker
33 uses: docker/login-action@v1
34 with:
35 username: ${{ secrets.DOCKER_USERNAME }}
36 password: ${{ secrets.DOCKER_PASSWORD }}
37 - name: Push to Docker Hub
38 uses: docker/build-push-action@v2
39 with:
40 push: true
41 tags: ${{ secrets.DOCKER_USERNAME }}/api_yamdb:latest
42
43 deploy:
44 name: Deploy
45 runs-on: ubuntu-latest
46 needs: build_and_push_to_docker_hub
47 if: github.ref == 'refs/heads/master'
48
49 steps:
50 - uses: actions/checkout@v2
51
52 - name: Copy files from the repository
53 uses: appleboy/scp-action@master
54 with:
55 host: ${{ secrets.HOST }}
56 username: ${{ secrets.USER }}
57 key: ${{ secrets.SSH_KEY }}
58 source: "docker-compose.yaml, nginx/"
59 target: "~/api_yamdb"
60
61 - name: executing remote ssh commands to deploy
62 uses: appleboy/ssh-action@master
63 with:
64 host: ${{ secrets.HOST }}
65 username: ${{ secrets.USER }}
66 key: ${{ secrets.SSH_KEY }}
67 script: |
68 cd api_yamdb
69 sudo touch .env
70 export SECRET_KEY=${{ secrets.SECRET_KEY }}
71 export DB_ENGINE=${{ secrets.DB_ENGINE }}
72 export DB_NAME=${{ secrets.DB_NAME }}
73 export POSTGRES_USER=${{ secrets.POSTGRES_USER }}
74 export POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}
75 export DB_HOST=${{ secrets.DB_HOST }}
76 export DB_PORT=${{ secrets.DB_PORT }}
77 docker-compose stop
78 docker-compose up -d --force-recreate --build
79 docker-compose exec web python manage.py migrate --noinput
80 docker-compose exec web python manage.py collectstatic --no-input
81
82 send_message:
83 name: Report to Telegram
84 runs-on: ubuntu-latest
85 needs: deploy
86
87 steps:
88 - name: Send message to Telegram
89 uses: appleboy/telegram-action@master
90 with:
91 to: ${{ secrets.TELEGRAM_TO }}
92 token: ${{ secrets.TELEGRAM_TOKEN }}
93 message: ${{ github.workflow }} completed successfully!
94