Photo by Daniel Cheung on Unsplash

Deploy API applications to Heroku.

This article is about the deployment of the API flask application to Heroku server and having a continuous integration with Github.

We will be continuing the above article for deploying our API application. In brief, the above article follows a step by step process for creating our API application using Flask. The application can generate a random name and email on every request that hit the API.

Part 2. Deploy the Application

To begin we have to commit and push our code to a Github repository. If you had followed the previous article you will be currently having a folder called flask_api and a file named server.py in it.

1. Getting the source code for deployment.

Don’t worry if you are new and haven’t followed the steps from the previous article. You can download the files from the following URL. If you had followed the steps in the previous article you can skip this step.

https://github.com/Joel-hanson/flask_api.git

You can download the source code by clicking the clone or download button shows on the top right side of the file listing.

After downloading the source code from Github extract the files. After extraction opens your terminal at the downloaded folder and checkout to the part-1 branch by running the following command.

git checkout part-1

2. Making the required file for Heroku deployment.

requirements.txt

For running the application on Heroku we need to specify the python packages required for our files to run in a file called requirements.txt.

The requirements.txt will have 3 packages which we use to make this application run.

flask
faker
gunicorn

Flask is used as a microframework for Python to run our application, Whereas Faker is used for making random names and emails. Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX, it’s used for running our application on Heroku.

You can get all the packages that are installed in your machine using pip by running the command pip freeze > requirements.txt

Procfile

Procfile file is used to tell Heroku which application and which file to run. This file does not need any extension, copy paste the following code to this file.

web: gunicorn server:app

web is used by Heroku to start a web server for the application. The server:appspecifies the module and application name. In our application, we have the app module and our flask application is also called app. If your’s are different you can change them.

So Procfile and requirements.txt are the two files which are needed for our application to run in our Heroku server.

runtime.txt

This file specifies which python version should be used to run our application. The supported runtime versions can be found here.

python-3.6.8

3. Heroku setup

  • Create a Heroku account by signing up:
  • Install the Heroku CLI:
  • Login to Heroku account from CLI:
heroku login

You will be prompted to press any key to open up the browser to log in and then click the login button.

4. Deployment

create a Heroku app by running the following command with your app name. I named my app flask — api.

$ heroku create flask--api

If you go to https://dashboard.heroku.com/apps you will find that your app will be created there. The next will be pushing our code to Heroku. You should commit your changes in which branch you are working on and merge that branch with master branch so that we can deploy to Heroku. After every change had reached the master branch we can run the following command.

git push heroku master
git push heroku master

The deployed URL will be shown at the bottom.

https://flask--api.herokuapp.com/

If anything goes wrong with the code and if it’s not working the logs can be accessed by running

heroku logs --tail

Thus our Heroku server is up and running.

5. Continuous Integration (CI)

What is Continuous Integration?

- Continuous Integration (CI) is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests.

For our, CI go to https://dashboard.heroku.com and click on the app so you can find an overview of our app. For continuous integration go to the Deploy tab in the dashboard.

Dashboard

In the Deploy tab, you can find 3 integration for deployment

  1. Heroku Git
  2. Github
  3. Container Registry
Deployment methods

The steps we followed initially was the Heroku Git the other 2 are also pretty as Heroku Git.

GitHub CI

The GitHub CI can be done by connecting our app with the GitHub account. After connecting the Github with Heroku you will be asked to enter the repository that is needed to connect with our Heroku app. Thus our GitHub CI is over.

Container Registry CI

The Container Registry is done by following the steps:

Container Registry

Thus by following the above steps, we can do the continuous integration for Heroku app with our source code.

Next Steps

We have done the deployment part for our application. The continuation parts of this post will be showing you how to test this application using Heroku pipeline.

Next things we are going to do:

  1. Create a pipeline.
  2. Run tests for each continuous integration.

Please provide your feedback and suggestions. Follow me to get the latest updates.

Linkedin: linkedin.com/in/joel-hanson/

Portfolio: joel-hanson.github.io/

Github: github.com/Joel-hanson

Twitter: twitter.com/Joelhanson25

Works at @impressAI