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:
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
- Follow the Preparing your app steps from this tutorial.
- In your terminal, cd to your app on your local machine
run ruby -v
to verify your ruby version and copy this info- Open the
Gemfile
from your app and add the ruby version to the end of the file (i.eruby "2.3.1"
) - Commit everything to your Github repository.
- In your local terminal run
run heroku create
- Confirm that the heroku app has been created with
git config --list | grep heroku
- Rename the app to the name you want to give to your app with
heroku apps:rename newname where newname
. - 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 withgit remote rm heroku && heroku git:remote -a newname
- Push to heroku with
git push heroku master
- Once the push is complete, run migrations with
heroku run rake db:migrate
- 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.
-
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).
-
Give the new fork a git remote
-
run
heroku info -a appname-staging
to find the Git url and copy it. -
Then run
git remote add staging <the url you copied>
where staging is the name you want to give the git remote. -
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 branchgit push staging staging:master
. -
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
.
- One called
-
Migrate your staging apps DB with
heroku run rake db:migrate --remote staging
Codeship Integration
We will use Codeship as our Continuous Integration platform.
- To integrate Codeship with Github and Heroku, follow the steps here.
- 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.
- In Codeship, go to your project settings and click on the Deploy tab.
- Click on the + Add new deployment pipeline tab.
- Select Branch is exactly and enter the branch name of your production branch (e.g.
master
) - Click on Save pipeline settings.
- You will be at the Add a deployment to your pipeline page, there, click on Heroku.
- Enter the Application name, Heroku Key, Application URL, which you should already have from the previous steps Heroku section steps.
- Optional: Enter a post-deployment command, e.g.
rails db:migrate
- Optional: Enable Backup Database
- Optional: Enable Check app URL
- Click on Save Deployment changes.
- Follow steps 4 to 13 again but for your test / staging branch.
- 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:
- You have your feature branch and decide that it is ready to go live!
- Open a PR to the staging / test branch. Example
- The build and your all your checks (e.g. code quality, code coverage, etc.) pass.
- Merge the feature branch to staging. Example. With this step, your our new code is deployed to staging automagically.
- Perform your testing in staging (ideally, automated tests).
- The tests pass and the feature is ready to go to production.
- Open a PR to the master / production branch. Example.
- The build and your all your checks (e.g. code quality, code coverage, etc.) pass.
- Merge the feature branch to production. Example. With this step, your our new code is deployed to production automagically.
- Perform your testing in production (ideally, automated tests).
- Pum! Your feature is live!
- As a clean-up step, your can now delete your feature branch.
Resources
- The repository where this version of the app is running.
- Create Staging and Production apps on Heroku with corresponding git branches
- Put Your App Online With Heroku
- Continuous Deployment with the Codeship
- Learn more about Continuous Delivery
Thank you for reading! May the force be with you!