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

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

June 23rd 2022 3,381 reads
Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
image
Mohamed Labouardy HackerNoon profile picture

Mohamed Labouardy

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

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:
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
react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!