Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
python: circleci/python@3.0.0
Use 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 dependecy 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
version: '2.1'
orbs:
python: circleci/python@3.0.0
jobs:
publish:
executor: python/default
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
version: '2.1'
orbs:
python: circleci/python@3.0.0
jobs:
build:
executor: python/default
steps:
- checkout
- python/install-packages:
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
version: '2.1'
orbs:
python: circleci/python@3.0.0
jobs:
build:
executor: python/default
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
version: '2.1'
orbs:
python: circleci/python@3.0.0
jobs:
build:
executor: python/default
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
- run:
command: |
poetry 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. | No | ~/project | string |
args | Arguments to pass to install command for pipenv and poetry. Override '-r requirements.txt' for pip. | No | '' | string |
cache-version | Change the default cache version if you need to clear the cache for any reason. | No | v1 | string |
ensure-test-tool | If true, a step will be ran to ensure the configured test tool is installed.
| No | true | boolean |
executor | The name of executor to use.
| No | default | 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.
| No | false | boolean |
include-branch-in-cache-key | If true, this cache bucket will only apply to jobs within the same branch.
| No | true | boolean |
include-python-in-cache-key | If true, this cache bucket will checksum the pyenv python version with the cache-key
| No | true | boolean |
module-src-dir | Path relative to app-dir which contains module source. | No | src | 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. | No | requirements.txt | string |
pkg-manager | Select the package manager to use. Default is pip | No | auto | enum |
pre-install-steps | Steps needed between restoring the cache and the install step.
| No | [] | steps |
pypi-cache | Keep all versions of pypi and site-package caches for faster rebuilding overall. | No | true | boolean |
setup | Provide any optional steps you would like to run prior to install the python project. | No | [] | steps |
setup-file-path | Path to the setup.py file. | No | '' | string |
test-tool | The tool to run the tests with. | No | pytest | enum |
test-tool-args | Arguments to pass to test tool, i.e. discovery settings for unittest - 'discover -s tests_dir'. | No | '' | string |
venv-cache | Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager. | No | true | 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. | No | . | string |
build-tool | Choose the build command to run. Options include PEP427 wheel (default) and PEP517 build. | No | wheel | enum |
no-isolation | Do not isolate the build in a virtual environment. Exclusive to build-tool option "build" only. | No | false | boolean |
out-dir | Path where to write the output files. | No | ./dist | string |
sdist-separate | Opt out of default behavior and build sdist independently from wheel. Exclusive to build-tool option "build" only. | No | false | boolean |
skip-dependency-check | Do not check that build dependencies are installed. Exclusive to build-tool option "build" only. | No | false | 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. | No | false | 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. | No | . | 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.
| No | '' | string |
cache-version | Change the default cache version if you need to clear the cache for any reason. | No | v1 | string |
include-branch-in-cache-key | If true, this cache bucket will only apply to jobs within the same branch.
| No | true | boolean |
include-python-in-cache-key | If true, this cache bucket will checksum the pyenv python version with the cache-key.
| No | true | boolean |
no_output_timeout | Elapsed time the command can run without output. Passed to install command. | No | 10m | 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`.
| No | . | 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.
| No | requirements.txt | 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). | No | auto | enum |
pre-install-steps | Steps needed between restoring the cache and the install step.
| No | [] | steps |
pypi-cache | Keep all versions of pypi and site-package caches for faster rebuilding overall. | No | true | boolean |
venv-cache | Use the lockfile to cache the virtualenv. Not used with pip as pkg-manager. | No | true | boolean |
venv-path | Override venv path. As json array - ex '[ "path", "path2" ]' | No | '' | string |
The official CircleCI CIMG Python Docker image.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
tag | The `cimg/python` Docker image version tag. | No | '3.8' | 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
# 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-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
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 "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: "# shellcheck source=detect-env.sh\nsource \"$AUTO_DETECT_ENV_SCRIPT\"\n\nCACHE_DIR=\".cci_pycache\"\nLOCKFILE_PATH=\"${CACHE_DIR}/lockfile\"\n\nmkdir -p \"${CACHE_DIR}\"\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 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_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 >>
name: Link lockfile
working_directory: << parameters.app-dir >>
- run:
command: python --version | cut -d ' ' -f2 > .temp-python-version && cat .temp-python-version
name: Save python version
- restore_cache:
keys:
- <<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 ".temp-python-version" }}-<</parameters.include-python-in-cache-key>>{{ checksum "<<parameters.app-dir>>/.cci_pycache/lockfile" }}
- <<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 ".temp-python-version" }}-<</parameters.include-python-in-cache-key>>
- run:
command: "recurse() {\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}\n\nCACHE_DIR=\".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_PYPI_CACHE: << parameters.pypi-cache >>
PARAM_VENV_CACHE: << parameters.venv-cache >>
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}"
;;
pip-dist)
PYTHON_INSTALL_ARGS="-e ${PARAM_PATH_ARGS}"
;;
poetry)
PYTHON_INSTALL_ARGS="--no-ansi"
;;
esac
eval "${PYTHON_ENV_TOOL:-pip} install ${PYTHON_INSTALL_ARGS} ${PARAM_ADDITIONAL_ARGS}"
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:
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\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) # 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 ;;\nesac\n\nif [ -n \"${PARAM_VENV_PATH}\" ]; then\n VENV_PATHS=\"${PARAM_VENV_PATH}\"\nfi\n\nCACHE_DIR=\".cci_pycache\"\nmkdir -p \"${CACHE_DIR}\"\n\nlink_paths() {\n if [ -d \"${1}\" ]; then\n echo \"INFO: Cache directory already exists. Skipping...\"\n return\n fi\n \n mkdir \"${1}\"\n \n for encoded in $(echo \"${2}\" | jq -r '.[] | @base64'); do\n decoded=$(echo \"${encoded}\" | 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"
environment:
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 >>
name: Copy to cache directory
working_directory: << parameters.app-dir >>
- save_cache:
key: <<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 ".temp-python-version" }}-<</parameters.include-python-in-cache-key>>{{ checksum "<<parameters.app-dir>>/.cci_pycache/lockfile" }}
paths:
- <<parameters.app-dir>>/.cci_pycache
executors:
default:
description: The official CircleCI CIMG Python Docker image.
docker:
- image: cimg/python:<< parameters.tag >>
parameters:
tag:
default: "3.8"
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 and poetry. Override '-r requirements.txt' for pip.
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 or poetry, 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
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-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\nsource \"$AUTO_DETECT_ENV_SCRIPT\"\n\ncase ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} in\n pip)\n REQUIREMENTS_PATH=${PARAM_REQUIREMENTS_PATH:-requirements.txt}\n ;;\n pip-dist)\n REQUIREMENTS_PATH=\"requirements.txt\"\n ;;\n pipenv) # TODO: use PIPENV_PIPFILE\n REQUIREMENTS_PATH=\"Pipfile\"\n PYTHON_ENV_TOOL=\"pipenv\"\n ;;\n poetry)\n PYTHON_INSTALL_ARGS=\"--no-ansi\"\n REQUIREMENTS_PATH=\"pyproject.toml\"\n PYTHON_ENV_TOOL=\"poetry\"\n ;;\nesac\n\nif [ -f ${REQUIREMENTS_PATH} ]; then\n echo \"INFO: Detected dependency file: $REQUIREMENTS_PATH\"\nelse\n echo \"WARNING: No dependency file for ${DETECT_PKG_MNGR:-${PARAM_PKG_MNGR}} found. ${REQUIREMENTS_PATH} expected.\"\nfi\n\n# Automatically install test package. unittest is preinstalled and not required.\nif [ \"${PARAM_TEST_TOOL}\" != \"unittest\" ]; then\n DETECT_TEST_TOOL=$(eval \"${PYTHON_ENV_TOOL:+$PYTHON_ENV_TOOL run} pip --disable-pip-version-check list\" |\n awk 'NR > 2 && NF > 0 { print $1 }' | grep \"^${PARAM_TEST_TOOL}$\")\n \n NOT_DETECTED=$?\n \n if (( NOT_DETECTED > 0 )) && [ \"${PARAM_FAIL_IF_MISSING_TOOL}\" = true ]; then\n exit $NOT_DETECTED\n fi\n \n # If the test package is not detected, install using PYTHON_INSTALL_TOOL\n if [ -z \"$DETECT_TEST_TOOL\" ]; then\n echo \"INFO: Test package ${PARAM_TEST_TOOL} was not found. Installing...\"\n eval \"${PYTHON_ENV_TOOL:-pip} install ${PYTHON_INSTALL_ARGS} ${PARAM_TEST_TOOL}\"\n INSTALL_RESULT=$?\n else\n echo \"INFO: Detected test package: $DETECT_TEST_TOOL\"\n fi\n \n # Exit with test package install result, or exit 0 if param fail is set to false\n if (( NOT_DETECTED > 0 )) && [ \"${PARAM_FAIL_IF_MISSING_TOOL}\" = false ]; then\n exit ${INSTALL_RESULT:-0}\n fi\nfi"
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 >>
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 >>
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 dependecy in its Pipfile.
The CircleCI project settings would have the environment variables for twine auth.
usage:
version: "2.1"
orbs:
python: circleci/python@3.0.0
jobs:
publish:
executor: python/default
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@3.0.0
jobs:
build:
executor: python/default
steps:
- checkout
- python/install-packages:
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@3.0.0
jobs:
build:
executor: python/default
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@3.0.0
jobs:
build:
executor: python/default
steps:
- checkout
- python/install-packages:
pkg-manager: poetry
- run:
command: |
poetry run pytest --version
name: Test it
workflows:
main:
jobs:
- build