-
Notifications
You must be signed in to change notification settings - Fork 18
245 lines (213 loc) · 7.63 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
name: Build & test
on:
pull_request:
paths:
- "backend/**"
- "frontend/**"
- "docs/docs/setup/config.toml"
- "util/dev-config/*"
- ".deployment/templates/config.toml"
- "util/dummy-login/dist/index.js"
- ".github/workflows/ci.yml"
- ".github/workflows/deploy.yml"
push:
branches:
- "*"
tags-ignore:
- "*"
env:
CARGO_TERM_COLOR: always
RUSTFLAGS: --deny warnings
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
# Figure out build mode
- name: Determine build mode
run: |
target_dir="target"
if (( "$(date +%w)" % 2 == 0 )); then
sudo apt install -y musl-dev musl-tools
rustup target add x86_64-unknown-linux-musl
printf -- "--target=x86_64-unknown-linux-musl " >> .cargo-flags
printf "musl" >> .cache-key
target_dir="${target_dir}/x86_64-unknown-linux-musl"
else
printf "gnu" >> .cache-key
fi
if [[ "$GITHUB_REPOSITORY" == "elan-ev/tobira" ]] && [ "$GITHUB_REF" == "refs/heads/master" ]; then
printf -- "--profile=release-ci" >> .cargo-flags
printf -- "-release" >> .cache-key
echo "ci_targetdir=${target_dir}/release-ci" >> $GITHUB_ENV
echo "ci_webpack_flags=production" >> $GITHUB_ENV
else
printf -- "--features=embed-in-debug" >> .cargo-flags
printf -- "-dev" >> .cache-key
echo "ci_targetdir=${target_dir}/debug" >> $GITHUB_ENV
echo "ci_webpack_flags=development" >> $GITHUB_ENV
fi
echo "cache_key=$(cat .cache-key)" >> $GITHUB_ENV
- name: Restore backend cache
uses: Swatinem/rust-cache@v2
with:
shared-key: '${{ env.cache_key }}'
workspaces: backend
# Frontend cache: only the NPM folder is cached, not the node_modules, as
# recommended here: https://github.com/actions/cache/blob/main/examples.md#node---npm
- name: Restore NPM cache
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('frontend/package-lock.json') }}
# ----- Build the frontend -----
# We also lint and typecheck, as it is convenient and fastest to do here.
- name: Installing frontend dependencies (npm ci)
working-directory: frontend
run: npm ci
- name: Generate GraphQL query types
working-directory: frontend
run: npx relay-compiler
- name: Lint frontend
working-directory: frontend
run: npx eslint --max-warnings 0 .
- name: Build frontend
working-directory: frontend
run: npx webpack --mode=${{ env.ci_webpack_flags }}
- name: Typecheck frontend
working-directory: frontend
run: npx tsc
# ----- Build the backend -----
- name: Build backend
working-directory: backend
run: cargo build $(cat ../.cargo-flags)
# ----- Prepare binary -----
- name: Move Tobira binary
run: mv backend/${{ env.ci_targetdir }}/tobira tobira
- name: Compress Tobira binary
run: objcopy --compress-debug-sections tobira
# Prepare the ID (used in the subdomain) for deployment. This has to be done
# here because in the `deploy` workflow, we don't have access to the correct
# `GITHUB_REF` anymore.
- name: Write deploy ID to file
run: ./.deployment/deploy-id.sh "$GITHUB_REF" > .deploy-id
# Archive files to be used in the `deploy` workflow
- name: Upload binary and deployment files as artifact
uses: actions/upload-artifact@v4
with:
name: test-deployment-files
retention-days: 1
path: |
tobira
util/dev-config/logo-large.svg
util/dev-config/logo-small.svg
util/dev-config/logo-large-dark.svg
util/dev-config/favicon.svg
.deploy-id
.cache-key
.cargo-flags
.deployment/templates/config.toml
util/dummy-login/dist/index.js
# This is uploaded for the test job, specifically `cargo test`
- name: Upload frontend artifacts
uses: actions/upload-artifact@v4
with:
name: frontend-artifacts
retention-days: 1
path: frontend/build
# --------------------------------------------------------------------------
test:
runs-on: ubuntu-20.04
needs: build
services:
postgres:
image: postgres:12
env:
POSTGRES_USER: tobira
POSTGRES_PASSWORD: tobira
POSTGRES_DB: tobira
ports:
- 5432:5432
options: '--name tobira_pg'
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v4
with:
name: test-deployment-files
- uses: actions/download-artifact@v4
with:
name: frontend-artifacts
path: ./frontend/build
- run: chmod +x tobira
# Perform simple checks
- name: Make sure `schema.graphql` is up to date
run: ./tobira export-api-schema | diff -u --color=always - frontend/src/schema.graphql
- name: Make sure `docs/docs/setup/config.toml` is up to date
run: ./tobira write-config | diff -u --color=always - docs/docs/setup/config.toml
# Run backend tests
- name: Read cache key
run: echo "cache_key=$(cat .cache-key)" >> $GITHUB_ENV
- name: Install MUSL dependencies
if: startsWith(env.cache_key, 'musl')
run: |
sudo apt install -y musl-dev musl-tools
rustup target add x86_64-unknown-linux-musl
- name: Restore backend cache
uses: Swatinem/rust-cache@v2
with:
shared-key: '${{ env.cache_key }}'
workspaces: backend
save-if: false
- name: Test backend
working-directory: backend
run: cargo test $(cat ../.cargo-flags)
# Test DB migrations
- name: Download latest DB dump
run: curl --silent --output db-dump.xz -L https://github.com/elan-ev/tobira/raw/db-dumps/db-dump-latest.xz
- name: Decompress DB dump
run: xz -d db-dump.xz
# We need to use the same version as the DB, so we use 'docker exec'
- name: Restore DB dump
run: |
docker exec -i tobira_pg pg_restore \
--dbname postgresql://tobira:tobira@localhost/postgres \
--clean \
--create \
--if-exists \
< db-dump || true
- name: Run migrations
run: ./tobira db migrate --config util/dev-config/config.toml
# Run UI Playwright tests
- name: Start docker containers
working-directory: util/containers
run: |
docker-compose -f docker-compose.yml up -d \
tobira-auth-proxy \
tobira-login-handler \
tobira-meilisearch
- name: Rebuild search index
run: ./tobira search-index update --config util/dev-config/config.toml
- name: Install Playwright
working-directory: frontend
run: npm i @playwright/test
- name: Install Playwright browsers
working-directory: frontend
run: npx playwright install --with-deps
- name: Start Tobira for playwright tests
uses: JarvusInnovations/background-action@v1
with:
run: ./tobira serve --config util/dev-config/config.toml &
wait-on: http://localhost:3090
- name: Run playwright tests
working-directory: frontend
run: npx playwright test
- name: Upload test results
# A test might need a retry to succeed or run longer than expected.
# In these cases the results should also be saved, since they might
# indicate what went wrong. Hence `always()` instead of on `failure()`.
if: always()
uses: actions/upload-artifact@v3
with:
name: playwright-report
path: frontend/playwright-report/
retention-days: 7