CI/CD with CircleCI - Heroku deploy
In this post, we'll deploy a Flask app to Heroku. Any commit to Github, the CircleCI will be triggered and test will be performed. If the test finished successfully, the CircleCI will deploy our app to Heroku.
First, sign up for CircleCI.
We can login to CircleCI platform via Github by allowing access to the repo.
At the click on "Authorize application", we'll have welcome screen with a list of our Github repositories:
We'll use the following source in GitHub: circleci-heroku.
Here are the steps to install and run the test:
- Clone the repo and cd circleci-heroku.
- Setup virtualenv : virtualenv venv and then source venv/bin/activate.
- Run pip install -r requirements.txt (preferably inside a virtualenv) to install the dependencies.
- To run the "hello" app locally:
- Run nosetests to run the unit test.
(venv) k@laptop:~/TEST/circleci-heroku$ python hello/hello_app.py * Running on http://127.0.0.1:5000/
(venv) k@laptop:~/TEST/circleci-heroku$ nosetests . ---------------------------------------------------------------------- Ran 1 test in 1.206s OK
Basic CircleCI configuration is available from here.
But in this tutorial, we'll use Python Flask, so the reference is available from Continuous Integration and Continuous Deployment with Python.
CircleCI works well for Python projects. CircleCI runs automatic inference on each build to determine dependencies and test commands. If CircleCI doesn't infer all of our settings, we can also add custom configuration to a circle.yml file checked into our repo's root directory, which is our case.
When CircleCI detects Python, CircleCI automatically uses virtualenv to create an isolated Python environment. It also automatically installs our dependencies using either pip when it finds a requirements.txt, or distutils when we find a setup.py file.
CircleCI has pre-installed more than a dozen databases and queues, including PostgreSQL and MySQL. If needed, we can manually set up our test database from our circle.yml.
If Heroku is not installed, let's do it:
$ sudo apt-get install software-properties-common $ sudo add-apt-repository "deb https://cli-assets.heroku.com/branches/stable/apt ./" $ curl -L https://cli-assets.heroku.com/apt/release.key | sudo apt-key add - $ sudo apt-get update $ sudo apt-get install heroku
To verify our CLI installation use the heroku --version command:
$ heroku --version heroku-cli/5.6.28-2643c0a (linux-amd64) go1.7.5
Get Heroku API key:
$ heroku auth:token Enter your Heroku credentials. Email: email@example.com Password (typing will be hidden): Logged in as firstname.lastname@example.org ea408fd3-7681-6g47-19c4-61282704700g
And SSH key:
$ heroku keys:add Found an SSH public key at /home/k/.ssh/id_rsa.pub ? Would you like to upload it to Heroku? Yes Uploading /home/k/.ssh/id_rsa.pub SSH key... done
We must supply or confirm some authentication settings before Heroku deployment. CircleCI divides this setup work into three steps.
- API key
- SSH key
- App name
We need to input the API key into the account settings on CircleCI:
Also, we may want to set the registered person as the Heroku deploy user and to associate a Heroku SSH key with the deploy user's account.
One more thing: we need to create a new project in Heroku. The project name should be in circle.yml:
Click "Add Projects" on the Welcome screen:
CircleCI automatically runs test commands when certain files are detected:
- tox when tox.ini is found
- nosetest when unittest.py is found
- manage.py test when manage.py contains a testing section
- setup.py test when setup.py contains a testing section
We can get the url for the deployed app:
Here is the app that's been deployed after a successful testing:
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization