287 lines (266 loc) · 10.8 KB
/
ci.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# from https://github.com/marketplace/actions/install-poetry-action
name: build and deploy to server
env:
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1
on:
push:
branches: [ "master", "main" ]
paths-ignore: [ "docs/**" ]
workflow_dispatch:
concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
linter:
name: Linter
runs-on: ubuntu-22.04
steps:
#----------------------------------------------
# check-out repo and set-up python
#----------------------------------------------
- name: Check out repository
uses: actions/checkout@v4
- name: Set up python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.11'
#----------------------------------------------
# ----- install & configure poetry -----
#----------------------------------------------
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
#----------------------------------------------
# install dependencies if cache does not exist
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
#----------------------------------------------
# install your root project, if required
#----------------------------------------------
- name: Install project
run: poetry install --no-interaction
#----------------------------------------------
# Run poetry
#----------------------------------------------
- name: Run pre-commit
uses: pre-commit/action@v3.0.0
test:
name: Test
runs-on: ubuntu-22.04
steps:
#----------------------------------------------
# check-out repo and set-up python
#----------------------------------------------
- name: Check out repository
uses: actions/checkout@v4
- name: Set up python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.11'
#----------------------------------------------
# ----- install & configure poetry -----
#----------------------------------------------
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
#----------------------------------------------
# install dependencies if cache does not exist
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root
#----------------------------------------------
# install your root project, if required
#----------------------------------------------
- name: Install project
run: poetry install --no-interaction
#----------------------------------------------
# run test suite
#----------------------------------------------
- name: Run tests
run: |
source .venv/bin/activate
coverage run --source='.' manage.py test
coverage report
coverage xml
#----------------------------------------------
# upload coverage stats
# (requires CODECOV_TOKEN in repository secrets)
#----------------------------------------------
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # Only required for private repositories
file: ./coverage.xml
fail_ci_if_error: true
build:
name: Build
runs-on: ubuntu-22.04
needs: [test, linter]
steps:
- name: Checkout
uses: actions/checkout@v4
#----------------------------------------------
# Prepare commit info for deploy
#----------------------------------------------
- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v4
#----------------------------------------------
# Extact commit info for build
#----------------------------------------------
- name: Docker meta
uses: docker/metadata-action@v5
id: meta
with:
images: ${{ env.GITHUB_REPOSITORY }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=raw,value=latest,enable={{is_default_branch}}
#----------------------------------------------
# Prepare for building image
#----------------------------------------------
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
id: buildx
#----------------------------------------------
# Auth to docker hub
#----------------------------------------------
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
#----------------------------------------------
# Build and upload image
#----------------------------------------------
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: deploy/django/Dockerfile
push: true
ulimit: nofile=1048576:1048576
builder: ${{ steps.buildx.outputs.name }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
name: Deploy
runs-on: ubuntu-22.04
needs: build
environment:
name: production
steps:
#----------------------------------------------
# Copy docker compose production config
#----------------------------------------------
- name: Checkout
uses: actions/checkout@v4
#----------------------------------------------
# Prepare commit info for deploy
#----------------------------------------------
- name: Inject slug/short variables
uses: rlespinasse/github-slug-action@v4
#----------------------------------------------
# Copy configs to server
#----------------------------------------------
- name: copy configs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
source: "deploy/docker_compose.prod.yml,deploy/nginx.conf,deploy/crontab.conf"
target: "pythondigest/"
#----------------------------------------------
# Run docker compose
#----------------------------------------------
- name: executing remote ssh commands
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
port: ${{ secrets.SSH_PORT }}
passphrase: ${{ secrets.SSH_PASSPHRASE }}
script: |
export DJANGO_SECRET_KEY="${{ secrets.DJANGO_SECRET_KEY }}"
export REDIS_URL="${{ secrets.REDIS_URL }}"
export POSTGRES_HOST="${{ secrets.POSTGRES_HOST }}"
export POSTGRES_PORT="${{ secrets.POSTGRES_PORT }}"
export POSTGRES_DB="${{ secrets.POSTGRES_DB }}"
export POSTGRES_USER="${{ secrets.POSTGRES_USER }}"
export POSTGRES_PASSWORD="${{ secrets.POSTGRES_PASSWORD }}"
export SENTRY_DSN="${{ secrets.SENTRY_DSN }}"
export SENTRY_ENVIRONMENT="${{ secrets.SENTRY_ENVIRONMENT }}"
export BASE_DOMAIN="${{ secrets.BASE_DOMAIN }}"
export USE_DOCKER="${{ secrets.USE_DOCKER }}"
export GITTER_TOKEN="${{ secrets.GITTER_TOKEN }}"
export TWITTER_CONSUMER_KEY="${{ secrets.TWITTER_CONSUMER_KEY }}"
export TWITTER_CONSUMER_SECRET="${{ secrets.TWITTER_CONSUMER_SECRET }}"
export TWITTER_TOKEN="${{ secrets.TWITTER_TOKEN }}"
export TWITTER_TOKEN_SECRET="${{ secrets.TWITTER_TOKEN_SECRET }}"
export TGM_BOT_ACCESS_TOKEN="${{ secrets.TGM_BOT_ACCESS_TOKEN }}"
export TGM_CHANNEL="${{ secrets.TGM_CHANNEL }}"
export IFTTT_MAKER_KEY="${{ secrets.IFTTT_MAKER_KEY }}"
export VK_APP_ID="${{ secrets.VK_APP_ID }}"
export VK_LOGIN="${{ secrets.VK_LOGIN }}"
export VK_PASSWORD="${{ secrets.VK_PASSWORD }}"
# image tag
export COMMIT_TAG=${{env.GITHUB_REF_SLUG}}
cd ~/pythondigest/deploy/
# deploy app
docker compose -f docker_compose.prod.yml pull
docker compose -f docker_compose.prod.yml up -d
# prepare static folders for serve
cd ~/pythondigest/deploy/
sudo usermod -a -G pythondigest www-data
sudo chown -R :www-data static
sudo chown -R :www-data media
# make link for serve django-remdow links:
# django-remdow download external image to local png file
# if original file is jpeg - download it and create symlink to png file
# this commands create link outside container to this symlinks
sudo mkdir -p /app/static/remdow/
sudo ln -s /home/pythondigest/pythondigest/deploy/static/remdow/img /app/static/remdow/img
# validate and reload nginx
sudo mkdir -p /var/log/nginx/pythondigest/
sudo nginx -t && sudo service nginx reload
# update crontab
crontab < crontab.conf