-
Notifications
You must be signed in to change notification settings - Fork 0
203 lines (186 loc) · 7.11 KB
/
foodgram_workflow.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: Foodgram Workflow
on:
push:
branches:
- '*'
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.9.20", "3.11"]
services:
postgres:
image: postgres:13.10
env:
POSTGRES_USER: foodgram_user
POSTGRES_PASSWORD: foodgram_password
POSTGRES_DB: foodgram
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8==6.0.0
pip install -r backend/requirements.txt
- name: Test with flake8 and django tests
env:
POSTGRES_USER: foodgram_user
POSTGRES_PASSWORD: foodgram_password
POSTGRES_DB: foodgram
DB_HOST: db
DB_PORT: 5432
run: |
cd backend
python -m flake8
build_and_push_to_docker_hub:
name: Push backend image to Docker Hub
runs-on: ubuntu-latest
needs: tests
if: github.ref == 'refs/heads/main'
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to Docker Hub
uses: docker/build-push-action@v4
with:
context: ./backend/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/foodgram_backend:latest
build_frontend_and_push_to_docker_hub:
name: Push frontend image to DockerHub
runs-on: ubuntu-latest
needs: tests
if: github.ref == 'refs/heads/main'
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to DockerHub
uses: docker/build-push-action@v4
with:
context: ./frontend/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/foodgram_frontend:latest
deploy:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
needs:
- build_and_push_to_docker_hub
- build_frontend_and_push_to_docker_hub
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Create project structure and copy files
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
# Создаем необходимые директории
mkdir -p foodgram/docs
mkdir -p foodgram/frontend
mkdir -p foodgram/infra
- name: Copy project files
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "infra/docker-compose.yml,infra/default.conf,docs/openapi-schema.yml,docs/redoc.html"
target: "foodgram/"
strip_components: 1
- name: Setup environment and deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd foodgram/
# Перемещаем файлы в нужные директории
mv openapi-schema.yml docs/
mv redoc.html docs/
mv docker-compose.yml infra/
mv default.conf infra/
# Создаем и настраиваем .env файл
cd infra/
sudo touch .env
echo DB_ENGINE=django.db.backends.postgresql >> .env
echo POSTGRES_DB=${{ secrets.DB_NAME }} >> .env
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
echo DB_HOST=db >> .env
echo DB_PORT=5432 >> .env
echo SECRET_KEY="${{ secrets.SECRET_KEY }}" >> .env
echo ALLOWED_HOSTS=${{ secrets.ALLOWED_HOSTS }} >> .env
echo DEBUG=False >> .env
# Запускаем контейнеры
sudo docker compose down
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/foodgram_backend:latest
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/foodgram_frontend:latest
sudo docker compose up -d
- name: Execute database commands
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd foodgram/infra/
# Ждем, пока база данных станет доступной
echo "Waiting for database to be ready..."
sleep 30
# Проверяем доступность базы данных
until sudo docker compose exec -T db pg_isready -U ${{ secrets.POSTGRES_USER }} -d ${{ secrets.DB_NAME }}; do
echo "Database is unavailable - sleeping"
sleep 2
done
echo "Database is up - executing commands"
# Выполняем миграции и другие команды
sudo docker compose exec -T backend python manage.py makemigrations
sudo docker compose exec -T backend python manage.py migrate
sudo docker compose exec -T backend python manage.py collectstatic --no-input
sudo docker compose exec -T backend python manage.py create_tags
sudo docker compose exec -T backend python manage.py load_ingredients data/ingredients.json
sudo docker compose exec -T backend python manage.py create_users
sudo docker compose exec -T backend python manage.py create_recipes
send_message:
runs-on: ubuntu-latest
needs: deploy
steps:
- name: Send message
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
message: |
Деплой Foodgram успешно завершен!
Коммит: ${{ github.event.head_commit.message }}
Ветка: ${{ github.ref }}
Автор: ${{ github.actor }}