forked from intelowlproject/GreedyBear
-
Notifications
You must be signed in to change notification settings - Fork 0
576 lines (529 loc) · 20.5 KB
/
_python.yml
File metadata and controls
576 lines (529 loc) · 20.5 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
name: Reusable python linter workflow
on:
workflow_call:
inputs:
# Base configs
python_versions:
description: Python versions to use (in the form of a JSON array)
type: string
required: true
ubuntu_version:
description: Ubuntu version to use
type: string
default: latest
required: false
working_directory:
description: Directory that must be run against the linters
type: string
required: true
requirements_path:
description: Path to the requirements.txt file
type: string
required: true
project_dev_requirements_file:
description: Path to an additional project dev requirements file
type: string
required: false
install_from:
description: Directory that must be used to install the packages
type: string
required: false
default: .
packages_path:
description: Path to the packages.txt file (APT requirements)
type: string
required: false
env:
description: Environment variables to set
type: string
required: false
default: >-
{}
max_timeout:
description: Max time that the CI can be run
type: number
required: false
default: 30
# Formatters
use_black:
description: Use black formatter
type: boolean
required: false
use_isort:
description: Use isort formatter
type: boolean
required: false
use_ruff_formatter:
description: Use ruff formatter
type: boolean
required: false
# Linters
use_autoflake:
description: Use autoflake linter
type: boolean
required: false
use_bandit:
description: Use bandit linter
type: boolean
required: false
use_flake8:
description: Use flake8 linter
type: boolean
required: false
use_pylint:
description: Use pylint linter
type: boolean
required: false
use_ruff_linter:
description: Use ruff linter
type: boolean
required: false
# Coverage configs
use_coverage:
description: Use coverage.py.
type: boolean
required: false
coverage_config_path:
description: Path to the coverage.py config file
type: string
required: false
default: .coveragerc
upload_coverage:
description: Upload coverage.py report to github
type: boolean
required: false
# CodeQL configs
run_codeql:
description: Run codeql
type: boolean
required: false
# Services
use_celery:
description: Create a celery worker
type: boolean
required: false
use_elastic_search:
description: Use elastic_search service
type: boolean
required: false
use_memcached:
description: Use memcached service
type: boolean
required: false
use_mongo:
description: Use mongo service
type: boolean
required: false
use_postgres:
description: Use postgres service
type: boolean
required: false
use_rabbitmq:
description: Use rabbitmq service
type: boolean
required: false
use_redis:
description: Use redis service
type: boolean
required: false
# Services configs
## Celery service configs
celery_app:
description: Celery app name. Requires use_celery to be true
type: string
required: false
celery_queues:
description: Celery queues separated by ,. Requires use_celery to be true
type: string
required: false
default: default
## Elasticsearch service configs
elasticsearch_version:
description: Elasticsearch container version
type: string
required: false
default: latest
elasticsearch_port:
description: Elasticsearch container port
type: string
required: false
default: 9200
## Memcached service configs
memcached_version:
description: Memcached alpine container version
type: string
required: false
default: latest
## Mongo service configs
mongo_version:
description: Mongo container version
type: string
required: false
default: latest
## Postgres service configs
postgres_db:
description: Postgres service db. Requires use_postgres to be true
type: string
required: false
default: db
postgres_user:
description: Postgres service user. Requires use_postgres to be true
type: string
required: false
default: user
postgres_password:
description: Postgres service password. Requires use_postgres to be true
type: string
required: false
default: password
postgres_version:
description: Postgres service version. Requires use_postgres to be true
type: string
required: false
default: latest
## RabbitMQ service configs
rabbitmq_version:
description: RabbitMQ management-alpine container version
type: string
required: false
default: latest
## Redis service configs
redis_version:
description: Redis alpine container version
type: string
required: false
default: latest
# Django configs
django_settings_module:
description: Path to the django settings file
type: string
required: false
check_migrations:
description: Check if migrations are valid. Require django_settings_module to be set.
type: boolean
required: false
check_requirements_licenses:
description: Check if requirements have a valid license. Require django_settings_module to be set.
type: boolean
required: false
default: true
ignore_requirements_licenses_regex:
description: Regex of repositories of which ignore license
type: string
required: false
default: uWSGI.*|lunardate.*|.*QuokkaClient.*|pyquokka.*
tags_for_slow_tests:
description: Tags for tests that will be run only on master/main branch, space separated. Can be used only for django projects.
default: slow
type: string
required: false
tags_for_manual_tests:
description: Tags for tests that will not be run on the CI, space separated. Can be used only for django projects.
default: manual
type: string
required: false
# Misc configs
custom_command:
description: String of custom command to run
type: string
required: false
check_docs_directory:
description: Check docs using rstcheck inside this directory
type: string
required: false
check_dockerfile:
description: Check dockerfile build. WARNING action total time may increase significantly
type: string
required: false
jobs:
python:
name: Run python test suite
runs-on: ubuntu-${{ inputs.ubuntu_version }}
timeout-minutes: ${{ inputs.max_timeout }}
strategy:
matrix:
python_version: ${{ fromJson(inputs.python_versions) }}
language: ['python']
env: ${{ fromJson(inputs.env) }}
steps:
- name: Check out latest commit
uses: actions/checkout@v4
- name: Set up Python
id: setup_python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python_version }}
- name: Inject stuff to environment
run: |
if [[ '${{ inputs.django_settings_module}}' != 'false' ]]; then
echo "PYTHONPATH=${{ github.workspace }}/${{ inputs.working_directory }}" >> $GITHUB_ENV
echo "DJANGO_SETTINGS_MODULE=${{ inputs.django_settings_module }}" >> $GITHUB_ENV
fi
if [[ '${{ inputs.run_codeql}}' != 'false' ]]; then
echo "CODEQL_PYTHON=$(which python)" >> $GITHUB_ENV
fi
shell:
bash
- name: Restore APT cache related to PR event
id: restore_apt_cache_pr
uses: ./.github/actions/apt_requirements/restore_apt_cache
with:
apt_requirements_file_path: ${{ inputs.packages_path }}
- name: Restore APT cache related to target branch
id: restore_apt_cache_target_branch
if: steps.restore_apt_cache_pr.outputs.cache-hit != 'true'
uses: ./.github/actions/apt_requirements/restore_apt_cache
with:
apt_requirements_file_path: ${{ inputs.packages_path }}
git_reference: ${{ github.base_ref }}
- name: Refresh APT repositories
if: >
steps.restore_apt_cache_pr.outputs.cache-hit != 'true' &&
steps.restore_apt_cache_target_branch.outputs.cache-hit != 'true'
run: |
sudo apt-get update
shell: bash
- name: Install APT requirements
run: |
sudo apt-get install -y --no-install-recommends $(tr '\n' ' ' < ${{ inputs.packages_path }})
shell: bash
- name: Save APT cache related to PR event
if: >
steps.restore_apt_cache_pr.outputs.cache-hit != 'true'
uses: ./.github/actions/apt_requirements/save_apt_cache
with:
apt_requirements_file_path: ${{ inputs.packages_path }}
- name: Create linter requirements file
uses: ./.github/actions/python_requirements/create_linter_requirements_file
with:
install_from: ${{ inputs.install_from }}
django_settings_module: ${{ inputs.django_settings_module }}
use_autoflake: ${{ inputs.use_autoflake }}
use_bandit: ${{ inputs.use_bandit }}
use_black: ${{ inputs.use_black }}
use_flake8: ${{ inputs.use_flake8 }}
use_isort: ${{ inputs.use_isort }}
use_pylint: ${{ inputs.use_pylint }}
use_ruff_formatter: ${{ inputs.use_ruff_formatter }}
use_ruff_linter: ${{ inputs.use_ruff_linter }}
- name: Create dev requirements file
uses: ./.github/actions/python_requirements/create_dev_requirements_file
with:
install_from: ${{ inputs.install_from }}
project_dev_requirements_file: ${{ inputs.project_dev_requirements_file }}
- name: Create docs requirements file
uses: ./.github/actions/python_requirements/create_docs_requirements_file
with:
install_from: ${{ inputs.install_from }}
check_docs_directory: ${{ inputs.check_docs_directory }}
django_settings_module: ${{ inputs.django_settings_module }}
- name: Restore Python virtual environment related to PR event
id: restore_python_virtual_environment_pr
uses: ./.github/actions/python_requirements/restore_virtualenv/
with:
requirements_paths: "${{ inputs.requirements_path }} requirements-linters.txt requirements-dev.txt requirements-docs.txt"
python_version: ${{ steps.setup_python.outputs.python-version }}
- name: Restore Python virtual environment related to target branch
id: restore_python_virtual_environment_target_branch
if: steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true'
uses: ./.github/actions/python_requirements/restore_virtualenv/
with:
requirements_paths: ${{ inputs.requirements_path }}
git_reference: ${{ github.base_ref }}
python_version: ${{ steps.setup_python.outputs.python-version }}
- name: Create Python virtual environment
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true' &&
steps.restore_python_virtual_environment_target_branch.outputs.cache-hit != 'true'
uses: ./.github/actions/python_requirements/create_virtualenv
- name: Restore pip cache related to PR event
id: restore_pip_cache_pr
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true' &&
steps.restore_python_virtual_environment_target_branch.outputs.cache-hit != 'true'
uses: ./.github/actions/python_requirements/restore_pip_cache
- name: Restore pip cache related to target branch
id: restore_pip_cache_target_branch
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true' &&
steps.restore_python_virtual_environment_target_branch.outputs.cache-hit != 'true' &&
steps.restore_pip_cache_pr.outputs.cache-hit != 'true'
uses: ./.github/actions/python_requirements/restore_pip_cache
with:
git_reference: ${{ github.base_ref }}
- name: Install project requirements
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true' &&
steps.restore_python_virtual_environment_target_branch.outputs.cache-hit != 'true'
run: pip install -r ${{ inputs.requirements_path }}
shell: bash
working-directory: ${{ inputs.install_from }}
- name: Install other requirements
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true'
run: |
pip install -r requirements-dev.txt
pip install -r requirements-linters.txt
pip install -r requirements-docs.txt
shell: bash
working-directory: ${{ inputs.install_from }}
- name: Check requirements licenses
if: >
inputs.check_requirements_licenses &&
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true'
id: license_check_report
continue-on-error: true
uses: pilosus/action-pip-license-checker@v2
with:
requirements: ${{ inputs.install_from }}/${{ inputs.requirements_path }}
exclude: ${{ inputs.ignore_requirements_licenses_regex }}
headers: true
fail: 'StrongCopyleft,NetworkCopyleft,Error'
fails-only: true
- name: Print wrong licenses
if: steps.license_check_report.outcome == 'failure'
run: |
echo "License check failed"
echo "===================="
echo "${{ steps.license_check_report.outputs.report }}"
echo "===================="
exit 1
shell: bash
- name: Save Python virtual environment related to PR event
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true'
uses: ./.github/actions/python_requirements/save_virtualenv
with:
requirements_paths: "${{ inputs.requirements_path }} requirements-linters.txt requirements-dev.txt requirements-docs.txt"
python_version: ${{ steps.setup_python.outputs.python-version }}
- name: Save pip cache related to PR event
if: >
steps.restore_python_virtual_environment_pr.outputs.cache-hit != 'true' &&
steps.restore_pip_cache_pr.outputs.cache-hit != 'true'
uses: ./.github/actions/python_requirements/save_pip_cache
- name: Run linters
uses: ./.github/actions/python_linter
if: >
inputs.use_black ||
inputs.use_isort ||
inputs.use_flake8 ||
inputs.use_pylint ||
inputs.use_bandit ||
inputs.use_autoflake ||
inputs.use_ruff_formatter ||
inputs.use_ruff_linter
with:
working_directory: ${{ inputs.working_directory }}
use_black: ${{ inputs.use_black }}
use_isort: ${{ inputs.use_isort }}
use_flake8: ${{ inputs.use_flake8 }}
use_pylint: ${{ inputs.use_pylint }}
use_bandit: ${{ inputs.use_bandit }}
use_autoflake: ${{ inputs.use_autoflake }}
use_ruff_formatter: ${{ inputs.use_ruff_formatter }}
use_ruff_linter: ${{ inputs.use_ruff_linter }}
- name: Run CodeQL
if: inputs.run_codeql
uses: ./.github/actions/codeql
with:
language: python
working_directory: ${{ inputs.working_directory }}
- name: Build Docs
if: inputs.check_docs_directory
run: |
rstcheck -r --report-level error --ignore-directives automodule,autoclass .
python3 -m sphinx -a -T -E -b html -d _build/doctrees -D language=en . _build/html
shell: bash
working-directory: ${{ inputs.check_docs_directory }}
- name: Start services
uses: ./.github/actions/services
if: inputs.use_postgres || inputs.use_elastic_search || inputs.use_memcached || inputs.use_redis || inputs.use_rabbitmq || inputs.use_mongo
with:
use_mongo: ${{ inputs.use_mongo }}
use_redis: ${{ inputs.use_redis }}
use_postgres: ${{ inputs.use_postgres }}
use_elastic_search: ${{ inputs.use_elastic_search }}
use_memcached: ${{ inputs.use_memcached }}
use_rabbitmq: ${{ inputs.use_rabbitmq }}
postgres_db: ${{ inputs.postgres_db }}
postgres_user: ${{ inputs.postgres_user }}
postgres_password: ${{ inputs.postgres_password }}
postgres_version: ${{ inputs.postgres_version }}
mongo_version: ${{ inputs.mongo_version }}
elasticsearch_version: ${{ inputs.elasticsearch_version }}
elasticsearch_port: ${{ inputs.elasticsearch_port }}
memcached_version: ${{ inputs.memcached_version }}
redis_version: ${{ inputs.redis_version }}
rabbitmq_version: ${{ inputs.rabbitmq_version }}
- name: Start celery worker
if: inputs.use_celery
run: |
celery -A ${{ inputs.celery_app }} worker -E -c 1 -n worker -Q ${{ inputs.celery_queues }} --detach
working-directory: ${{ inputs.working_directory }}
- name: Run custom command
if: inputs.custom_command
run: |
COMMAND='${{ inputs.custom_command }}'
echo "Running command: $COMMAND"
eval $COMMAND
working-directory: ${{ inputs.working_directory }}
shell: bash
- name: Check migrations
if: inputs.check_migrations && inputs.django_settings_module
run: |
django-admin makemigrations --dry-run >/dev/null
working-directory: ${{ inputs.working_directory }}
shell: bash
- name: Run unittest
run: |
CMD="python"
if [[ '${{ inputs.use_coverage }}' != 'false' ]]; then
CMD="coverage run --rcfile=${{ inputs.coverage_config_path }}"
if [[ -n '${{ inputs.django_settings_module }}' ]]; then
CMD="${CMD} -a manage.py test"
# manage manual tags
if [[ -n '${{ inputs.tags_for_manual_tests }}' ]]; then
for tag in ${{ inputs.tags_for_manual_tests }}; do
CMD="${CMD} --exclude-tag=$tag"
done
fi
# manage slow tags
if [[ '${{ github.base_ref }}' != 'master' && '${{ github.base_ref }}' != 'main' ]]; then
if [[ -n '${{ inputs.tags_for_slow_tests }}' ]]; then
for tag in ${{ inputs.tags_for_slow_tests }}; do
CMD="${CMD} --exclude-tag=$tag"
done
fi
fi
else
CMD="${CMD} -m unittest discover"
fi
else
CMD="python -m unittest discover"
fi
CMD="${CMD} --failfast"
echo "Running command: ${CMD}"
$CMD
working-directory: ${{ inputs.working_directory }}
env: ${{ secrets }}
shell: bash
- name: Create coverage output
if: inputs.use_coverage && inputs.upload_coverage
id: coverage-output
run: |
echo "Uploading coverage.py report"
echo "## Coverage.py report" >> $GITHUB_STEP_SUMMARY
echo "$(coverage report -m --format=markdown)" >> $GITHUB_STEP_SUMMARY
working-directory: ${{ inputs.working_directory }}
- name: Generate coverage XML
if: inputs.use_coverage && inputs.upload_coverage
run: |
coverage xml
working-directory: ${{ inputs.working_directory }}
- name: Upload coverage report as artifact
if: inputs.use_coverage && inputs.upload_coverage
uses: actions/upload-artifact@v4
with:
name: coverage-report-py${{ matrix.python_version }}
path: ${{ inputs.working_directory }}/coverage.xml
retention-days: 30