Flask is a great Python framework which can be used to create a web application. In my current project I wanted to set up a continous deployment to an Heroku app. My sources are versionned in a Git repostory hosted on Bitbucket.

Prerequisites

Before starting this tutorial, you will need to :

In this article, I will use 2 Git branches :

Create Procfile

Procfile is a file that specificies to Heroku what commands to launch at the start of your application.

To run your Flask application, we will use the Python package gunicorn which is a small web server.

Here is the content of the Procfile :

web: gunicorn app:app

This file will be placed in the root directory of your project.

Set up Bitbucket Pipeline

Bitbucket Pipelines is a feature of the Bitbucket platform that allow to integrate a continuous integration and deployment in your development process.

So when a new code will be pushed on the master branch, Bitbucket will create a new pipeline to get your entire code and deploy it on your Heroku app.

Configuration of bitbucket-pipelines.yml

bitbucket-pipelines.yml is a file that will be again in the root directory of your project. It will configure the pipeline and describe the steps to deploy the code to Heroku.

Here is the content (It’s YAML so be careful with indentation) :

image: python:3.7.0 # Specifies Python version
clone:
  depth: full # Tell to clone the repository all deepth
pipelines:
  branches:
    master: # Only for master branch
      - step:
          name: Deploy to Heroku # Step name
          deployment: production # Target environment
          script:
            - git push https://heroku:[email protected]/$HEROKU_APP_NAME.git HEAD # Push & Deploy to Heroku app

Configure Bitbucket Pipeline variables

In the yml file, we used 2 pipeline variables : HEROKU_API_KEY & HEROKU_APP_NAME.

This avoids to store in the Git repo some production environment credentials. But we still need to configure them.

Save Python dependencies

To be able to execute your application, Heroku will need to install your dependencies in its app. In order to achieve this, you have to save the list of them in a file. Typically, this is the command to launch in the root directory of your project :

pip freeze > requirements.txt

requirements.txt contains now all your dependencies.

Final step

Now your final step is to commit and push into master branch the newly created files and let Bitbucket Pipeline take the rest of the deployment process part. Now, everytime you push some code on master branch, a new version is deployed in the Heroku app.