forked from CybercentreCanada/clue
-
Notifications
You must be signed in to change notification settings - Fork 0
376 lines (346 loc) · 11.2 KB
/
Copy pathapi-workflow.yml
File metadata and controls
376 lines (346 loc) · 11.2 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
name: Clue API
on:
push:
branches:
- main
- develop
- rc/*
- patch/*
paths:
- "api/**"
- "!api/LICENSE"
- "!api/README.md"
- "!api/docs/**"
- "!api/build_scripts/generate_classes.py"
- .github/workflows/api-workflow.yml
tags:
- v*
pull_request:
branches:
- main
- develop
- rc/*
- patch/*
paths:
- "api/**"
- "!api/LICENSE"
- "!api/README.md"
- "!api/docs/**"
- "!api/build_scripts/generate_classes.py"
- .github/workflows/api-workflow.yml
- "!api/plugin/**"
workflow_dispatch: {}
permissions:
contents: read
pull-requests: write
jobs:
formatting:
name: Validate Clue API Code Quality
runs-on: ubuntu-latest
defaults:
run:
working-directory: api
steps:
- name: Checkout develop
uses: actions/checkout@v4
with:
ref: develop
- name: Checkout current ref
uses: actions/checkout@v4
- name: Setup Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Poetry
run: python -m pip install poetry
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./api/.venv
key: venv-${{ hashFiles('api/poetry.lock') }}
- name: Check for poetry config integrity
run: |
poetry --version
poetry env use 3.12
poetry check
poetry env info
- name: Install Dependencies
run: |
poetry install --verbose --with test
- name: Run ruff formatting checks
run: |
poetry run ruff format clue --diff
- name: Run ruff linter checks
run: |
poetry run ruff check clue --output-format=github
- name: Run type checking
run: |
poetry run type_check
- name: "Update poetry version"
run: |
poetry run python build_scripts/set_version.py
env:
BRANCH: ${{ github.ref }}
BUILD_ID: ${{ github.run_number }}
test:
runs-on: ubuntu-latest
name: Run Clue API Unit Tests
needs: formatting
defaults:
run:
working-directory: api
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- name: Get base depth of PR
if: github.event_name == 'pull_request'
id: base-depth
working-directory: .
run: |
echo "base-depth=$(expr ${{ github.event.pull_request.commits }} + 1)" >> $GITHUB_OUTPUT
- name: Checkout current ref
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: ${{ steps.base-depth.outputs.base-depth }}
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Check for valid merge base
run: |
git status
git diff $BASE_SHA..$HEAD_SHA
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
- name: Spin up test dependencies
uses: hoverkraft-tech/compose-action@v2.2.0
with:
compose-file: "api/dev/docker-compose.yml"
up-flags: "--build"
- name: Install Poetry
uses: abatilo/actions-poetry@v4
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./api/.venv
key: venv-${{ hashFiles('api/poetry.lock') }}
- name: Setup Environment
run: |
sudo mkdir -p /etc/clue/conf/
sudo mkdir -p /etc/clue/lookups/
sudo mkdir -p /var/log/clue
sudo chmod a+rw /etc/clue/conf/
sudo chmod a+rw /etc/clue/lookups/
sudo chmod a+rw /var/log/clue
cp build_scripts/classification.yml /etc/clue/conf/classification.yml
cp test/unit/config.yml /etc/clue/conf/config.yml
- name: Install Dependencies
run: |
poetry install --all-extras -vvv --with test
- name: Ensure keycloak is healthy
run: |
poetry run python build_scripts/keycloak_health.py
- name: Run tests
id: tests
continue-on-error: true
run: |
poetry run test
env:
WRITE_MARKDOWN: "true"
- name: Add Failed Test PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request' && steps.tests.outcome == 'failure'
# https://github.com/marocchino/sticky-pull-request-comment/issues/227
continue-on-error: true
with:
path: api/test-results.md
header: api
- name: Coverage Results
if: steps.tests.outcome == 'success'
run: |
git diff --name-only $BASE_SHA..$HEAD_SHA
git diff $BASE_SHA..$HEAD_SHA > diff.txt
poetry run coverage_report
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
GIT_BRANCH: ${{ github.event.pull_request.head.ref }}
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: steps.tests.outcome == 'success' && github.event_name == 'pull_request' && matrix.python-version == '3.12'
# https://github.com/marocchino/sticky-pull-request-comment/issues/227
continue-on-error: true
with:
path: api/coverage-results.md
header: api
- name: Force Failure if Tests Failed
if: steps.tests.outcome == 'failure'
run: |
exit 1
test_docker_build:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
name: Test Clue API Docker Image Build
needs:
- formatting
- test
defaults:
run:
working-directory: ./api
steps:
- name: Checkout current ref
uses: actions/checkout@v4
- name: Install Poetry
uses: abatilo/actions-poetry@v4
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./api/.venv
key: venv-${{ hashFiles('api/poetry.lock') }}
- name: Install Dependencies and Build
run: |
cp -r ../docs .
poetry install
poetry build
- name: Extract Clue API version
id: version
run: |
echo "version=$(poetry version -s)" >> $GITHUB_OUTPUT
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Test build API Docker image (no push)
uses: docker/build-push-action@v3
with:
context: api
file: api/Dockerfile
push: false
tags: clue-api:test
build-args: |
version=${{ steps.version.outputs.version }}
branch=${{ github.ref }}
commit=${{ github.sha }}
push_to_pypi:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
name: Build Clue API Python Package
needs:
- formatting
- test
defaults:
run:
working-directory: ./api
steps:
- name: Checkout develop
uses: actions/checkout@v4
with:
ref: develop
- name: Checkout current ref
uses: actions/checkout@v4
- name: Install Poetry
uses: abatilo/actions-poetry@v4
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./api/.venv
key: venv-${{ hashFiles('api/poetry.lock') }}
- name: Install Dependencies
run: |
poetry install
- name: "Update poetry version"
run: |
poetry run python build_scripts/set_version.py
env:
BRANCH: ${{ github.ref }}
BUILD_ID: ${{ github.run_number }}
- name: Build Package
run: |
poetry install
poetry build
- name: Configure Poetry for PyPI
run: poetry config pypi-token.pypi ${{ secrets.PYPI_TOKEN }}
- name: Publish the package to PyPI
if: github.event_name != 'pull_request'
run: |
poetry publish
push_to_registry:
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
name: Build Clue API Docker Image
needs:
- formatting
- test
defaults:
run:
working-directory: ./api
permissions:
id-token: write
contents: read
attestations: write
steps:
- name: Checkout develop
uses: actions/checkout@v4
with:
ref: develop
- name: Checkout current ref
uses: actions/checkout@v4
- name: Install Poetry
uses: abatilo/actions-poetry@v4
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./api/.venv
key: venv-${{ hashFiles('api/poetry.lock') }}
- name: Install Dependencies and Build
run: |
cp -r ../docs .
poetry install
poetry build
- name: Extract Clue API version
id: version
run: |
echo "version=$(poetry version -s)" >> $GITHUB_OUTPUT
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: cccs/clue-api
tags: |
type=raw,event=branch,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=raw,event=branch,value=nightly,enable=${{ github.ref == format('refs/heads/{0}', 'develop') }}
type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags') }}
type=semver,pattern={{version}}
type=raw,value=${{steps.version.outputs.version}}_{{branch}}
type=raw,value=${{steps.version.outputs.version}}_{{branch}}_{{sha}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push API Docker image
id: push
uses: docker/build-push-action@v3
with:
context: api
file: api/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
version=${{ steps.meta.outputs.version }}
branch=${{ github.ref }}
commit=${{ github.sha }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v2
with:
subject-name: index.docker.io/cccs/clue-api
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true