-
Notifications
You must be signed in to change notification settings - Fork 0
209 lines (179 loc) · 6.5 KB
/
build-and-deploy.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
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
name: Build and deploy
on:
push:
branches:
- master
tags:
- v**
pull_request:
branches:
- master
jobs:
test-backend:
name: Test backend
runs-on: ubuntu-latest
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v3
- name: Install python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10.x'
cache: 'pip'
cache-dependency-path: 'backend/requirements.txt'
- name: Install and execute 'tox'
run: |
pip install tox
tox
build-backend:
name: Build backend container
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build and push backend container image
uses: bCyberGmbH/build-container-image-action@main
with:
registry_name: docker.io
registry_username: ${{ secrets.DOCKER_HUB_USER }}
registry_password: ${{ secrets.DOCKER_HUB_PASS }}
image_repository: codeformuenster/muenster-jetzt-api
dockerfile_path: backend/deployment/Dockerfile.prod
build_context: backend
push_to_registry: ${{ github.event_name != 'pull_request' }}
- name: Build and push backend nginx container image
uses: bCyberGmbH/build-container-image-action@main
with:
registry_name: docker.io
registry_username: ${{ secrets.DOCKER_HUB_USER }}
registry_password: ${{ secrets.DOCKER_HUB_PASS }}
image_repository: codeformuenster/muenster-jetzt-api-static
dockerfile_path: backend/deployment/Dockerfile.prod
build_context: backend
build_target: staticfiles
push_to_registry: ${{ github.event_name != 'pull_request' }}
check-api-client-up-to-date:
name: Check if generated api client is up to date
runs-on: ubuntu-latest
needs: test-backend
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v3
with:
fetch-depth: '2' # for comparison
- name: Gather branch information
id: branch_info
continue-on-error: true
run: |
branch_name=${GITHUB_REF##*/}
short_sha=${GITHUB_SHA:0:7}
echo "branch_name=${branch_name}" >> $GITHUB_OUTPUT
echo "short_sha=${short_sha}" >> $GITHUB_OUTPUT
echo "We're running on ${branch_name} (${short_sha})"
if [ "$branch_name" = "master" ] || [ "$GITHUB_REF_TYPE" = "tag" ]; then
echo "changes=1" >> $GITHUB_OUTPUT
else
echo "changes=$(git diff --name-only HEAD^1 -- . | wc -l)" >> $GITHUB_OUTPUT
fi
- name: Install Node.js 14
if: steps.branch_info.outputs.changes != '0'
uses: actions/setup-node@v3
with:
node-version: 14
cache: 'npm'
cache-dependency-path: 'frontend/package-lock.json'
- name: Run 'npm ci' to install frontend dependencies
if: steps.branch_info.outputs.changes != '0'
run: |
npm ci
- name: Build backend image
if: steps.branch_info.outputs.changes != '0'
uses: docker/build-push-action@v5
with:
push: false
repository: muenster-jetzt-api
dockerfile: backend/deployment/Dockerfile.prod
tags: in-ci
path: backend
always_pull: true
cache_froms: ${{ steps.branch_info.outputs.cache_from_container_image }}
- name: Start container from backend image
if: steps.branch_info.outputs.changes != '0'
run: |
docker run -d --name backend -p 8000:8000 --env-file ../backend/.env.example muenster-jetzt-api:in-ci
- name: Run 'npm run generate-client'
if: steps.branch_info.outputs.changes != '0'
run: |
sleep 10
docker logs backend
npm run generate-client
- name: Check for changes according to git
if: steps.branch_info.outputs.changes != '0'
run: |
git checkout -- ../backend/deployment/Dockerfile.prod
git update-index --refresh
git diff-index --exit-code -p HEAD
frontend:
name: Validate, test and build frontend
runs-on: ubuntu-latest
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v3
with:
fetch-depth: '2' # for comparison
- name: Gather branch information
id: branch_info
continue-on-error: true
run: |
branch_name=${GITHUB_REF##*/}
short_sha=${GITHUB_SHA:0:7}
echo "branch_name=${branch_name}" >> $GITHUB_OUTPUT
echo "short_sha=${short_sha}" >> $GITHUB_OUTPUT
echo "We're running on ${branch_name} (${short_sha})"
echo "REACT_APP_VERSION=${branch_name}-${short_sha}" >> $GITHUB_ENV
if [ "$GITHUB_REF_TYPE" = "tag" ]; then
echo "REACT_APP_BACKEND_BASE_PATH=https://api.muenster-update.de" >> $GITHUB_ENV
else
echo "REACT_APP_BACKEND_BASE_PATH=https://api.staging.muenster-update.de" >> $GITHUB_ENV
fi
- name: Install Node.js 14
uses: actions/setup-node@v3
with:
node-version: 14
cache: 'npm'
cache-dependency-path: 'frontend/package-lock.json'
- name: Run 'npm ci' to install frontend dependencies
run: |
npm ci
- name: Check frontend code style
run: |
npm run lint
- name: Run frontend tests
run: |
npm run test
- name: Build frontend
run: |
echo "Executing npm run build with env REACT_APP_VERSION='$REACT_APP_VERSION' and REACT_APP_BACKEND_BASE_PATH='$REACT_APP_BACKEND_BASE_PATH'"
npm run build
- name: Precompress static files
run: |
find build \
-type f -regextype posix-extended \
-size +512c \
-iregex '.*\.(css|csv|html?|js|svg|txt|xml|json|webmanifest|ttf)' \
-exec gzip -9 -k '{}' \;
- name: Build and push frontend container image
uses: bCyberGmbH/build-container-image-action@main
with:
registry_name: docker.io
registry_username: ${{ secrets.DOCKER_HUB_USER }}
registry_password: ${{ secrets.DOCKER_HUB_PASS }}
image_repository: codeformuenster/muenster-jetzt-frontend
dockerfile_path: frontend/deployment/Dockerfile.prod
build_context: frontend
push_to_registry: ${{ github.event_name != 'pull_request' }}