This article is about the deployment of the API flask application to Heroku server and having a continuous integration with Github.
How to make a simple API that generates a random name and email using a Flask application.
This post is for the beginner who is looking to create an API using the flask. We will be creating an API that can…
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.
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.
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 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 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.
This file specifies which python version should be used to run our application. The supported runtime versions can be found here.
3. Heroku setup
- Create a Heroku account by signing up:
Cloud Application Platform | Heroku
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in…
- Install the Heroku CLI:
The Heroku CLI | Heroku Dev Center
How to download, install, and start using, the Heroku CLI. The Heroku CLI used to be part of the Heroku Toolbelt.
- Login to Heroku account from CLI:
You will be prompted to press any key to open up the browser to log in and then click the login button.
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
The deployed URL will be shown at the bottom.
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.
In the Deploy tab, you can find 3 integration for deployment
- Heroku Git
- Container Registry
The steps we followed initially was the Heroku Git the other 2 are also pretty as Heroku Git.
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:
Thus by following the above steps, we can do the continuous integration for Heroku app with our source code.
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:
- Create a pipeline.
- Run tests for each continuous integration.
Please provide your feedback and suggestions. Follow me to get the latest updates.