-
Notifications
You must be signed in to change notification settings - Fork 21
132 lines (118 loc) · 4.6 KB
/
deploy_prod.yaml
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
name: Prod backend build and deploy
on:
pull_request:
branches:
- develop
# types: [ closed ]
env:
REGISTRY: ghcr.io
REP_OWNER: studio-yandex-practicum
IMAGE_NAME: lomaya_baryery_backend
DEPLOY_PATH: /lombarye/full
WORK_DIR: /LOMAYA_BARYERY_BACKEND
HEALTHCHECK_URL: https://бот.ломаябарьеры.рф/api/healthcheck
defaults:
run:
working-directory: .
jobs:
build_and_push_docker_image:
name: Push Docker image to GitHub Packages
# if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
environment:
name: prod_deploy
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Docker login
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: Dockerfile
labels: runnumber=${GITHUB_RUN_ID}
push: true
tags: |
${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:prod,
${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
start_application:
name: Start application
# if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
needs: build_and_push_docker_image
environment:
name: prod_deploy
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Copy docker-compose and nginx files
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
source: "docker-compose.prod.yaml,nginx/default.prod.conf"
target: ${{ env.DEPLOY_PATH }}
- name: Create .env file
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd ${{ env.DEPLOY_PATH }}
mv docker-compose.prod.yaml docker-compose.yaml
mv nginx/default.prod.conf nginx/default.conf
rm -f .env
touch .env
echo POSTGRES_DB=${{ secrets.POSTGRES_DB }} >> .env
echo POSTGRES_USER=${{ secrets.POSTGRES_USER }} >> .env
echo POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }} >> .env
echo DB_HOST=${{ secrets.DB_HOST }} >> .env
echo DB_PORT=${{ secrets.DB_PORT }} >> .env
echo BOT_TOKEN=${{ secrets.TELEGRAM_BOT_TOKEN }} >> .env
echo BOT_WEBHOOK_MODE=${{ secrets.BOT_WEBHOOK_MODE }} >> .env
echo APPLICATION_URL=${{ secrets.APPLICATION_URL }} >> .env
echo HEALTHCHECK_API_URL=${{ secrets.HEALTHCHECK_API_URL }} >> .env
echo MAIL_LOGIN=${{ secrets.MAIL_LOGIN }} >> .env
echo MAIL_PASSWORD=${{ secrets.MAIL_PASSWORD }} >> .env
echo ORGANIZATIONS_EMAIL=${{ secrets.ORGANIZATIONS_EMAIL }} >> .env
echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env
echo ROOT_PATH=${{ secrets.ROOT_PATH }} >> .env
echo TZ=${{ secrets.TZ }} >> .env
echo DEBUG=${{ secrets.DEBUG }} >> .env
- name: Start application
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd ${{ env.DEPLOY_PATH }}
docker image prune -f
docker pull ${{ env.REGISTRY }}/${{ env.REP_OWNER }}/${{ env.IMAGE_NAME }}:prod
docker compose --file docker-compose.yaml stop
docker compose --file docker-compose.yaml rm backend
docker compose --file docker-compose.yaml up -d
- name: Making migrations
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd ${{ env.DEPLOY_PATH }}
docker compose exec -T -w ${{ env.WORK_DIR }} backend alembic upgrade head
- name: Sleep for 30 seconds
run: sleep 30s
shell: bash
- name: Get the status of healthcheck
uses: im-open/[email protected]
with:
url: ${{ env.HEALTHCHECK_URL }}
fail-on-bad-status: true