Python is one of the most versatile languages in the programming world - it's not only one of the fist languages new programmers get introduced to either through education or hobby projects, but it's also a tried and trusted go-to for experienced programmers in engineering, science, mathematics and commercial software development. Created way back in 1991, Python is one of the most enduring langauages in the programming community and continues to receive regular updates to keep it as relevant today as it was back then.
Needless to say, most of the Travis CI team are also pretty fond of it - let's get stuck into the Python Cookbook!
Okay, let's do this! Open up your favorite text editor or IDE. A classic
.travis.yml file for Python would look like this:
dist: xenial language: python cache: pip python: - "3.6" - "3.7" - "3.8" - "nightly" matrix: allow_failures: - python: "nightly" install: - pip install pipenv --upgrade-strategy=only-if-needed - pipenv install --dev script: - bash scripts/test.sh after_script: - bash <(curl -s https://codecov.io/bash)
Next were going to explain what
dist is, and why you need it!
dist in the .yml file is where the Ubuntu Release codename is specified. Please see: releases for a full list. This specifies the base operating system used for the rest of the workflow.
Now we are gonna talk about caching, we all like speed right? It starts out, something like this:
Cache allows for a Python package versions to be stored between runtime, to speed up sequential builds. Cache can apply to more than just Python packages.
python: - "3.6" - "3.7" - "3.8" - "nightly"
Python, given the above language specification, is a key for a sequence of Python versions to perform builds against. Generally most CI tools use the latest bug release version for each minor version. The build logs will tell you the specific versions, you can also view these logs manually via running in debug mode.
matrix: allow_failures: - python: "nightly"
Matrix allows for modifications in the above build sequence. In this case, the
allow_failures key specifies a reference to the Python sequence above, and has the value of "nightly", meaning that that version is possibly allowed to fail. Depending on what more build instructions you have.
At some point, you'll have a requirements.txt file, in here this is where you'll have your dependencies:
pip install -r requirements.txt
To continue with the Python build, you're going to need to run some updates in the
.travis.yml file, via:
before_install: - sudo apt-get update - sudo apt-get install python3-pip - sudo apt-get install python3-pytest
Now add an existing project to GitHub, let's invoke the classic GitHub build flow (init a repo, push files). Head on over to Travis CI, login, sync up and start the build for your Python project! If you forgot the build flow luckily we've attached it below! Open a terminal, run these commands and you can then push your Python project to GitHub via:
git init git add . git commit -m "Travis build" git remote add origin remote repository URL git remote -v git push -u origin master
There you go, your Python project is now connected to Travis, and your CI/CD is ready to go!
We have new recipes every other week, make sure you come back for a practical way of using Travis for beginners.
Author: Montana Mendy
Originally posted on the Travis CI blog