-
Notifications
You must be signed in to change notification settings - Fork 1
180 lines (171 loc) · 5.95 KB
/
ci.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
name: CI
on:
# Open any PR -> run tests
pull_request:
types: [opened, synchronize, reopened]
# Push to main branch -> run tests, build & deploy to test
push:
branches: [main]
# Release -> run tests, build & deploy to production
release:
types: [released]
env:
NODE_VERSION: 18.20.4
IMAGE_NAME: ${{ secrets.REGISTRY_ADDRESS }}/hanna
APP_VERSION: ${{ github.event_name == 'release' && github.event.release.name || format('build {0}', github.sha) }}
VITE_FEATURE_SAP_REPORTS: 'true'
jobs:
test-frontend:
name: Test frontend
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: ./frontend/package-lock.json
- name: Install shared dependencies
working-directory: shared
run: npm i
# Install backend dependencies to make tRPC types work
- name: Install backend dependencies
working-directory: backend
run: npm i
- name: Build & test
working-directory: frontend
run: npm i && npm test && npm run build
test-backend:
name: Test backend
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: ./backend/package-lock.json
- name: Install shared dependencies
working-directory: shared
run: npm i
- name: Build & test
working-directory: backend
run: npm i && npm test && npm run build
test-shared:
runs-on: ubuntu-latest
name: Test shared
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
cache-dependency-path: ./shared/package-lock.json
- name: Build & test
working-directory: shared
run: npm i && npm test && npm run build
test-e2e:
name: Test E2E
runs-on: ubuntu-latest
defaults:
run:
working-directory: e2e
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
cache: 'npm'
cache-dependency-path: ./e2e/package-lock.json
- run: npm i
- name: Get installed Playwright version
id: playwright-version
run: echo "version=$(npm ls @playwright/test --json | jq --raw-output '.dependencies["@playwright/test"].version')" >> $GITHUB_OUTPUT
- uses: actions/cache@v4
id: playwright-cache
with:
path: ~/.cache/ms-playwright
key: ${{ runner.os }}-playwright-${{ steps.playwright-version.outputs.version }}
restore-keys: |
${{ runner.os }}-playwright-
# Install Playwright dependencies unless found from the cache
- name: Install Playwright's dependencies
if: steps.playwright-cache.outputs.cache-hit != 'true'
run: npx playwright install --with-deps
# Start up the stack & wait for all services to be healthy
- run: CI=1 docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d --wait
timeout-minutes: 10
# Output container logs if any of the previous steps failed
- run: docker compose logs
if: failure()
# Execute the E2E tests
- run: npm test -- --workers=1
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_ADDRESS }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
# Push only with 'latest' tag for caching
- name: Build
uses: docker/build-push-action@v6
with:
push: true
build-args: |
"APP_VERSION=${{ env.APP_VERSION }}"
tags: ${{ env.IMAGE_NAME }}:latest
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:latest
cache-to: type=inline
push:
name: Push
# Run push only on pushes to main & when publishing a release
if: ${{ github.event_name == 'push' || github.event_name == 'release' }}
runs-on: ubuntu-latest
needs: [test-frontend, test-backend, test-shared, test-e2e, build]
env:
ENV_NAME: ${{ github.event_name == 'release' && 'production' || 'test' }}
APP_URL: ${{ github.event_name == 'release' && 'https://hanna.tampere.fi' || 'https://tre-hanna-test.azurewebsites.net' }}
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to registry
uses: docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_ADDRESS }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
# Rebuild the image using the "latest" tag as cache
- name: Build and push
uses: docker/build-push-action@v6
with:
push: true
build-args: |
"EXTRA_HOSTS=${{ secrets.IMAGE_EXTRA_HOSTS }}"
"APP_VERSION=${{ env.APP_VERSION }}"
"VITE_FEATURE_SAP_REPORTS=${{ env.VITE_FEATURE_SAP_REPORTS }}"
tags: ${{ env.IMAGE_NAME }}:${{ env.ENV_NAME }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:latest
cache-to: type=inline
# "Export" some environment variables for the deploy job
outputs:
env_name: ${{ env.ENV_NAME }}
app_url: ${{ env.APP_URL }}
deploy:
name: Deploy
needs: push
runs-on: ubuntu-latest
environment:
name: ${{ needs.push.outputs.env_name }}
url: ${{ needs.push.outputs.app_url }}
steps:
- name: Deploy
run: curl --fail -X POST '${{ secrets.DEPLOY_WEBHOOK_URL }}'