robotsnoindex
robotsnoindex

This guide shows you how to use Bitbucket Pipelines for building and testing a Python software project in a Docker container.

If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo python repo.
Instructions to import the demo repo

For this demo, all you need to do is import this repo and enable pipelines to get your first pipeline to run!

  1. Import this repository by visiting bitbucket.org and log in. Then:

    Import gif

  2. On the left navigation bar, click Pipelines and scroll down to view the bitbucket-pipelines.yml file. Click Enable

    Enable gif

  3. Watch your build run!

    Build gif

Note: Bitbucket Pipelines includes fifty free minutes per account, at the time of writing. You can check your team or account's minutes usage for the month by clicking your Avatar > Bitbucket settings > Plan details.




If you'd like to set it up by hand, most of the configuration happens in the bitbucket-pipelines.yml file that Pipelines uses to define the build.

Specify your Python version with Docker

Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file. You can easily use Python with Bitbucket Pipelines by using one of the official Python Docker images on Docker Hub.  If you use the default Python image it will come with pip installed by default to help you manage your dependencies.

For instance, you can use Python 3.7.2 by specifying it at the beginning of your bitbucket-pipelines.yml file.

image: python:3.7.2
pipelines:
  default:
    - step:
        script:
          - python --version

If you wanted to use a different version of Python you simply need to change the tag of the Python Docker image. The example below would start a container with Python 3.5.1

image: python:3.5.1

You can find a list of all supported Python versions and corresponding image tags, refer to https://hub.docker.com/r/library/python/.

Note that the Django Docker images have been deprecated in favor of the standard Python images mentioned above.

You can check your bitbucket-pipelines.yml file with our online validator.

Install dependencies

If you are using a requirements.txt file, you can simply run pip at the beginning of your script to install all the dependencies.

image: python:3.7.2
pipelines:
  default:
    - step:
        script:
          - pip install -r requirements.txt

You can also install dependencies with the pip install command.

image: python:3.7.2
pipelines:
  default:
    - step:
        script:
          - pip install django

Databases

Bitbucket Pipelines allows you to launch extra services during the execution of your pipeline by defining the service, and instantiating it on the appropriate step.

We've compiled a list of of bitbucket-pipeline.yml examples to help get started with your favourite database.


Testing

You simply need to add to your bitbucket-pipelines.yml file the same commands that you would run locally to test your application. Following are some examples for specific Python tools.

PyUnit

Running PyUnit tests is quite straight forward.

image: python:3.7.2
pipelines:
  default:
    - step:
        script:
          - python -m unittest discover tests/

Django

To test your Django projects, you can run it the same as you run Django tests locally. Just remember to make sure that Django is installed in your Pipelines environment too.

image: python:3.7.2
pipelines:
  default:
    - step:
        script:
          - pip install django
          - python manage.py test


Remember, you can check your bitbucket-pipelines.yml file with our online validator.