Setting Up Review Apps On Heroku

Gabriel Martin
Echobind
Published in
4 min readFeb 11, 2020
Photo by Christopher Gower on Unsplash

Heroku is an awesome platform for hosting applications. It’s easy to use and offers a ton of integrations right out of the gate. This post is going to walk through setting up review apps that automatically build and deploy whenever a new pull request is opened on GitHub.

Why Review Apps?

Review apps are a great way to automate deployments for internal QA of new features before they are pushed to a staging environment. For example, with review apps you could:

  • Spin up new instances of your app automatically whenever a pull request is open.
  • Do internal QA with the link created for the app.
  • When the connected PR is merged, the master branch will automatically deploy to staging where the client can QA on their end.

This is the exact process we will set up. Let’s get to it.

Pre-reqs

These steps assume that you already have a GitHub repo set up and a pipeline configured for your app on Heroku. I’ll be using a basic Rails application but the process will be similar for different frameworks/languages.

Configure the Pipeline

Open up the pipeline on the Heroku dashboard and click Settings.

In the Connect to GitHub section, search for the repo name and click Connect.

Just below that, in the Review Apps section, click on Enable and configure review apps for the pipeline.

Sweet, now we can add the file that will allow Heroku to build review apps when we open a new PR on GitHub.

Create a new git branch.

git checkout -b review-apps-setup

Create the file app.json in the root of your project.

$ touch app.json

Heroku uses this file to automate the process of creating a new app.

From the Heroku docs:

app.json is a manifest format for describing web apps. It declares environment variables, add-ons, and other information required to run an app on Heroku.

This is what my app.json file looks like. It applies to Rails apps hosted on Heroku.

{
"addons": [
{
"plan": "heroku-postgresql:hobby-dev"
}
],
"buildpacks": [
{
"url": "heroku/ruby"
}
],
"scripts": {
"postdeploy": "bundle exec rails db:seed"
}
}

What are these properties for?

addons: An array of strings or objects that define the resources your app needs to run. In this case, whenever a review is created, this field will tell Heroku to provision the heroku-postgresql add-on with the hobby-dev plan selected.

buildpacks: An array of objects specifying the buildpacks needed to build the app. Rails apps can use the heroku/ruby buildpack, Node apps, heroku/nodejs etc.

scripts: A key-value object specifying scripts or shell commands to execute at different stages in the build/release process. We are setting the postdeploy script to seed the database after the app has been created. This command only runs once.

Awesome. Let’s commit this file and push the changes to GitHub.

git add . && git commit -m 'create app manifest for Heroku'

Once the changes are up, go ahead and create a new pull request. If you go back to the Heroku dashboard, you should see a new review app in the build process.

Auto-deploy to staging

Right on. Now that review apps are set up, we should configure automatic deploys to staging when a pull request is merged into the master branch.

On your staging app, click the vertical arrows on the bottom-right of the card and select Configure automatic deploys…

Select the branch you want to deploy from and click Enable Automatic Deploys.

Merge that PR we just opened and the staging app should build and deploy!

If you need to add environment variables for all review, that’s super easy. On the pipeline dashboard, just click the Settings tab and scroll down to the Review app config vars section and click Reveal Config Vars, just like a normal app.

Wrap Up

That’s all there is for this post. Review apps are a great tool to automate the deployment process of apps for testing and QA. Be sure to check out the docs for more info on setting up review apps.

Contributor’s Bio

Gabriel is a software and summertime enthusiast. He is an Associate Engineer at Echobind.

--

--