A shareable package of CircleCI configuration to integrate with python, written by circleci
CertifiedLanguage/FrameworkUse CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1Add the orbs stanza below your version, invoking the orb:
1
2
orbs:
python: circleci/python@4.0.0Use python elements in your existing workflows and jobs.
An example of running a pipenv based project with pytest. And then using twine to publish to pypi. The project using these args would have pytest as a dev dependency in its Pipfile. The CircleCI project settings would have the environment variables for twine auth.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '2.1'
orbs:
python: circleci/python@4.0.0
jobs:
publish:
executor:
name: python/default
tag: '3.14'
steps:
- checkout
- python/dist
- run: pip install twine && twine upload dist/*
workflows:
main:
jobs:
- python/test:
args: '--dev'
pkg-manager: pipenv
test-tool: pytest
- publish
An example of working with the Pip and caching requirements.txt on CircleCI to speed up builds.
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
version: '2.1'
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: '3.14'
steps:
- checkout
- python/install-packages:
pkg-manager: pip
- python/install-packages:
cache-key-override: >-
pip-{{ checksum 'dev-requirements.txt' }}-{{ checksum
'requirements.txt' }}
pkg-manager: pip
- python/install-packages:
pip-dependency-file: dev-requirements.txt
pkg-manager: pip
- python/install-packages:
args: pytest
pkg-manager: pip
pypi-cache: false
- run:
command: |
pytest --version
name: Test
workflows:
main:
jobs:
- build
An example of working with the Pipenv cache on CircleCI to speed up builds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
version: '2.1'
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: '3.14'
steps:
- checkout
- python/install-packages:
args: pytest
pkg-manager: pipenv
- run:
command: |
pipenv run pytest --version
name: Test it
workflows:
main:
jobs:
- build
An example of working with the Poetry cache on CircleCI to speed up builds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '2.1'
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: '3.14'
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
- run:
command: |
poetry run pytest --version
name: Test it
workflows:
main:
jobs:
- build
An example of working with the uv cache on CircleCI to speed up builds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '2.1'
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: '3.14'
steps:
- checkout
- python/install-packages:
pkg-manager: uv
- run:
command: |
uv run pytest --version
name: Test it
workflows:
main:
jobs:
- build
Simple drop-in job to setup a python project, run tests and store the test results if possible. If test-tool is pytest, the job assumes the project defines pytest in the dependency file. Otherwise, use the built in unittest module as the test-tool.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
app-dir Path to the directory containing your python project. Not needed if dependency file lives in the root. | Path to the directory containing your python project. Not needed if dependency file lives in the root. | No | ~/project type: string | string |
args Arguments to pass to install command for pipenv, poetry or uv. Override '-r requirements.txt' for pip. | Arguments to pass to install command for pipenv, poetry or uv. Override '-r requirements.txt' for pip. | No | '' type: string | string |
cache-folder-prefix A directory where you would like to save the cache files. Defaults to empty, saving the cache to project folder.
If you don't want the cache in your project directory, specify the absolute path of an existing directory here.
| A directory where you would like to save the cache files. Defaults to empty, saving the cache to project folder.
If you don't want the cache in your project directory, specify the absolute path of an existing directory here.
| No | /tmp/cache type: string | string |
cache-version Change the default cache version if you need to clear the cache for any reason. | Change the default cache version if you need to clear the cache for any reason. | No | v1 type: string | string |
ensure-test-tool If true, a step will be ran to ensure the configured test tool is installed.
| If true, a step will be ran to ensure the configured test tool is installed.
| No | true type: boolean | boolean |
executor The name of executor to use.
| The name of executor to use.
| No | default type: executor | executor |
fail-if-missing-tool If true, this job will fail if the test tool was not found. If false, it will automatically install to recover.
| If true, this job will fail if the test tool was not found. If false, it will automatically install to recover.
| No | false type: boolean | boolean |
include-branch-in-cache-key If true, this cache bucket will only apply to jobs within the same branch.
| If true, this cache bucket will only apply to jobs within the same branch.
| No | true type: boolean | boolean |
include-python-in-cache-key If true, this cache bucket will checksum the pyenv python version with the cache-key
| If true, this cache bucket will checksum the pyenv python version with the cache-key
| No | true type: boolean | boolean |
module-src-dir Path relative to app-dir which contains module source. | Path relative to app-dir which contains module source. | No | src type: string | string |
pip-dependency-file Name of the requirements file that needs to be installed with pip. Prepended with `app-dir`. If using pipenv, poetry or uv, this is ignored. | Name of the requirements file that needs to be installed with pip. Prepended with `app-dir`. If using pipenv, poetry or uv, this is ignored. | No | requirements.txt type: string | string |
pkg-manager Select the package manager to use. Default is pip | Select the package manager to use. Default is pip | No | auto type: enum | enum |
pre-install-steps Steps needed between restoring the cache and the install step.
| Steps needed between restoring the cache and the install step.
| No | [] type: steps | steps |
pypi-cache Keep all versions of pypi and site-package caches for faster rebuilding overall. | Keep all versions of pypi and site-package caches for faster rebuilding overall. | No | true type: boolean | boolean |
setup Provide any optional steps you would like to run prior to install the python project. | Provide any optional steps you would like to run prior to install the python project. | No | [] type: steps | steps |
setup-file-path Path to the setup.py file. | Path to the setup.py file. | No | '' type: string | string |
test-tool The tool to run the tests with. | The tool to run the tests with. | No | pytest type: enum | enum |
test-tool-args Arguments to pass to test tool, i.e. discovery settings for unittest - 'discover -s tests_dir'. | Arguments to pass to test tool, i.e. discovery settings for unittest - 'discover -s tests_dir'. | No | '' type: string | string |
venv-cache Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager. | Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager. | No | true type: boolean | boolean |
Build a distribution package
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
app-dir Path to the directory containing your python project. Not needed if 'setup.py' lives in root project dir. | Path to the directory containing your python project. Not needed if 'setup.py' lives in root project dir. | No | . type: string | string |
build-tool Choose the build command to run. Options include PEP427 wheel (default) and PEP517 build. | Choose the build command to run. Options include PEP427 wheel (default) and PEP517 build. | No | wheel type: enum | enum |
no-isolation Do not isolate the build in a virtual environment. Exclusive to build-tool option "build" only. | Do not isolate the build in a virtual environment. Exclusive to build-tool option "build" only. | No | false type: boolean | boolean |
out-dir Path where to write the output files. | Path where to write the output files. | No | ./dist type: string | string |
sdist-separate Opt out of default behavior and build sdist independently from wheel. Exclusive to build-tool option "build" only. | Opt out of default behavior and build sdist independently from wheel. Exclusive to build-tool option "build" only. | No | false type: boolean | boolean |
skip-dependency-check Do not check that build dependencies are installed. Exclusive to build-tool option "build" only. | Do not check that build dependencies are installed. Exclusive to build-tool option "build" only. | No | false type: boolean | boolean |
wheel-separate Build a wheel directly from the source. Default behavior builds an sdist, then builds a wheel from that sdist. Exclusive to build-tool option "build" only. | Build a wheel directly from the source. Default behavior builds an sdist, then builds a wheel from that sdist. Exclusive to build-tool option "build" only. | No | false type: boolean | boolean |
Setup a python environment and install the packages for your project either globally with pip or in a virtualenv with poetry or pipenv. With pip as pkg-manager, the command will assume `-r requirements.txt`. With poetry as pkg-manager, the command will assume `--no-ansi`. For pipenv, no args are provided. Expect the default caching locations for packages and virtualenvs on a debian system with pyenv.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
app-dir Path to the directory containing your python project. Not needed if dependency file lives in the root. | Path to the directory containing your python project. Not needed if dependency file lives in the root. | No | . type: string | string |
args Arguments to pass to install command for pipenv and poetry. For pip, arguments are after the command, `pip install -r requirements.txt <args>`.
For poetry, args are after `--no-ansi` as output option.
| Arguments to pass to install command for pipenv and poetry. For pip, arguments are after the command, `pip install -r requirements.txt <args>`.
For poetry, args are after `--no-ansi` as output option.
| No | '' type: string | string |
cache-folder-prefix A directory where you would like to save the cache files. Defaults to empty, saving the cache to project folder.
If you don't want the cache in your project directory, specify the absolute path of an existing directory here.
| A directory where you would like to save the cache files. Defaults to empty, saving the cache to project folder.
If you don't want the cache in your project directory, specify the absolute path of an existing directory here.
| No | /tmp/cache type: string | string |
cache-key-override Override the built-in cache key. If set, this value will be used as the cache key for restore_cache/save_cache steps.
You can use CircleCI template functions, e.g.:
cache-key-override: "pip-{{ checksum 'dev-requirements.txt' }}-{{ checksum 'requirements.txt' }}"
If not set, the default cache key logic will be used.
| Override the built-in cache key. If set, this value will be used as the cache key for restore_cache/save_cache steps.
You can use CircleCI template functions, e.g.:
cache-key-override: "pip-{{ checksum 'dev-requirements.txt' }}-{{ checksum 'requirements.txt' }}"
If not set, the default cache key logic will be used.
| No | '' type: string | string |
cache-version Change the default cache version if you need to clear the cache for any reason. | Change the default cache version if you need to clear the cache for any reason. | No | v1 type: string | string |
include-branch-in-cache-key If true, this cache bucket will only apply to jobs within the same branch.
| If true, this cache bucket will only apply to jobs within the same branch.
| No | true type: boolean | boolean |
include-python-in-cache-key If true, this cache bucket will checksum the pyenv python version with the cache-key.
| If true, this cache bucket will checksum the pyenv python version with the cache-key.
| No | true type: boolean | boolean |
no_output_timeout Elapsed time the command can run without output. Passed to install command. | Elapsed time the command can run without output. Passed to install command. | No | 10m type: string | string |
path-args If using `pip-dist` these are the arguments after the command `pip install -e` that is by default set to `.`. Use of this parameter allows
for multiple paths to be specified. This is important when extra paths are required to install extra packages referenced via `extras_requires`.
| If using `pip-dist` these are the arguments after the command `pip install -e` that is by default set to `.`. Use of this parameter allows
for multiple paths to be specified. This is important when extra paths are required to install extra packages referenced via `extras_requires`.
| No | . type: string | string |
pip-dependency-file Name of the requirements file that needs to be installed with pip. Prepended with `app-dir`. If using pipenv or poetry, this is ignored.
If using `pip-dist`, use this to use the cache checksum against the `setup.py` or `pyproject.toml` if desired.
If `pip-dependency-file` is set to an empty string, no dependency file is used in the `pip install` command.
| Name of the requirements file that needs to be installed with pip. Prepended with `app-dir`. If using pipenv or poetry, this is ignored.
If using `pip-dist`, use this to use the cache checksum against the `setup.py` or `pyproject.toml` if desired.
If `pip-dependency-file` is set to an empty string, no dependency file is used in the `pip install` command.
| No | requirements.txt type: string | string |
pkg-manager Which package management tool to use, pipenv, pip or poetry with dependency file. Use `pip-dist` to install with project setup.py or PEP621 (pyproject.toml). | Which package management tool to use, pipenv, pip or poetry with dependency file. Use `pip-dist` to install with project setup.py or PEP621 (pyproject.toml). | No | auto type: enum | enum |
pre-install-steps Steps needed between restoring the cache and the install step.
| Steps needed between restoring the cache and the install step.
| No | [] type: steps | steps |
pypi-cache Keep all versions of pypi and site-package caches for faster rebuilding overall. | Keep all versions of pypi and site-package caches for faster rebuilding overall. | No | true type: boolean | boolean |
venv-cache Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager. | Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager. | No | true type: boolean | boolean |
venv-path Override venv path. As json array - ex '[ "path", "path2" ]' | Override venv path. As json array - ex '[ "path", "path2" ]' | No | '' type: string | string |
The official CircleCI CIMG Python Docker image.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
tag The `cimg/python` Docker image version tag. | The `cimg/python` Docker image version tag. | No | '3.14' type: string | string |
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Common CircleCI tasks for the Python programming language.
Supports Linux x86_64, MacOS, and Arm64 V8.
display:
source_url: https://github.com/circleci-public/python-orb
commands:
dist:
description: Build a distribution package
parameters:
app-dir:
default: .
description: Path to the directory containing your python project. Not needed if 'setup.py' lives in root project dir.
type: string
build-tool:
default: wheel
description: Choose the build command to run. Options include PEP427 wheel (default) and PEP517 build.
enum:
- wheel
- build
type: enum
no-isolation:
default: false
description: Do not isolate the build in a virtual environment. Exclusive to build-tool option "build" only.
type: boolean
out-dir:
default: ./dist
description: Path where to write the output files.
type: string
sdist-separate:
default: false
description: Opt out of default behavior and build sdist independently from wheel. Exclusive to build-tool option "build" only.
type: boolean
skip-dependency-check:
default: false
description: Do not check that build dependencies are installed. Exclusive to build-tool option "build" only.
type: boolean
wheel-separate:
default: false
description: Build a wheel directly from the source. Default behavior builds an sdist, then builds a wheel from that sdist. Exclusive to build-tool option "build" only.
type: boolean
steps:
- run:
command: |-
if [ ! "${BASH_ENV_PYTHON_ALIASED}" ]; then
echo 'if [ ! $(command -v python) ]; then
shopt -s expand_aliases
alias python=python3
alias pip=pip3
fi
BASH_ENV_PYTHON_ALIASED=true' >> "$BASH_ENV"
fi
name: Alias Python
- when:
condition:
equal:
- << parameters.build-tool >>
- build
steps:
- run:
command: pip install build
name: Install pypa build
- run:
command: |
main() {
local -a build_args
build_args=(
--outdir "$PARAM_OUTDIR"
)
[[ "$PARAM_SDIST" == 1 ]] && build_args+=( --sdist )
[[ "$PARAM_WHEEL" == 1 ]] && build_args+=( --wheel )
[[ "$PARAM_SKIP_DEPENDENCY_CHECK" == 1 ]] && build_args+=( --skip-dependency-check )
[[ "$PARAM_NO_ISOLATION" == 1 ]] && build_args+=( --no-isolation )
set -x
python -m build "${build_args[@]}" .
set +x
ls -l "$PARAM_OUTDIR"
}
main "$@"
environment:
PARAM_NO_ISOLATION: << parameters.no-isolation >>
PARAM_OUTDIR: << parameters.out-dir >>
PARAM_SDIST: << parameters.sdist-separate >>
PARAM_SKIP_DEPENDENCY_CHECK: << parameters.skip-dependency-check >>
PARAM_WHEEL: << parameters.wheel-separate >>
name: Build distribution package
working_directory: << parameters.app-dir >>
- when:
condition:
equal:
- << parameters.build-tool >>
- wheel
steps:
- run:
command: pip install wheel
name: Install wheel
- run:
command: |
python setup.py sdist bdist_wheel --dist-dir "$PARAM_OUTDIR"
ls -l "$PARAM_OUTDIR"
environment:
PARAM_OUTDIR: << parameters.out-dir >>
name: Build distribution package
working_directory: << parameters.app-dir >>
install-packages:
description: |
Setup a python environment and install the packages for your project either globally with pip or in a virtualenv with poetry or pipenv. With pip as pkg-manager, the command will assume `-r requirements.txt`. With poetry as pkg-manager, the command will assume `--no-ansi`. For pipenv, no args are provided. Expect the default caching locations for packages and virtualenvs on a debian system with pyenv.
parameters:
app-dir:
default: .
description: Path to the directory containing your python project. Not needed if dependency file lives in the root.
type: string
args:
default: ""
description: |
Arguments to pass to install command for pipenv and poetry. For pip, arguments are after the command, `pip install -r requirements.txt <args>`.
For poetry, args are after `--no-ansi` as output option.
type: string
cache-folder-prefix:
default: /tmp/cache
description: |
A directory where you would like to save the cache files. Defaults to empty, saving the cache to project folder.
If you don't want the cache in your project directory, specify the absolute path of an existing directory here.
type: string
cache-key-override:
default: ""
description: |
Override the built-in cache key. If set, this value will be used as the cache key for restore_cache/save_cache steps.
You can use CircleCI template functions, e.g.:
cache-key-override: "pip-{{ checksum 'dev-requirements.txt' }}-{{ checksum 'requirements.txt' }}"
If not set, the default cache key logic will be used.
type: string
cache-version:
default: v1
description: Change the default cache version if you need to clear the cache for any reason.
type: string
include-branch-in-cache-key:
default: true
description: |
If true, this cache bucket will only apply to jobs within the same branch.
type: boolean
include-python-in-cache-key:
default: true
description: |
If true, this cache bucket will checksum the pyenv python version with the cache-key.
type: boolean
no_output_timeout:
default: 10m
description: Elapsed time the command can run without output. Passed to install command.
type: string
path-args:
default: .
description: |
If using `pip-dist` these are the arguments after the command `pip install -e` that is by default set to `.`. Use of this parameter allows
for multiple paths to be specified. This is important when extra paths are required to install extra packages referenced via `extras_requires`.
type: string
pip-dependency-file:
default: requirements.txt
description: |
Name of the requirements file that needs to be installed with pip. Prepended with `app-dir`. If using pipenv or poetry, this is ignored.
If using `pip-dist`, use this to use the cache checksum against the `setup.py` or `pyproject.toml` if desired.
If `pip-dependency-file` is set to an empty string, no dependency file is used in the `pip install` command.
type: string
pkg-manager:
default: auto
description: Which package management tool to use, pipenv, pip or poetry with dependency file. Use `pip-dist` to install with project setup.py or PEP621 (pyproject.toml).
enum:
- auto
- poetry
- pipenv
- pip
- pip-dist
- uv
type: enum
pre-install-steps:
default: []
description: |
Steps needed between restoring the cache and the install step.
type: steps
pypi-cache:
default: true
description: Keep all versions of pypi and site-package caches for faster rebuilding overall.
type: boolean
venv-cache:
default: true
description: Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager.
type: boolean
venv-path:
default: ""
description: Override venv path. As json array - ex '[ "path", "path2" ]'
type: string
steps:
- run:
command: |-
# shellcheck disable=SC2016
echo 'if [ "${PARAM_PKG_MNGR}" = "auto" ]; then
if [ -f "requirements.txt" ]; then
if [ -f "${PARAM_SETUP_FILE_PATH:-setup.py}" ]; then
export DETECT_PKG_MNGR="pip-dist"
else
export DETECT_PKG_MNGR="pip"
fi
elif [ -f "Pipfile" ]; then
export DETECT_PKG_MNGR="pipenv"
export PYTHON_ENV_TOOL="pipenv"
elif [ -f "uv.lock" ]; then
export DETECT_PKG_MNGR="uv"
export PYTHON_ENV_TOOL="uv"
elif [ -f "pyproject.toml" ]; then
export DETECT_PKG_MNGR="poetry"
export PYTHON_ENV_TOOL="poetry"
fi
echo "INFO: Detected Package Manager ${DETECT_PKG_MNGR}"
fi' > /tmp/detect-env.sh
chmod +x /tmp/detect-env.sh
echo 'export AUTO_DETECT_ENV_SCRIPT="/tmp/detect-env.sh"' >> "$BASH_ENV"
name: Export automatic environment detection script
- run:
command: |-
if [ ! "${BASH_ENV_PYTHON_ALIASED}" ]; then
echo 'if [ ! $(command -v python) ]; then
shopt -s expand_aliases
alias python=python3
alias pip=pip3
fi
BASH_ENV_PYTHON_ALIASED=true' >> "$BASH_ENV"
fi
name: Alias Python
- when:
condition:
or:
- << parameters.pypi-cache >>
- << parameters.venv-cache >>
steps:
- run:
command: "eval \"$SCRIPT_UTILS\"\n# shellcheck source=detect-env.sh\nsource \"$AUTO_DETECT_ENV_SCRIPT\"\nPARAM_CACHE_FOLDER_PREFIX=\"$(echo \"$PARAM_CACHE_FOLDER_PREFIX\" | circleci env subst)\"\ndetect_os\nif [[ \"$PARAM_CACHE_FOLDER_PREFIX\" == /* ]]; then\n if [[ \"$PLATFORM\" == \"windows\" ]]; then\n CACHE_PREFIX=\"/c$PARAM_CACHE_FOLDER_PREFIX\"\n else\n CACHE_PREFIX=\"$PARAM_CACHE_FOLDER_PREFIX\"\n fi\n\nelse\n CACHE_PREFIX=\"${PWD%/\"$PARAM_APP_DIR\"}/$PARAM_CACHE_FOLDER_PREFIX\"\nfi\n\n LOCKFILE_PATH=\"${CACHE_PREFIX%/}/.cci_pycache/lockfile\"\nmkdir -p \"${CACHE_PREFIX}/.cci_pycache/\"\n\nif [ ! -f \"${LOCKFILE_PATH}\" ]; then\n case ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} in\n pip | pip-dist)\n LOCK_FILE=\"${PARAM_DEPENDENCY_FILE:-requirements.txt}\"\n ;;\n pipenv)\n LOCK_FILE=\"Pipfile.lock\"\n ;;\n poetry)\n LOCK_FILE=\"poetry.lock\"\n ;;\n uv)\n LOCK_FILE=\"uv.lock\"\n ;;\n esac\n \n if [ -z \"${LOCK_FILE}\" ]; then\n echo \"WARNING: Could not determine lockfile path for ${DETECT_PKG_MNGR:-PARAM_PKG_MNGR}\"\n else\n FULL_LOCK_FILE=$(readlink -f \"${LOCK_FILE}\")\n\n if [ -f \"${LOCK_FILE}\" ]; then\n echo \"INFO: Copying ${FULL_LOCK_FILE} to ${LOCKFILE_PATH}\"\n cp \"${FULL_LOCK_FILE}\" \"${LOCKFILE_PATH}\"\n else\n echo \"WARNING: Could not find lockfile at ${LOCK_FILE}\"\n fi\n fi\nfi"
environment:
PARAM_APP_DIR: <<parameters.app-dir>>
PARAM_CACHE_FOLDER_PREFIX: <<parameters.cache-folder-prefix>>
PARAM_DEPENDENCY_FILE: << parameters.pip-dependency-file >>
PARAM_PKG_MNGR: << parameters.pkg-manager >>
PARAM_PYPI_CACHE: << parameters.pypi-cache >>
PARAM_VENV_CACHE: << parameters.venv-cache >>
PARAM_VENV_PATH: << parameters.venv-path >>
SCRIPT_UTILS: |4-
detect_os() {
detected_platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$detected_platform" in
linux*)
export PLATFORM=linux
;;
darwin*)
export PLATFORM=macos
;;
msys*|cygwin*)
export PLATFORM=windows
;;
*)
printf '%s\n' "Unsupported OS: \"$detected_platform\"."
exit 1
;;
esac
}
name: Link lockfile
working_directory: << parameters.app-dir >>
- run:
command: |-
eval "$SCRIPT_UTILS"
detect_os
PARAM_CACHE_FOLDER_PREFIX="$(echo "$PARAM_CACHE_FOLDER_PREFIX" | circleci env subst)"
if [[ "$PARAM_CACHE_FOLDER_PREFIX" == /* ]]; then
if [[ "$PLATFORM" == "windows" ]]; then
CACHE_PREFIX="/c$PARAM_CACHE_FOLDER_PREFIX"
else
CACHE_PREFIX="$PARAM_CACHE_FOLDER_PREFIX"
fi
else
CACHE_PREFIX="${PWD%/"$PARAM_APP_DIR"}/$PARAM_CACHE_FOLDER_PREFIX"
fi
CACHE_DIR="${CACHE_PREFIX%/}/.temp-python-version"
mkdir -p "${CACHE_PREFIX}"
echo "INFO: Copying python version to ${CACHE_DIR}"
python --version | cut -d ' ' -f2 > "$CACHE_DIR" && cat "$CACHE_DIR"
environment:
PARAM_APP_DIR: <<parameters.app-dir>>
PARAM_CACHE_FOLDER_PREFIX: <<parameters.cache-folder-prefix>>
SCRIPT_UTILS: |4-
detect_os() {
detected_platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$detected_platform" in
linux*)
export PLATFORM=linux
;;
darwin*)
export PLATFORM=macos
;;
msys*|cygwin*)
export PLATFORM=windows
;;
*)
printf '%s\n' "Unsupported OS: \"$detected_platform\"."
exit 1
;;
esac
}
name: Save python version
working_directory: << parameters.app-dir >>
- restore_cache:
keys:
- <<#parameters.cache-key-override>><<parameters.cache-key-override>><</parameters.cache-key-override>><<^parameters.cache-key-override>><<parameters.cache-version>>-cci_pycache-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>><<#parameters.include-python-in-cache-key>>{{ checksum "<<parameters.cache-folder-prefix>>/.temp-python-version" }}-<</parameters.include-python-in-cache-key>>{{ checksum "<<parameters.cache-folder-prefix>>/.cci_pycache/lockfile" }}<</parameters.cache-key-override>>
- <<^parameters.cache-key-override>><<parameters.cache-version>>-cci_pycache-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>><<#parameters.include-python-in-cache-key>>{{ checksum "<<parameters.cache-folder-prefix>>/.temp-python-version" }}-<</parameters.include-python-in-cache-key>><</parameters.cache-key-override>>
- run:
command: "eval \"$SCRIPT_UTILS\"\ndetect_os\n\nrecurse() {\n if [ ! -d \"$1\" ] || [ ! -e \"$2\" ]; then\n mv -u \"$1\" \"$2\" || exit\n return\n fi\n for entry in \"$1/\"* \"$1/.\"[!.]* \"$1/..\"?*; do\n if [ -e \"$entry\" ]; then\n recurse \"$entry\" \"$2/${entry##\"$1/\"}\"\n fi\n done\n}\n\nrestore_paths() {\n if [ -d \"${1}\" ] && [ -n \"$(ls -A \"${1}\" 2>/dev/null)\" ]; then\n for file in \"${1}\"/*; do\n decoded=$(basename \"${file}\" | base64 -d)\n parent_dir=$(dirname \"${decoded}\")\n \n # make sure the parent directories exist\n if [ ! -d \"${parent_dir}\" ]; then\n mkdir -p \"${parent_dir}\"\n fi\n \n echo \"INFO: Restoring ${file} to ${decoded}\"\n\n recurse \"${file}\" \"${decoded}\"\n done\n fi\n}\nPARAM_CACHE_FOLDER_PREFIX=\"$(echo \"$PARAM_CACHE_FOLDER_PREFIX\" | circleci env subst)\"\n\nif [[ \"$PARAM_CACHE_FOLDER_PREFIX\" == /* ]]; then\n if [[ \"$PLATFORM\" == \"windows\" ]]; then\n CACHE_PREFIX=\"/c$PARAM_CACHE_FOLDER_PREFIX\"\n else\n CACHE_PREFIX=\"$PARAM_CACHE_FOLDER_PREFIX\"\n fi\n\nelse\n CACHE_PREFIX=\"${PWD%/\"$PARAM_APP_DIR\"}/$PARAM_CACHE_FOLDER_PREFIX\"\nfi\n\nCACHE_DIR=\"${CACHE_PREFIX%/}/.cci_pycache\"\n\nif [ \"${PARAM_VENV_CACHE}\" = \"1\" ]; then\n restore_paths \"${CACHE_DIR}/venv\"\nfi\n\nif [ \"${PARAM_PYPI_CACHE}\" = \"1\" ]; then\n restore_paths \"${CACHE_DIR}/pypi\"\nfi"
environment:
PARAM_APP_SRC_DIR: <<parameters.app-dir>>
PARAM_CACHE_FOLDER_PREFIX: <<parameters.cache-folder-prefix>>
PARAM_PYPI_CACHE: << parameters.pypi-cache >>
PARAM_VENV_CACHE: << parameters.venv-cache >>
SCRIPT_UTILS: |4-
detect_os() {
detected_platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$detected_platform" in
linux*)
export PLATFORM=linux
;;
darwin*)
export PLATFORM=macos
;;
msys*|cygwin*)
export PLATFORM=windows
;;
*)
printf '%s\n' "Unsupported OS: \"$detected_platform\"."
exit 1
;;
esac
}
name: Move restored cache
working_directory: << parameters.app-dir >>
- steps: <<parameters.pre-install-steps>>
- when:
condition:
equal:
- auto
- << parameters.pkg-manager >>
steps:
- run:
command: |
# shellcheck source=detect-env.sh
source "$AUTO_DETECT_ENV_SCRIPT"
case ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} in
pip)
PYTHON_INSTALL_ARGS="-r ${PARAM_DEPENDENCY_FILE:-requirements.txt}"
eval "${PYTHON_ENV_TOOL:-pip} install ${PYTHON_INSTALL_ARGS} ${PARAM_ADDITIONAL_ARGS}"
;;
pip-dist)
PYTHON_INSTALL_ARGS="-e ${PARAM_PATH_ARGS}"
eval "${PYTHON_ENV_TOOL:-pip} install ${PYTHON_INSTALL_ARGS} ${PARAM_ADDITIONAL_ARGS}"
;;
poetry)
PYTHON_INSTALL_ARGS="--no-ansi"
eval "poetry install ${PYTHON_INSTALL_ARGS} ${PARAM_ADDITIONAL_ARGS}"
;;
uv)
PYTHON_INSTALL_ARGS=""
eval "uv sync ${PYTHON_INSTALL_ARGS} ${PARAM_ADDITIONAL_ARGS}"
;;
esac
environment:
PARAM_ADDITIONAL_ARGS: << parameters.args >>
PARAM_DEPENDENCY_FILE: << parameters.pip-dependency-file >>
PARAM_PATH_ARGS: << parameters.path-args >>
PARAM_PKG_MNGR: << parameters.pkg-manager >>
name: Install dependencies with automatically determined project package manager
no_output_timeout: << parameters.no_output_timeout >>
working_directory: << parameters.app-dir >>
- when:
condition:
equal:
- pipenv
- << parameters.pkg-manager >>
steps:
- run:
command: |
pipenv install << parameters.args >>
name: Install dependencies with pipenv using project Pipfile or inline packages
no_output_timeout: << parameters.no_output_timeout >>
working_directory: << parameters.app-dir >>
- when:
condition:
equal:
- poetry
- << parameters.pkg-manager >>
steps:
- run:
command: |
poetry install --no-ansi << parameters.args >>
name: Install dependencies with poetry using project pyproject.toml
no_output_timeout: << parameters.no_output_timeout >>
working_directory: << parameters.app-dir >>
- when:
condition:
equal:
- uv
- << parameters.pkg-manager >>
steps:
- run:
command: |
uv sync << parameters.args >>
name: Install dependencies with uv using project pyproject.toml and uv.lock
no_output_timeout: << parameters.no_output_timeout >>
working_directory: << parameters.app-dir >>
- when:
condition:
and:
- equal:
- pip
- << parameters.pkg-manager >>
- or:
- <<parameters.args>>
- <<parameters.pip-dependency-file>>
steps:
- run:
command: |
pip install <<#parameters.pip-dependency-file>>-r <<parameters.pip-dependency-file>><</parameters.pip-dependency-file>> << parameters.args >>
name: Install dependencies with pip using project <<parameters.pip-dependency-file>>
no_output_timeout: << parameters.no_output_timeout >>
working_directory: <<parameters.app-dir>>
- when:
condition:
equal:
- pip-dist
- << parameters.pkg-manager >>
steps:
- run:
command: |
pip install -e << parameters.path-args >> << parameters.args >>
name: Install dependencies with pip using project setup.py or pyproject.toml
no_output_timeout: << parameters.no_output_timeout >>
working_directory: <<parameters.app-dir>>
- when:
condition:
or:
- << parameters.pypi-cache >>
- << parameters.venv-cache >>
steps:
- run:
command: "# shellcheck source=detect-env.sh\neval \"$SCRIPT_UTILS\"\ndetect_os\nsource \"$AUTO_DETECT_ENV_SCRIPT\"\n\ncase ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} in\n pip | pip-dist)\n LOCK_FILE=\"${PARAM_DEPENDENCY_FILE:-requirements.txt}\"\n CACHE_PATHS='[ \"/home/circleci/.cache/pip\", \"/home/circleci/.pyenv/versions\", \"/home/circleci/.local/lib\" ]'\n ;;\n pipenv) \n # TODO: use PIPENV_PIPFILE\n LOCK_FILE=\"Pipfile.lock\"\n PIPENV_VENV_PATH=\"${WORKON_HOME:-/home/circleci/.local/share/virtualenvs}\"\n \n if [ -z \"${PIPENV_VENV_IN_PROJECT}\" ]; then\n VENV_PATHS=\"[ \\\"${PIPENV_VENV_PATH}\\\" ]\"\n else\n VENV_PATHS=\"[ \\\"${CIRCLE_WORKING_DIRECTORY}/.venvs\\\" ]\"\n fi\n \n CACHE_PATHS='[ \"/home/circleci/.cache/pip\", \"/home/circleci/.cache/pipenv\" ]'\n ;;\n poetry)\n LOCK_FILE=\"poetry.lock\"\n VENV_PATHS='[ \"/home/circleci/.cache/pypoetry/virtualenvs\" ]'\n CACHE_PATHS='[ \"/home/circleci/.cache/pip\" ]'\n ;;\n uv)\n LOCK_FILE=\"uv.lock\"\n VENV_PATHS=\"[ \\\"${CIRCLE_WORKING_DIRECTORY}/.venv\\\" ]\"\n CACHE_PATHS='[ \"/home/circleci/.cache/uv\" ]'\n ;;\nesac\n\nif [ -n \"${PARAM_VENV_PATH}\" ]; then\n VENV_PATHS=\"${PARAM_VENV_PATH}\"\nfi\nPARAM_CACHE_FOLDER_PREFIX=\"$(echo \"$PARAM_CACHE_FOLDER_PREFIX\" | circleci env subst)\"\n\nif [[ \"$PARAM_CACHE_FOLDER_PREFIX\" == /* ]]; then\n if [[ \"$PLATFORM\" == \"windows\" ]]; then\n CACHE_PREFIX=\"/c$PARAM_CACHE_FOLDER_PREFIX\"\n else\n CACHE_PREFIX=\"$PARAM_CACHE_FOLDER_PREFIX\"\n fi\n\nelse\n CACHE_PREFIX=\"${PWD%/\"$PARAM_APP_DIR\"}/$PARAM_CACHE_FOLDER_PREFIX\"\nfi\n\nCACHE_DIR=\"${CACHE_PREFIX%/}/.cci_pycache\"\nmkdir -p \"${CACHE_DIR}\"\n\nlink_paths() {\n if [ -d \"${1}\" ]; then\n echo \"INFO: Cache directory already exists. Deleting...\"\n rm -r \"${1}\"\n # return\n fi\n \n mkdir \"${1}\"\n \n for encoded in $(echo \"${2}\" | jq -r '.[] | @base64'); do\n decoded=$(echo \"${encoded}\" | tr -d '\\r' | base64 -d)\n \n if [ -e \"${decoded}\" ]; then\n echo \"INFO: Copying ${decoded} to ${1}/${encoded}\"\n cp -a \"${decoded}\" \"${1}/${encoded}\"\n else\n echo \"INFO: Could not find ${decoded}. Skipping...\"\n fi\n done\n}\n\nif [ \"${PARAM_VENV_CACHE}\" = \"1\" ] && [ -n \"${VENV_PATHS}\" ]; then\n link_paths \"${CACHE_DIR}/venv\" \"${VENV_PATHS}\"\nfi\n\nif [ \"${PARAM_PYPI_CACHE}\" = \"1\" ]; then\n link_paths \"${CACHE_DIR}/pypi\" \"${CACHE_PATHS}\"\nfi\n\nLOCKFILE_PATH=\"${CACHE_DIR}/lockfile\"\n\nif [ -e \"${LOCKFILE_PATH}\" ]; then\n rm -f \"${LOCKFILE_PATH}\"\nfi\n\nif [ -e \"${LOCK_FILE}\" ]; then\n FULL_LOCK_FILE=$(readlink -f \"${LOCK_FILE}\")\n \n echo \"INFO: Copying ${FULL_LOCK_FILE} to ${LOCKFILE_PATH}\"\n cp \"${FULL_LOCK_FILE}\" \"${LOCKFILE_PATH}\"\nfi\n"
environment:
PARAM_APP_SRC_DIR: <<parameters.app-dir>>
PARAM_CACHE_FOLDER_PREFIX: <<parameters.cache-folder-prefix>>
PARAM_DEPENDENCY_FILE: << parameters.pip-dependency-file >>
PARAM_PKG_MNGR: << parameters.pkg-manager >>
PARAM_PYPI_CACHE: << parameters.pypi-cache >>
PARAM_VENV_CACHE: << parameters.venv-cache >>
PARAM_VENV_PATH: << parameters.venv-path >>
SCRIPT_UTILS: |4-
detect_os() {
detected_platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$detected_platform" in
linux*)
export PLATFORM=linux
;;
darwin*)
export PLATFORM=macos
;;
msys*|cygwin*)
export PLATFORM=windows
;;
*)
printf '%s\n' "Unsupported OS: \"$detected_platform\"."
exit 1
;;
esac
}
name: Copy to cache directory
working_directory: << parameters.app-dir >>
- save_cache:
key: <<#parameters.cache-key-override>><<parameters.cache-key-override>><</parameters.cache-key-override>><<^parameters.cache-key-override>><<parameters.cache-version>>-cci_pycache-<<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>><<#parameters.include-python-in-cache-key>>{{ checksum "<<parameters.cache-folder-prefix>>/.temp-python-version" }}-<</parameters.include-python-in-cache-key>>{{ checksum "<<parameters.cache-folder-prefix>>/.cci_pycache/lockfile" }}<</parameters.cache-key-override>>
paths:
- <<parameters.cache-folder-prefix>>/.cci_pycache
executors:
default:
description: The official CircleCI CIMG Python Docker image.
docker:
- image: cimg/python:<< parameters.tag >>
parameters:
tag:
default: "3.14"
description: The `cimg/python` Docker image version tag.
type: string
jobs:
test:
description: |
Simple drop-in job to setup a python project, run tests and store the test results if possible.
If test-tool is pytest, the job assumes the project defines pytest in the dependency file.
Otherwise, use the built in unittest module as the test-tool.
executor: <<parameters.executor>>
parameters:
app-dir:
default: ~/project
description: Path to the directory containing your python project. Not needed if dependency file lives in the root.
type: string
args:
default: ""
description: Arguments to pass to install command for pipenv, poetry or uv. Override '-r requirements.txt' for pip.
type: string
cache-folder-prefix:
default: /tmp/cache
description: |
A directory where you would like to save the cache files. Defaults to empty, saving the cache to project folder.
If you don't want the cache in your project directory, specify the absolute path of an existing directory here.
type: string
cache-version:
default: v1
description: Change the default cache version if you need to clear the cache for any reason.
type: string
ensure-test-tool:
default: true
description: |
If true, a step will be ran to ensure the configured test tool is installed.
type: boolean
executor:
default: default
description: |
The name of executor to use.
type: executor
fail-if-missing-tool:
default: false
description: |
If true, this job will fail if the test tool was not found. If false, it will automatically install to recover.
type: boolean
include-branch-in-cache-key:
default: true
description: |
If true, this cache bucket will only apply to jobs within the same branch.
type: boolean
include-python-in-cache-key:
default: true
description: |
If true, this cache bucket will checksum the pyenv python version with the cache-key
type: boolean
module-src-dir:
default: src
description: Path relative to app-dir which contains module source.
type: string
pip-dependency-file:
default: requirements.txt
description: Name of the requirements file that needs to be installed with pip. Prepended with `app-dir`. If using pipenv, poetry or uv, this is ignored.
type: string
pkg-manager:
default: auto
description: Select the package manager to use. Default is pip
enum:
- auto
- pip
- pipenv
- poetry
- pip-dist
- uv
type: enum
pre-install-steps:
default: []
description: |
Steps needed between restoring the cache and the install step.
type: steps
pypi-cache:
default: true
description: Keep all versions of pypi and site-package caches for faster rebuilding overall.
type: boolean
setup:
default: []
description: Provide any optional steps you would like to run prior to install the python project.
type: steps
setup-file-path:
default: ""
description: Path to the setup.py file.
type: string
test-tool:
default: pytest
description: The tool to run the tests with.
enum:
- pytest
- unittest
type: enum
test-tool-args:
default: ""
description: Arguments to pass to test tool, i.e. discovery settings for unittest - 'discover -s tests_dir'.
type: string
venv-cache:
default: true
description: Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager.
type: boolean
steps:
- checkout
- steps: << parameters.setup >>
- install-packages:
app-dir: <<parameters.app-dir>>
args: <<parameters.args>>
cache-folder-prefix: <<parameters.cache-folder-prefix>>
cache-version: <<parameters.cache-version>>
include-branch-in-cache-key: <<parameters.include-branch-in-cache-key>>
include-python-in-cache-key: <<parameters.include-python-in-cache-key>>
pip-dependency-file: <<parameters.pip-dependency-file>>
pkg-manager: <<parameters.pkg-manager>>
pre-install-steps: <<parameters.pre-install-steps>>
pypi-cache: <<parameters.pypi-cache>>
venv-cache: <<parameters.venv-cache>>
- when:
condition: <<parameters.ensure-test-tool>>
steps:
- run:
command: |
# shellcheck source=detect-env.sh
source "$AUTO_DETECT_ENV_SCRIPT"
case ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} in
pip)
REQUIREMENTS_PATH=${PARAM_REQUIREMENTS_PATH:-requirements.txt}
;;
pip-dist)
REQUIREMENTS_PATH="requirements.txt"
;;
pipenv) # TODO: use PIPENV_PIPFILE
REQUIREMENTS_PATH="Pipfile"
PYTHON_ENV_TOOL="pipenv"
;;
poetry)
PYTHON_INSTALL_ARGS="--no-ansi"
REQUIREMENTS_PATH="pyproject.toml"
PYTHON_ENV_TOOL="poetry"
;;
uv)
REQUIREMENTS_PATH="uv.lock"
PYTHON_ENV_TOOL="uv"
;;
esac
if [ -f ${REQUIREMENTS_PATH} ]; then
echo "INFO: Detected dependency file: $REQUIREMENTS_PATH"
else
echo "WARNING: No dependency file for ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} found. ${REQUIREMENTS_PATH} expected."
fi
# Automatically install test package. unittest is preinstalled and not required.
if [ "${PARAM_TEST_TOOL}" != "unittest" ]; then
if ! eval "${PYTHON_ENV_TOOL:+$PYTHON_ENV_TOOL run} pip --disable-pip-version-check list" | awk 'NR > 2 && NF > 0 { print $1 }' | grep -q "^${PARAM_TEST_TOOL}$"; then
if [ "${PARAM_FAIL_IF_MISSING_TOOL}" = true ]; then
echo "ERROR: Test package ${PARAM_TEST_TOOL} was not found"
exit 1
fi
# If the test package is not detected, install using PYTHON_INSTALL_TOOL
echo "INFO: Test package ${PARAM_TEST_TOOL} was not found. Installing..."
if [ "$PYTHON_ENV_TOOL" = "uv" ]; then
eval "uv add ${PYTHON_INSTALL_ARGS} ${PARAM_TEST_TOOL}"
else
eval "${PYTHON_ENV_TOOL:-pip} install ${PYTHON_INSTALL_ARGS} ${PARAM_TEST_TOOL}"
fi
else
echo "INFO: Detected test package: $DETECT_TEST_TOOL"
fi
fi
environment:
PARAM_FAIL_IF_MISSING_TOOL: <<parameters.fail-if-missing-tool>>>
PARAM_PKG_MNGR: <<parameters.pkg-manager>>
PARAM_SETUP_FILE_PATH: <<parameters.setup-file-path>>>
PARAM_TEST_TOOL: <<parameters.test-tool>>
name: Ensuring test tool is installed
working_directory: <<parameters.app-dir>>
- when:
condition:
equal:
- auto
- << parameters.pkg-manager >>
steps:
- run:
command: mkdir test-report
name: Create test-report directory
working_directory: <<parameters.app-dir>>
- run:
command: |-
# shellcheck source=detect-env.sh
source "$AUTO_DETECT_ENV_SCRIPT"
if [ "${PARAM_TEST_TOOL}" = "pytest" ]; then
INSTALL_COMMAND="pytest --junit-xml=test-report/report.xml ${PARAM_TEST_TOOL_ARGS}"
else
INSTALL_COMMAND="python -m unittest ${PARAM_TEST_TOOL_ARGS}"
fi
if [ -n "${PYTHON_ENV_TOOL}" ]; then
eval "${PYTHON_ENV_TOOL} run ${INSTALL_COMMAND}"
else
eval "${INSTALL_COMMAND}"
fi
environment:
PARAM_PKG_MNGR: <<parameters.pkg-manager>>
PARAM_TEST_TOOL: <<parameters.test-tool>>
PARAM_TEST_TOOL_ARGS: <<parameters.test-tool-args>>
PYTHONPATH: << parameters.app-dir >>/<< parameters.module-src-dir >>
name: Run tests with auto-detected packages run
working_directory: <<parameters.app-dir>>
- store_test_results:
path: <<#parameters.app-dir>><<parameters.app-dir>>/<</parameters.app-dir>>test-report
- when:
condition:
and:
- equal:
- unittest
- << parameters.test-tool >>
- not:
equal:
- auto
- << parameters.pkg-manager >>
steps:
- when:
condition:
or:
- equal:
- poetry
- << parameters.pkg-manager >>
- equal:
- pipenv
- << parameters.pkg-manager >>
- equal:
- uv
- << parameters.pkg-manager >>
steps:
- run:
command: <<parameters.pkg-manager>> run python -m unittest << parameters.test-tool-args >>
name: Run tests with <<parameters.pkg-manager>> run
- when:
condition:
or:
- equal:
- pip
- << parameters.pkg-manager >>
- equal:
- pip-dist
- << parameters.pkg-manager >>
steps:
- run:
command: python -m unittest << parameters.test-tool-args >>
environment:
PYTHONPATH: << parameters.app-dir >>/<< parameters.module-src-dir >>
name: Run tests with global python env
working_directory: <<parameters.app-dir>>
- when:
condition:
and:
- equal:
- pytest
- << parameters.test-tool >>
- not:
equal:
- auto
- << parameters.pkg-manager >>
steps:
- run:
command: mkdir test-report
name: Create test-report directory
working_directory: <<parameters.app-dir>>
- when:
condition:
or:
- equal:
- poetry
- << parameters.pkg-manager >>
- equal:
- pipenv
- << parameters.pkg-manager >>
- equal:
- uv
- << parameters.pkg-manager >>
steps:
- run:
command: <<parameters.pkg-manager>> run pytest --junit-xml=test-report/report.xml << parameters.test-tool-args >>
name: Run tests with <<parameters.pkg-manager>> run
working_directory: <<parameters.app-dir>>
- when:
condition:
or:
- equal:
- pip
- << parameters.pkg-manager >>
- equal:
- pip-dist
- << parameters.pkg-manager >>
steps:
- run:
command: pytest --junit-xml=test-report/report.xml << parameters.test-tool-args >>
environment:
PYTHONPATH: << parameters.app-dir >>/<< parameters.module-src-dir >>
name: Run tests with global python env
working_directory: <<parameters.app-dir>>
- store_test_results:
path: <<#parameters.app-dir>><<parameters.app-dir>>/<</parameters.app-dir>>test-report
examples:
using-test-job:
description: |
An example of running a pipenv based project with pytest. And then using twine to publish to pypi.
The project using these args would have pytest as a dev dependency in its Pipfile.
The CircleCI project settings would have the environment variables for twine auth.
usage:
version: "2.1"
orbs:
python: circleci/python@4.0.0
jobs:
publish:
executor:
name: python/default
tag: "3.14"
steps:
- checkout
- python/dist
- run: pip install twine && twine upload dist/*
workflows:
main:
jobs:
- python/test:
args: --dev
pkg-manager: pipenv
test-tool: pytest
- publish
work-with-pip:
description: |
An example of working with the Pip and caching requirements.txt on CircleCI to speed up builds.
usage:
version: "2.1"
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: "3.14"
steps:
- checkout
- python/install-packages:
pkg-manager: pip
- python/install-packages:
cache-key-override: pip-{{ checksum 'dev-requirements.txt' }}-{{ checksum 'requirements.txt' }}
pkg-manager: pip
- python/install-packages:
pip-dependency-file: dev-requirements.txt
pkg-manager: pip
- python/install-packages:
args: pytest
pkg-manager: pip
pypi-cache: false
- run:
command: |
pytest --version
name: Test
workflows:
main:
jobs:
- build
work-with-pipenv:
description: |
An example of working with the Pipenv cache on CircleCI to speed up builds.
usage:
version: "2.1"
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: "3.14"
steps:
- checkout
- python/install-packages:
args: pytest
pkg-manager: pipenv
- run:
command: |
pipenv run pytest --version
name: Test it
workflows:
main:
jobs:
- build
work-with-poetry:
description: |
An example of working with the Poetry cache on CircleCI to speed up builds.
usage:
version: "2.1"
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: "3.14"
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
- run:
command: |
poetry run pytest --version
name: Test it
workflows:
main:
jobs:
- build
work-with-uv:
description: |
An example of working with the uv cache on CircleCI to speed up builds.
usage:
version: "2.1"
orbs:
python: circleci/python@4.0.0
jobs:
build:
executor:
name: python/default
tag: "3.14"
steps:
- checkout
- python/install-packages:
pkg-manager: uv
- run:
command: |
uv run pytest --version
name: Test it
workflows:
main:
jobs:
- build