transition | logo |
---|---|
fade |
Continuous Integration for Python
Marseille (France) - 2017/07/05
- :fa-twitter:
@julienmaupetit
- PhD in structural bioinformatics (Paris Diderot)
- Research engineer (RPBS platform, Paris Diderot)
- Co-founder of TailorDev (Clermont-Ferrand)
What about you?
Do you
git
,bash
,python
?
- A brief theoretical introduction to CI
- Configure GitLab-CI
- Test an application with py.test
For the practical part, you will need:
- a UNIX shell (
bash
,zsh
, …) git
python >= 3.4
ping www.google.fr
Ready?
Test & add CI to
climate
, a python application that analyzes average temperature records by country since the 18th century.
- Load data from the Climate Change: Earth Surface Temperature Data Kaggle dataset (csv),
- Perform calculations on the data,
- Generate a plot.
There is no continuous integration for applications with no tests.
Shipping an application (or a feature) is an inherent bundle including code, tests and documentation.
:fa-github: github.com
:fa-gitlab: gitlab.com
(Third party) services can be integrated to GitHub or GitLab to provide continuous/automated testing (aka Continuous Integration).
- Build the software from sources,
- Run the test suite,
- Report results to the development team,
- Integrate changes to the main repository/branch.
- New branch
- New Pull/Merge Request (PR/MR)
- Merged feature branch
We recommend to use the Simple Git branching model
- The
master
branch is the current stable version - The
master
branch can be safely deployed in production - All other branches are feature branches
- Releases are performed by tagging the master branch
For more information about Continuous Delivery (CD), see: https://tailordev.fr/blog/2017/04/05/why-we-always-start-with-ci-cd/
Ready?
- Create a GitLab account
- Login to your account
- Add a
ssh
key to your account - Fork the tailordev-commons/jdev-2017-ci4py project
Clone the project
$ cd my/projects/directory
# substitute your-account with… your GitLab login
$ git clone [email protected]:your-account/jdev-2017-ci4py.git
Create a virtualenv
for your CI project:
$ cd jdev-2017-ci4py
$ python3 -m venv venv
Activate the newly created virtualenv
:
$ source venv/bin/activate
# Now your prompt should be modified:
(venv) $
# Install development dependencies
(venv) $ pip install -r requirements-dev.txt
# Install the app
(venv) $ python setup.py develop
Now, you should be able to work on the project 🎉
├── AUTHORS
├── LICENSE
├── MANIFEST.in
├── README.md
├── bin
│ └── factrump
├── climate
│ ├── __init__.py
│ ├── data
│ │ ├── temperature_by_country.csv
│ │ └── temperature_by_country_light.csv
│ ├── exceptions.py
│ ├── temperature.py
│ └── utils.py
├── examples
│ └── madagascar.png
├── requirements-dev.txt
├── requirements.txt
├── setup.cfg
├── setup.py
└── tests
├── __init__.py
└── test_temperature.py
# setup.cfg
[tool:pytest]
addopts = -vs --cov-report term-missing --cov=climate
testpaths = tests
[flake8]
exclude =
examples,
venv
(venv) $ pytest
============================== test session starts ==============================
platform darwin -- Python 3.6.0, pytest-3.1.2, py-1.4.34, pluggy-0.4.0 -- /Users/maupetit/projects/TailorDev/Workshops/2017/JDEV/ci4py/venv/bin/python3
cachedir: .cache
rootdir: /Users/maupetit/projects/TailorDev/Workshops/2017/JDEV/ci4py, inifile: setup.cfg
plugins: cov-2.5.1
collected 1 items
tests/test_temperature.py::test_temperature_data_loading PASSED
---------- coverage: platform darwin, python 3.6.0-final-0 -----------
Name Stmts Miss Cover Missing
------------------------------------------------------
climate/__init__.py 1 0 100%
climate/exceptions.py 2 0 100%
climate/temperature.py 43 23 47% 45, 53-58, 65-67, 75-104
climate/utils.py 10 7 30% 10-27
------------------------------------------------------
TOTAL 56 30 46%
=========================== 1 passed in 1.77 seconds ============================
# First things first: create a new branch to work on
(venv) $ git checkout -b add-gitlab-ci
(venv) $ touch .gitlab-ci.yml
Steps:
- install dependencies
- lint your code with
flake8
- run the test suite
You should read the documentation first: https://docs.gitlab.com/ce/ci/quick_start/README.html
# Commit your changes
(venv) $ git add .gitlab-ci.yml
(venv) $ git commit -m 'Add draft gitlab-ci integration'
# Push your branch to YOUR repo
(venv) $ git push origin add-gitlab-ci
Now it's time to open your first Merge Request! 🎉
Go to gitlab.com/your-account/jdev-2017-ci4py
Go to Settings
> General
> Merge Request
:fa-check: Only allow merge requests to be merged if the pipeline succeeds
image: tailordev/pandas
# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
paths:
- ~/.cache/pip/
# Install the project and dependencies
before_script:
# Print out python version for debugging
- python -V
- pip install -r requirements-dev.txt
- python setup.py install
stages:
- lint
- test
flake8:
stage: lint
script:
flake8
test climate:
stage: test
script:
pytest
Increase test coverage to ~100% 💪
https://docs.pytest.org/en/latest/