CI/CD pipeline using Github, Docker, CircleCI & Heroku by@mlabouardy
3,399 reads

CI/CD pipeline using Github, Docker, CircleCI & Heroku

Read on Terminal Reader

Too Long; Didn't Read


Company Mentioned

Mention Thumbnail
featured image - CI/CD pipeline using Github, Docker, CircleCI & Heroku
Mohamed Labouardy HackerNoon profile picture

@mlabouardy

Mohamed Labouardy
react to story with heart

This post will walk you through how to setup a Continuous Integration & Continuous Deployment (CI/CD) pipeline with CircleCI, Docker & Heroku easily. In the end of this tutorial, you should be able to setup your own CI/CD as shown in the diagram above.

This tutorial assumes that you have:

  • A Heroku Account — sign up for free.
  • A Heroku CLI installed.
  • A CircleCI Account — sign up for free with your Github account.
  • Python version 3.5 installed locally.

The source code of the application used in this demo is available on my Github.

image

1 — Heroku

First, login in using the email address & password you used when creating your Heroku account:

heroku login

To clone the sample application so that you have a local version of the code that you can then deploy to Heroku, execute the following commands in your local command shell or terminal

git clone https://github.com/mlabouardy/circleci-heroku-flask.gitcd circleci-heroku-flask/

Note: in case you are using your own app, you should add the following files to your code repository:

  1. Procfile: It tells Heroku what commands should be run
  2. requirements.txt: In this file you will list the packages/dependencies that pip should install for you

Create an app on Heroku, which prepares Heroku to receive your source code:

heroku create movies-store-demo

Provision a MySQL database add-on:

heroku addons:add cleardb:ignite — app movies-store-demo

Heroku will automatically add a config var with the database credentials in the form of a URL. You find the config vars under the Settings tab, and click the button to “Reveal config vars

image

Now deploy the application:

git remote add heroku https://git.heroku.com/movies-store-demo.git

image

Go to Heroku Dashboard, click on “Open App” button:

image

You should see:

image

Note: As a handy shortcut, you can open the application as follows:

heroku open

2 — CircleCI

The following sections walk through how CI/CD steps are configured for this application, how to run unit tests, build & push the Docker Image to DockerHub, and how to deploy the demo application to Heroku:

image

The .circleci/config.yml contains CI/CD steps:

  • We use Python 3.5 as the primary container & MySQL for the build environment.
  • To speed up the builds, we places the Python virtualenv into the CircleCi cache and restores cache before running pip install.
  • Unit Tests requires MySQL database therefore we need to wait for the container to be ready.
  • We install the Docker Client, build the docker image from the Dockerfile stored in the Github repository, and then Push the image to DockerHub.
  • Finally, we install Heroku CLI & push the changes to Heroku.

As shown in the configuration file above, we will need to set some environment variables, so navigate to the Project settings:

image

Finally, to enable the connection to the Heroku Git Server from CircleCI we need to create an SSH Key without passphrase. Issue the following command:

ssh-keygen -t rsa

Then, add the private key ithe CircleCI UI SSH Permissions page with a hostname of git.heroku.com as follows:

image

The public key is added to Heroku on the Account page:

image

Now every time you push changes to your Github repo, CircleCI will automatically deploy the changes to Heroku. Here’s a passing build:

image

The CI/CD pipeline steps as described in config.yml file:

image

The Docker Image repository on DockerHub:

image

Heroku last build from CircleCI:

image

For more articles check my blog

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa