Heroku Review Apps: setup & configuration
Tags: devops, ci, heroku, rails
Published: 2020-10-23 11:20:42

At my job at [VideoRunRun](https://www.videorunrun.com/), we use Github and Heroku.

We have a pipeline with a staging and a prod app, and when I joined the team few months ago, I suggested to enable Review Apps as well.

# What are Review Apps ?

I'll quote the [official documentation](https://devcenter.heroku.com/articles/github-integration-review-apps):

> Review apps run the code in any GitHub pull request in a
> complete, disposable Heroku app. Review apps each have a
> unique URL you can share, making them a great way to
> propose, test, and merge changes to your code base.


Mainly, it allows us to shorten feedback loops, especially with our Product Owner, for UI/UX considerations, for testing purposes in a production environment, for PR reviews, etc.

Overall it's very convenient for many different use cases and we just love it.

**It's a killer feature**.

# Setting it up

In order to use Review Apps up to their potential, they must be ready-to-use as soon as they are deployed.

If you need to manually set up heroku dependencies, define some env vars, or run a script every time you need to use a review, poor child you're lost.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/con29yt5c0quyaomoyqu.jpeg)

As always, automation is key. 

Luckily, **Review Apps are very easy to set up** and documentation is pretty straight-forward.

___

General config happens on Heroku UI:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/humn8lio0qlq1k69prkz.png)

There you can enable Review Apps, define env vars, choose a URL pattern, etc.

___

For advanced config, you'll need to add an `app.json` file to your app's root folder.

Here's a working example:
```
# app.json
{
  "environments": {
    "review": {
      "formation": {
        "worker": {
          "quantity": 1,
          "size": "free"
        }
      },
      "addons": [
        "heroku-postgresql:hobby-dev",
        "heroku-redis:hobby-dev"
      ],
      "scripts": {
        "postdeploy": "rails db:schema:load db:seed"
      }
  }
}
```

___

# Populate your database

If your seeding scripts aren't giving you all the data you need, you might lurk at your sexy staging database.

Here's what Heroku's team has to say about it:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u7pua9gc7hde84psgn5x.png)

Looks like you'll have to work on that seeding then.

You don't want to ? Tell me your reasons in comments please.

Anyway I've got you covered.

1. One could simply use his staging database both for reviews and staging environment. (bad solution imho)

2. One could hack his way around : 
```
# app.json
{
  "environments": {
    "review": {
      "buildpacks": [
        {
          "url": "https://github.com/heroku/heroku-buildpack-cli"
        },
        ...
      ],
      "scripts": {
        "postdeploy": "heroku pg:copy {STAGING_APP_NAME}::DATABASE DATABASE --app $HEROKU_APP_NAME --confirm $HEROKU_APP_NAME  && sleep 1s && rails db:migrate"
      }
    }
  }
}
```

It consists on installing [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) directly on every app reviews. Then the `postdeploy` script launchs the appropriate heroku command and runs migrations. Beware you'll need to set HEROKU_API_KEY first. RTFM !


# References:
- official documentation for heroku reviews (https://devcenter.heroku.com/articles/github-integration-review-apps)
- official documentation on `app.json` (https://devcenter.heroku.com/articles/app-json-schema)
- official documentation about buildpacks (https://devcenter.heroku.com/articles/app-json-schema#buildpacks)
- heroku buildpack-cli (https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-cli)
- official documentation about heroku database handling (https://devcenter.heroku.com/articles/heroku-postgres-backups#direct-database-to-database-copies)