-
Notifications
You must be signed in to change notification settings - Fork 0
186 lines (174 loc) · 5.89 KB
/
main.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
name: Main Foodgram workflow
on:
push:
branches:
- main
jobs:
# frontend_tests:
# runs-on: ubuntu-22.04
# steps:
# - uses: actions/checkout@v4
# - name: Set up nodeJS
# uses: actions/setup-node@v4
# with:
# node-version: 16
# - name: Install dependencies
# run: |
# cd frontend/
# npm ci
# - name: Test frontend
# run: |
# cd frontend/
# npm run test
backend_tests:
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:14.11
env:
POSTGRES_USER: django_user
POSTGRES_PASSWORD: django_password
POSTGRES_DB: django_db
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- name: Check out repo code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.10.12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8==7.0.0
pip install -r ./backend/requirements.txt
- name: Test with flake8 and other django tests
env:
POSTGRES_USER: django_user
POSTGRES_PASSWORD: django_password
POSTGRES_DB: django_db
# The DB server been started under Docker, with its port piped to
# the host, => we're connecting to 127.0.0.1:5432
DB_HOST: 127.0.0.1
DB_PORT: 5432
run: |
python -m flake8 backend/
cd backend/
pytest
build_and_push_backend_to_docker_hub:
name: Build & push FG backend Docker img to DockerHub
runs-on: ubuntu-22.04
# needs: [backend_tests, frontend_tests]
needs: backend_tests
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to DockerHub
uses: docker/build-push-action@v5
with:
context: ./backend/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/foodgram_backend:latest
build_and_push_frontend_to_docker_hub:
name: Build & push FG frontend Docker img to DockerHub
runs-on: ubuntu-22.04
# needs: frontend_tests
steps:
- name: Check out the repo
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push to DockerHub
uses: docker/build-push-action@v5
with:
context: ./frontend/
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/foodgram_frontend:latest
deploy:
runs-on: ubuntu-22.04
needs:
- build_and_push_backend_to_docker_hub
- build_and_push_frontend_to_docker_hub
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Copy docker-compose.production.yaml via ssh
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "./infra/docker-compose.production.yaml"
target: "foodgram"
- name: Copy nginx.conf via ssh
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "./infra/nginx.conf"
target: "foodgram"
- name: Copy doc folder via ssh
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "./docs"
target: "foodgram"
- name: Execute remote ssh commands to deploy(1)
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd foodgram
cp infra/docker-compose.production.yaml .
cp infra/nginx.conf .
docker compose -f docker-compose.production.yaml --progress=quiet pull
docker compose -f docker-compose.production.yaml --progress=quiet down -v
docker compose -f docker-compose.production.yaml --progress=quiet up -d
- name: Execute remote ssh commands to deploy(2)
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USER }}
key: ${{ secrets.SSH_KEY }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
cd foodgram
docker compose -f docker-compose.production.yaml exec backend python manage.py migrate
docker compose -f docker-compose.production.yaml exec backend python manage.py collectstatic
docker compose -f docker-compose.production.yaml exec backend python manage.py loaddata db.json
docker compose -f docker-compose.production.yaml exec backend python manage.py import_csv eng
rm -r infra
rm nginx.conf
send_telegram:
runs-on: ubuntu-22.04
needs: deploy
steps:
- name: Send a telegram to mark the end of the deployment
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
message: Foodgram been refreshed in builds & deployed OK to WebSpace.