-
Notifications
You must be signed in to change notification settings - Fork 0
123 lines (109 loc) · 3.76 KB
/
foodgram.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
name: Foodgram CI/CD
on: [push]
env:
REPO_TAG: holohup/foodgram_backend:latest
FRONT_TAG: holohup/foodgram_frontend:latest
jobs:
code_tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:13.0-alpine
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v2
- name: Python Setup
uses: actions/setup-python@v2
with:
python-version: 3.7
cache: 'pip'
cache-dependency-path: '**/*requirements*.txt'
- name: Installing dependencies and testing scripts
run: |
python -m pip install --upgrade pip
pip install -r backend/requirements.txt
- name: Flake8 tests
run: python -m flake8
- name: Unit tests
run: cd backend && python manage.py test
env:
DB_HOST: localhost
build_image_and_push_to_docker_hub:
name: Build and Push Docker image to Docker Hub
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
needs: code_tests
steps:
- name: check out repo
uses: actions/checkout@v2
- name: log in to docker
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: build and push backend to DockerHub
uses: docker/build-push-action@v4
with:
context: backend
push: true
tags: ${{ env.REPO_TAG }}
- name: build and push frontend to DockerHub
uses: docker/build-push-action@v4
with:
context: .
file: frontend/Dockerfile
push: true
tags: ${{ env.FRONT_TAG }}
deploy:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
needs: build_image_and_push_to_docker_hub
steps:
- name: executing remote ssh commands to deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
script: |
docker-compose --project-directory foodgram/infra stop backend frontend
docker pull ${{ env.REPO_TAG }} && docker pull ${{ env.FRONT_TAG }}
echo '${{ secrets.DB_CREDENTIALS }}' > foodgram/infra/.env
docker-compose --project-directory foodgram/infra/ up -d
smoke_tests:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
needs: deploy
steps:
- name: check statuscodes
run: |
EXPECTED_401=$(curl --write-out '%{http_code}' -L --silent --output /dev/null http://${{ secrets.HOST }}/api/)
EXPECTED_200=$(curl --write-out '%{http_code}' -L --silent --output /dev/null http://${{ secrets.HOST }}/admin/)
if [[ $EXPECTED_401 != "401" || $EXPECTED_200 != "200" ]]; then
echo 'Something is not working'
exit 1
else
echo 'URLs return expected status codes'
fi
send_message:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
needs: smoke_tests
steps:
- name: send message
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_CHAT_ID }}
token: ${{ secrets.TELEGRAM_TOKEN }}
message: ${{ github.workflow }} успешно выполнен!