How to Set Up a Staging Environment for Your Existing Heroku App

It's a great idea to have an intermediate environment between Development and Production that is as similar to Production as possible. The purpose is for testing. Your local environment can be too different from the way Production is set up, so when you push straight from Development to Production, you run the risk of your changes breaking thingsā€”on a live environment (Yikes!). The good news is that Heroku makes it pretty easy to set up another environment that is pretty much the same as Production.

Assuming you already have a heroku app up and running, the following steps will get you your staging environment:

  1. List your git remotes with git remote -v. If you have the default setup, your live app's remote name is 'heroku'.
  2. You may now want to rename your 'heroku' remote to 'production': git remote rename heroku production. Now, for example, if I run git remote -v again, I get the following results:

    origin  https://github.com/MarinaMurashev/blog.git (fetch)
    origin  https://github.com/MarinaMurashev/blog.git (push)
    production  git@heroku.com:marinamurashev.git (fetch)
    production  git@heroku.com:marinamurashev.git (push)
    
  3. Now it's time to actually create your staging environment. Run heroku fork -a your-production-app-name your-staging-app-name. In my case, I ran heroku fork -a marinamurashev staging-marinamurashev. This will not only replicate your Production app's setup, but also its database. Sweet!

  4. If you run git remote -v now, you'll see your staging app, like this:

    origin  https://github.com/MarinaMurashev/blog.git (fetch)
    origin  https://github.com/MarinaMurashev/blog.git (push)
    production  git@heroku.com:marinamurashev.git (fetch)
    production  git@heroku.com:marinamurashev.git (push)
    staging-marinamurashev  git@heroku.com:staging-marinamurashev.git (fetch)
    staging-marinamurashev  git@heroku.com:staging-marinamurashev.git (push)
    
  5. You'll want to rename you remote staging to something more easier to refer to. I like to rename it to just 'staging': git remote rename staging-marinamurashev staging. Results:

    origin  https://github.com/MarinaMurashev/blog.git (fetch)
    origin  https://github.com/MarinaMurashev/blog.git (push)
    production  git@heroku.com:marinamurashev.git (fetch)
    production  git@heroku.com:marinamurashev.git (push)
    staging git@heroku.com:staging-marinamurashev.git (fetch)
    staging git@heroku.com:staging-marinamurashev.git (push)
    
  6. Visit your new staging environment. In my case, I would go to staging-marinamurashev.herokuapp.com.

And that's it! Now when you push to master, remember that you can't run git push heroku master, but instead you will run git push production master. And in the same way, when you push to staging (which, hopefully, you'll do first) you do so with git push staging master.

Another thing to note is that if you want to get into the console on your staging app, you need to specify the app like this: heroku run console --app your_staging_app_name.

That's all, folks.