Continuous Delivery with Github, Codeship and Heroku

I am very excited about Continuous Delivery or CD. My excitement started a couple of years back when one of the companies I collaborated with started its journey to move to CD. Then, my excitement Skyrocketed when I had the opportunity to collaborate with a company that was already in that path, who made it! :D

I also recently had the chance to become a Rails Girls coach and I thought it would be a good idea to have some fun improving the CD process of Rails Girls tutorial app by adding a staging / test environment and adding it to a CI / CD pipeline.

The tools I used for this project are:

  1. Github
  2. Codeship
  3. Heroku

These are the steps I followed and some references that helped me get things done, I hope this is useful for you as well and that you are as excited as I am about CD.

Heroku Integration

Creating the Production App

  1. Follow the Preparing your app steps from this tutorial.
  2. In your terminal, cd to your app on your local machine
  3. run ruby -v to verify your ruby version and copy this info
  4. Open the Gemfile from your app and add the ruby version to the end of the file (i.e ruby "2.3.1")
  5. Commit everything to your Github repository.
  6. In your local terminal run run heroku create
  7. Confirm that the heroku app has been created with git config --list | grep heroku
  8. Rename the app to the name you want to give to your app with heroku apps:rename newname where newname.
  9. Verify the git remote is correct with git remote -v. You should see the new name of your app, not the automatically assigned name. If you don’t, reassign the remote manually with git remote rm heroku && heroku git:remote -a newname
  10. Push to heroku with git push heroku master
  11. Once the push is complete, run migrations with heroku run rake db:migrate
  12. Verify that the app deployed correctly by logging into Heroku, navigating to your app, and clicking the view app button.

Creating the Testing / Staging environment

We will create the Testing / Staging environment of our application by doing a fork of the production app. You could do this the other way around: Fork the production app from the staging app.

  1. Run heroku fork --from appname --to appname-staging

    • appname is the name of the original app and appname-staging is the name you want to give the fork version of the app (e.g. staging, testing).
  2. Give the new fork a git remote

  3. run heroku info -a appname-staging to find the Git url and copy it.

  4. Then run git remote add staging <the url you copied> where staging is the name you want to give the git remote.

  5. You’ll use this staging name in commands like git push staging master or if you follow the next steps to deploy to staging from a staging branch git push staging staging:master.

  6. Check the remotes are all there with git remote -v there should be three remotes for your local repo:

    • One called heroku (that’s the first heroku app you created)
    • one called staging for your staging app (or whatever you named your remote in the step above)
    • One for your github repo’s origin.
  7. Migrate your staging apps DB with heroku run rake db:migrate --remote staging

Codeship Integration

We will use Codeship as our Continuous Integration platform.

  1. To integrate Codeship with Github and Heroku, follow the steps here.
  2. Now, lets manage the deployment to the two apps that we created before (production & staging). We will create two deployment pipelines, one for production and another for staging.
  3. In Codeship, go to your project settings and click on the Deploy tab.
  4. Click on the + Add new deployment pipeline tab.
  5. Select Branch is exactly and enter the branch name of your production branch (e.g. master)
  6. Click on Save pipeline settings.
  7. You will be at the Add a deployment to your pipeline page, there, click on Heroku.
  8. Enter the Application name, Heroku Key, Application URL, which you should already have from the previous steps Heroku section steps.
  9. Optional: Enter a post-deployment command, e.g. rails db:migrate
  10. Optional: Enable Backup Database
  11. Optional: Enable Check app URL
  12. Click on Save Deployment changes.
  13. Follow steps 4 to 13 again but for your test / staging branch.
  14. Finally, you can add the status of the build in your Readme file. Example

What does this mean?

This means that you now the Development Flow is as follows:

  1. You have your feature branch and decide that it is ready to go live!
  2. Open a PR to the staging / test branch. Example
  3. The build and your all your checks (e.g. code quality, code coverage, etc.) pass.
  4. Merge the feature branch to staging. Example. With this step, your our new code is deployed to staging automagically.
  5. Perform your testing in staging (ideally, automated tests).
  6. The tests pass and the feature is ready to go to production.
  7. Open a PR to the master / production branch. Example.
  8. The build and your all your checks (e.g. code quality, code coverage, etc.) pass.
  9. Merge the feature branch to production. Example. With this step, your our new code is deployed to production automagically.
  10. Perform your testing in production (ideally, automated tests).
  11. Pum! Your feature is live!
  12. As a clean-up step, your can now delete your feature branch.

Resources

  1. The repository where this version of the app is running.
  2. Create Staging and Production apps on Heroku with corresponding git branches
  3. Put Your App Online With Heroku
  4. Continuous Deployment with the Codeship
  5. Learn more about Continuous Delivery

Thank you for reading! May the force be with you!