Slide 1

Slide 1 text

PyConPL 2023 Sebastian Witowski Python CI 101 A Beginner's Guide to Continuous Integration

Slide 2

Slide 2 text

PyConPL 2023 Sebastian Witowski Python CI 101 A Beginner's Guide to Continuous Integration

Slide 3

Slide 3 text

A bit of history

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Internet == Collaboration

Slide 6

Slide 6 text

Tabs! Spaces!

Slide 7

Slide 7 text

The End

Slide 8

Slide 8 text

CI

Slide 9

Slide 9 text

That's why we need Continuous Integration

Slide 10

Slide 10 text

What is Continuous Integration? It's a process of merging developer's code into the main repository.

Slide 11

Slide 11 text

What is Continuous Integration? It's a process of merging developer's code into the main repository.

Slide 12

Slide 12 text

What is Continuous Integration? It's a process of merging developer's code into the main repository. Are tests passing?

Slide 13

Slide 13 text

What is Continuous Integration? It's a process of merging developer's code into the main repository. What about the code style? Are tests passing?

Slide 14

Slide 14 text

What is Continuous Integration? It's a process of merging developer's code into the main repository. What about the code style? Static analysis (flake8/pylint) Are tests passing?

Slide 15

Slide 15 text

What is Continuous Integration? It's a process of merging developer's code into the main repository. What about the code style? Static analysis (flake8/pylint) Are tests passing? Merge conflicts?

Slide 16

Slide 16 text

Automation • Release packages • Build Docker images • Publish documentation

Slide 17

Slide 17 text

Main role of the CI

Slide 18

Slide 18 text

Main role of the CI Automate things • Tests • Static analysis • Build/publish/release

Slide 19

Slide 19 text

Main role of the CI Automate things • Tests • Static analysis • Build/publish/release Ensure consistency • Different IDE/OS • File path separators • LF vs. CRLF • And yes, tabs vs. spaces

Slide 20

Slide 20 text

Bad review

Slide 21

Slide 21 text

Good review Bad review

Slide 22

Slide 22 text

Choosing a CI platform

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Choosing a CI platform

Slide 28

Slide 28 text

The anatomy of a CI

Slide 29

Slide 29 text

The anatomy of a CI

Slide 30

Slide 30 text

The anatomy of a CI

Slide 31

Slide 31 text

The anatomy of a CI

Slide 32

Slide 32 text

https:/ /gitlab.com/switowski/python-ci-101

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Our first pipeline

Slide 35

Slide 35 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye

Slide 36

Slide 36 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest .

Slide 37

Slide 37 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff .

Slide 38

Slide 38 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 39

Slide 39 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 40

Slide 40 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 41

Slide 41 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 42

Slide 42 text

Our first pipeline

Slide 43

Slide 43 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 44

Slide 44 text

Our first pipeline (alternative version) # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: script: - pip install black - black --check --diff . flake8: allow_failure: true script: - pip install flake8 - flake8 --verbose .

Slide 45

Slide 45 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 46

Slide 46 text

Our first pipeline # .gitlab-ci.yml image: python:3.11-slim-bullseye test: script: - pip install pytest - pytest . black: image: registry.gitlab.com/pipeline-components/black:latest script: - black --check --diff . flake8: allow_failure: true image: registry.gitlab.com/pipeline-components/flake8:0.11.2 script: - flake8 --verbose .

Slide 47

Slide 47 text

Let's fix the pipeline

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Config files # setup.cfg [flake8] max-line-length = 120 # pyproject.toml [tool.black] line-length = 120

Slide 53

Slide 53 text

Fixed pipeline

Slide 54

Slide 54 text

Other tools for the CI

Slide 55

Slide 55 text

Other tools for the CI • Type checks (e.g. mypy) • Security checks (e.g. bandit) • Unused code detectors (e.g. vulture) • Tools combining other tools (e.g. prospector) • Commercial static analysis tools

Slide 56

Slide 56 text

Other tools for the CI • Type checks (e.g. mypy) • Security checks (e.g. bandit) • Unused code detectors (e.g. vulture) • Tools combining other tools (e.g. prospector) • Commercial static analysis tools + plugins

Slide 57

Slide 57 text

Display test coverage

Slide 58

Slide 58 text

Test coverage test: script: - pip install pytest - pytest .

Slide 59

Slide 59 text

Test coverage # Source: https://docs.gitlab.com/ee/ci/testing/ test_coverage_visualization.html#python-example test: script: - pip install pytest pytest-cov - pytest --cov --cov-report term --cov-report xml:coverage.xml coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/' artifacts: reports: coverage_report: coverage_format: cobertura path: coverage.xml

Slide 60

Slide 60 text

Test coverage

Slide 61

Slide 61 text

Finding Balance

Slide 62

Slide 62 text

Running linters on your computer

Slide 63

Slide 63 text

Running linters on your computer • Enable a bunch of plugins in you IDE • Use pre-commit

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

pre-commit default_language_version: python: python3.11 repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: 23.3.0 hooks: - id: check-merge-conflict - id: check-toml - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/psf/black rev: 'refs/tags/23.3.0:refs/tags/23.3.0' hooks: - id: black alias: autoformat - repo: https://github.com/pycqa/flake8 rev: 6.0.0 hooks: - id: flake8

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

pre-commit • Makes pipelines faster (and cheaper) by moving checks to your computer

Slide 68

Slide 68 text

pre-commit • Makes pipelines faster (and cheaper) by moving checks to your computer • Everyone on your project needs to set it up correctly

Slide 69

Slide 69 text

Run linters and formatters in: Your IDE on save For a real-time feedback as you write code Combining it all together

Slide 70

Slide 70 text

Your IDE on save For a real-time feedback as you write code pre-commit hooks Shareable configuration Faster feedback than a CI pipeline Combining it all together Run linters and formatters in:

Slide 71

Slide 71 text

Your IDE on save For a real-time feedback as you write code pre-commit hooks Shareable configuration Faster feedback than a CI pipeline CI Automated way to check everyone's code Combining it all together Run linters and formatters in:

Slide 72

Slide 72 text

Best practices

Slide 73

Slide 73 text

Best practices •Run jobs in parallel

Slide 74

Slide 74 text

Best practices •Run jobs in parallel •Cache things between jobs 
 store docker images in container registry

Slide 75

Slide 75 text

Best practices •Run jobs in parallel •Cache things between jobs 
 store docker images in container registry •Fail fast 
 pytest -x

Slide 76

Slide 76 text

Best practices •Run jobs in parallel •Cache things between jobs 
 store docker images in container registry •Fail fast 
 pytest -x •Keep MR pipelines fast

Slide 77

Slide 77 text

Best practices •Run jobs in parallel •Cache things between jobs 
 store docker images in container registry •Fail fast 
 pytest -x •Keep MR pipelines fast •Keep your CI setup close to your production environment

Slide 78

Slide 78 text

Thank you! switowski.com @SebaWitowski

Slide 79

Slide 79 text

• 1st slide: https:/ /www.midjourney.com/app/search/?jobId=e4145c75-beb5-4d1f-8ab3-df613868bc2b • Caveman with a computer: https:/ /www.midjourney.com/app/search/?jobId=2ae3ba28-0ad4-4fc7-924d-c035dbac5767 • Computers in middle ages: https:/ /www.midjourney.com/app/jobs/9818d80c-91de-49c4-b406-ab422f802259/ • War: https:/ /www.midjourney.com/app/search/?jobId=73615195-2716-47c6-9ea2-06451496ad72 • Tombstone: https:/ /www.midjourney.com/app/search/?jobId=e6231e8d-ed31-4cf7-b7e5-ac146dd77c63 • Chalice in cave: https:/ /www.midjourney.com/app/jobs/27e13667-8625-4df3-a6bb-2a3cc4958109/ • Pressing a button: https:/ /www.midjourney.com/app/search/?jobId=32357809-c329-4451-9206-a78c6b1f3eee • Robot using a computer: https:/ /www.midjourney.com/app/jobs/4b3df90d-207d-4d70-8a8d-ad0197652fb3/ • Gitlab logo: https:/ /www.midjourney.com/app/search/?jobId=a5491c2d-199e-43dd-b65f-9ebefc031f65 • Octocat: https:/ /www.midjourney.com/app/search/?jobId=77c1c017-1e1e-4d19-bc49-347c780bdf80 • Balance: https:/ /www.midjourney.com/app/search/?jobId=03f4ea11-dd9b-4404-bfc0-88a3ad2a6d81 • Robots in a factory: https:/ /www.midjourney.com/app/search/?jobId=e9d9f29b-4546-42f7-b415-f33b4acddf03 Attributions Most images come from midjourney.com Drawings were done with excalidraw.com

Slide 80

Slide 80 text

Questions? switowski.com @SebaWitowski https:/ /gitlab.com/switowski/python-ci-101 Slides: https:/ /speakerdeck.com/switowski