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:
The source code of the application used in this demo is available on my Github.
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:
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”
Now deploy the application:
git remote add heroku https://git.heroku.com/movies-store-demo.git
Go to Heroku Dashboard, click on “Open App” button:
You should see:
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:
The .circleci/config.yml contains CI/CD steps:
As shown in the configuration file above, we will need to set some environment variables, so navigate to the Project settings:
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:
The public key is added to Heroku on the Account page:
Now every time you push changes to your Github repo, CircleCI will automatically deploy the changes to Heroku. Here’s a passing build:
The CI/CD pipeline steps as described in config.yml file:
The Docker Image repository on DockerHub:
Heroku last build from CircleCI:
For more articles check my blog ❤