Heroku Pipelines: Python Flask App with pipelines

Introduction

This article shows how to run a Python Flask app, deploy and manage the app using Pipelines and Github Integration

../../_images/heroku-pipelines.png

Figure 1 : Heroku Pipeline Integration with Github and developer deployment flow

Figure 1 show how the commit travels from a developers desk top to Github and Heroku Pipeline.

Example Use Case

In this tutorial we look at how a simple Flask app is deplyed in development and staging and associated with Heroku Pipeline. We will also look at how to use command line to promote an app from development to staging

../../_images/heroku-pipeline-2.png

Clone the Source Code

$ git clone https://github.com/rajdeepd/flask-helloworld

Install Virtual Environment

Go to the root directory of the project cloned in the previous step and create a virtual environment env.

$ cd flask-helloworld
$ virtualenv venv
$ source venv/bin/activate

Install Dependencies

  1. Install the dependencies using pip install command

    $ pip install flask gunicorn
    
  2. Run the app using the following command

    $ python app.py
    

    Your app should now be running on localhost:5000

You will see the default HelloWorld output as shown below.

Deploy the App to Heroku

Run the following commands to create a git remote and push the app.

    $ heroku create flask-helloworld-dev --remote dev --region us
$ git push dev master

Output will be similar to the following output

Creating flask-helloworld-dev... done, region is us
https://flask-helloworld-dev.herokuapp.com/ | https://git.heroku.com/flask-helloworld-dev.git
(venv)ubuntu@ubuntu-vm-sf:~/work/temp2/flask-helloworld$ git push dev master
Counting objects: 1227, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1064/1064), done.
Writing objects: 100% (1227/1227), 5.54 MiB | 378.00 KiB/s, done.
Total 1227 (delta 85), reused 1227 (delta 85)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-2.7.11
...
remote:        Successfully installed Flask-0.10.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.5 gunicorn-19.4.5 itsdangerous-0.24 wheel-0.24.0
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 39.6M
remote: -----> Launching...
remote:        Released v3
remote:        https://flask-helloworld-dev.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/flask-helloworld-dev.git
 * [new branch]      master -> master

Create a Staging app and Deply to Heroku

heroku fork -a flask-helloworld-dev flask-helloworld-staging --region us

Output will be

Forking flask-helloworld-dev... done. Forked to flask-helloworld-staging
     Setting buildpacks... done
     Copying config vars:
       ... done
     Deploying c66e20c to flask-helloworld-staging... done
     Fork complete. View it at https://flask-helloworld-staging.herokuapp.com/

Add a git remote

git remote add staging git@heroku.com:flask-helloworld-staging.git

Install Heroku Pipelines

$ heroku plugins:install heroku-pipelines

Configure Pipeline for the App

  1. Run the following command from the project directory to create a pipeline and deploy the app to staging

    $ heroku pipelines:create --remote dev --stage development
    

    You will get the following error if the app is not created or you are not in the appropriate directory

         ▸    Error: No app specified
         ▸    Usage: heroku pipelines:create [NAME] --app APP
         ▸    We don't know which app to run this on.
         ▸    Run this command from inside an app folder or specify which app to use with --app APP
         ▸
         ▸    https://devcenter.heroku.com/articles/using-the-cli#app-commands
    
    
    Check in the dashboard to make sure the app is in development of the pipeline
    

Adding Staging app to the pipeline

$ heroku pipelines:create --remote dev --stage development
../../_images/heroku-pipeline-development-staging.png

Promoting the App from Staging the Production

You can run the following command to promote the app from staging to production

$ heroku pipelines:promote -r dev

Output will be similar to the listing below

Fetching app info... done
Fetching apps from flask-helloworld-pipeline... done
Starting promotion to staging... done
Waiting for promotion to complete... done

Promotion successful
flask-helloworld-staging: succeeded

Audit the Releases

$ heroku releases --remote staging

Output

=== flask-helloworld-staging Releases
v4  Deploy c66e20c                        dua_rajdeep@yahoo.com  2016/04/07 17:06:47 +0530 (~ 11m ago)
v3  Forked from flask-helloworld-staging  dua_rajdeep@yahoo.com  2016/04/07 16:49:50 +0530 (~ 28m ago)
v2  Enable Logplex                        dua_rajdeep@yahoo.com  2016/04/07 16:49:41 +0530 (~ 28m ago)
v1  Initial release                       dua_rajdeep@yahoo.com  2016/04/07 16:49:39 +0530 (~ 28m ago)

Summary

In this tutorial we learnt how to deploy a Heroku app and take it through Heroku Pipeline from Development to Staging