CircleCI — Migrating configuration to version 2.0 for a Ruby on Rails application on Heroku with continuous deployment

Onur Küçükkeçe
Sep 3, 2018 · 3 min read

Recently CircleCI announced that the CircleCI 1.0 configurations and features are no longer supported. If you are using this version, you need to migrate your configuration to version 2.0.

Here, you can find how we updated our CircleCI configuration for our Ruby on Rails application on Heroku with continuous deployment.

In CircleCI 1.0, we used to use below configuration for staging and production pipelines.

machine:
pre:
- sudo curl --output /usr/local/bin/phantomjs https://s3.amazonaws.com/circle-downloads/phantomjs-2.1.1
test:
override:
- bundle exec rake security # This is a custom rake task
- bundle exec rspec -r rspec_junit_formatter --format RspecJunitFormatter -o $CIRCLE_TEST_REPORTS/rspec/junit.xml
deployment:
staging:
branch: develop
commands:
- ./script/deploy/staging
production:
branch: master
commands:
- ./script/deploy/production

In above scripts, for deployment commands, we are using a shell script where all Heroku and other tasks are gathered up in one file. As you could list all commands in this section line by line, you could also use a shell script like below, save it to script/deploy folder and use it in commands section in CircleCI configuration which will make the migration one step easier.

This example is for a staging environment but can easily but adapted for the production as well.

#!/bin/bash
set -x
set -e
export RAILS_ENV='production'
export RACK_ENV='production'
[[ ! -s \"$(git rev-parse --git-dir)/shallow\" ]] || git fetch --unshallow
git push git@heroku.com:shine442-staging.git develop:master
heroku run rake db:migrate --app shine442-staging
heroku run rake db:seed --app shine442-staging
# ...

Now, I would suggest checking the configuration reference page before starting with CircleCI 2.0. It will give you what you need to know about the structure briefly.

version: 2
jobs:
job_name:
# ... definitions
another_job_name:
# ... definitions
workflows:
version: 2
a-flow-item:
jobs:
- job_name
- another_job_name:
# Options for this job and mind the indentation here.
another-flow-item:
jobs:
# ... and so on.

The CircleCI 2.0 configuration file must be placed in .circleci/config.yml file and must contain three top-level keys which are version, jobs, and workflows. Below is an example of a complete configuration file separated three parts.

version: 2

Then, each job definitions for your build, staging and production releases.

jobs:
build:

# You can copy necessary steps for your build from
# https://github.com/CircleCI-Public/circleci-demo-ruby-rails/blob/master/.circleci/config.yml
deploy-staging:
machine:
enabled: true
working_directory: ~/repo
steps:
- checkout # Special step used to check out source code.
- run:
# Again, mind the indentation here.
name: "Deploy to staging"
command: |
./script/deploy/heroku
./script/deploy/staging
deploy-production:
machine:
enabled: true
working_directory: ~/repo
steps:
- checkout
- run:
name: "Deploy to production"
command: |
./script/deploy/heroku
./script/deploy/production

Here we are running an additional shell script called heroku in deploy jobs. That’s because Heroku continuous deployment features are also deprecated with CircleCI 1.0 and you need to login to Heroku before running any Heroku commands. You can find the shell scripts used in heroku file down below but first, as the final step, workflows;

workflows:
version: 2
build-deploy:
jobs:
- build
- deploy-staging:
requires:
- build
filters:
branches:
only: develop
- deploy-production:
requires:
- build
filters:
branches:
only: master

For logging the container into Heroku, you will need to obtain an API key from your account settings page in Heroku and define HEROKU_EMAIL and HEROKU_TOKEN environment variables in build settings of your project in CircleCI. Then you can use below shell script which was referenced to ./script/deploy/heroku in the above example.

#!/bin/bashcat >~/.netrc <<EOF
machine api.heroku.com
login $HEROKU_EMAIL
password $HEROKU_TOKEN
machine git.heroku.com
login $HEROKU_EMAIL
password $HEROKU_TOKEN
EOF
chmod 600 ~/.netrc # Heroku cli complains about permissions without this

Alright, it’s now time to delete your old configuration file circle.yml and add the new .circleci/config.yml file to your repository and push. You can check the workflows section in CircleCI to see the status of your releases.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade