-
Notifications
You must be signed in to change notification settings - Fork 0
208 lines (198 loc) · 7.49 KB
/
deploy.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
name: Deploy
on:
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_TAG: latest
FRONTEND_PATH: ./frontend
BACKEND_PATH: ./backend
REPORT_GENERATOR_PATH: ./report-generator
DOCKER_COMPOSE_FILE: docker-compose.prod.yml
jobs:
form-repository-lowercase-name:
runs-on: ubuntu-latest
outputs:
repository_name_lowercase: ${{ steps.formatting.outputs.lowercase }}
steps:
- name: Get repository name in lowercase
id: formatting
uses: ASzc/change-string-case-action@v5
with:
string: ${{ github.repository }}
build-and-push-frontend-image:
needs: [form-repository-lowercase-name]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image-name: ${{ steps.image-name.outputs.IMAGE_NAME }}
steps:
- name: Get repository code
uses: actions/checkout@v4
- name: Log in container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}
- name: Output Frontend Docker image name
id: image-name
run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.form-repository-lowercase-name.outputs.repository_name_lowercase }}-frontend:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT"
- name: Build and push Frontend Docker image
uses: docker/build-push-action@v5
with:
context: ${{ env.FRONTEND_PATH }}
file: ${{ env.FRONTEND_PATH }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }},${{ steps.image-name.outputs.IMAGE_NAME }}
labels: ${{ steps.meta.outputs.labels }}
build-and-push-backend-image:
needs: [form-repository-lowercase-name]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image-name: ${{ steps.image-name.outputs.IMAGE_NAME }}
steps:
- name: Get repository code
uses: actions/checkout@v4
- name: Log in container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}
- name: Output Backend Docker image name
id: image-name
run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.form-repository-lowercase-name.outputs.repository_name_lowercase }}-backend:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT"
- name: Build and push Backend Docker image
uses: docker/build-push-action@v5
with:
context: ${{ env.BACKEND_PATH }}
file: ${{ env.BACKEND_PATH }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }},${{ steps.image-name.outputs.IMAGE_NAME }}
labels: ${{ steps.meta.outputs.labels }}
build-and-push-report-gen-image:
needs: [form-repository-lowercase-name]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image-name: ${{ steps.image-name.outputs.IMAGE_NAME }}
steps:
- name: Get repository code
uses: actions/checkout@v4
- name: Log in container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}
- name: Output Report Geneator Docker image name
id: image-name
run: echo "IMAGE_NAME=${{ env.REGISTRY }}/${{ needs.form-repository-lowercase-name.outputs.repository_name_lowercase }}-report-gen:${{ env.IMAGE_TAG }}" >> "$GITHUB_OUTPUT"
- name: Build and push Report Geneator Docker image
uses: docker/build-push-action@v5
with:
context: ${{ env.REPORT_GENERATOR_PATH }}
file: ${{ env.REPORT_GENERATOR_PATH }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }},${{ steps.image-name.outputs.IMAGE_NAME }}
labels: ${{ steps.meta.outputs.labels }}
pull-frontend-image:
needs: [build-and-push-frontend-image]
runs-on: ubuntu-latest
steps:
- name: Pull latest Frontend Docker image
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
script: docker pull ${{ needs.build-and-push-frontend-image.outputs.image-name }}
pull-backend-image:
needs: [build-and-push-backend-image]
runs-on: ubuntu-latest
steps:
- name: Pull latest Backend Docker image
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
script: docker pull ${{ needs.build-and-push-backend-image.outputs.image-name }}
pull-report-gen-image:
needs: [build-and-push-report-gen-image]
runs-on: ubuntu-latest
steps:
- name: Pull latest Report Generator Docker image
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
script: docker pull ${{ needs.build-and-push-report-gen-image.outputs.image-name }}
deploy:
needs:
- build-and-push-frontend-image
- build-and-push-backend-image
- build-and-push-report-gen-image
- pull-frontend-image
- pull-backend-image
- pull-report-gen-image
runs-on: ubuntu-latest
steps:
- name: Get repository code
uses: actions/checkout@v4
- name: Stop docker compose
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
# Необходимо установить хоть какие-то значения, чтобы команда сработала
script: |
NOTISEND_TOKEN="-" \
FRONT_IMAGE="-" \
BACK_IMAGE="-" \
REPORT_GEN_IMAGE="-" \
docker compose -f /tmp/${{ env.DOCKER_COMPOSE_FILE }} down
- name: Copy docker compose to remote server
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
source: ${{ env.DOCKER_COMPOSE_FILE }}
target: /tmp
- name: Run docker compose
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
password: ${{ secrets.SERVER_PASSWORD }}
script: |
NOTISEND_TOKEN=${{ secrets.NOTISEND_TOKEN }} \
FRONT_IMAGE=${{ needs.build-and-push-frontend-image.outputs.image-name }} \
BACK_IMAGE=${{ needs.build-and-push-backend-image.outputs.image-name }} \
REPORT_GEN_IMAGE=${{ needs.build-and-push-report-gen-image.outputs.image-name }} \
docker compose -f /tmp/${{ env.DOCKER_COMPOSE_FILE }} up -d