Skip to content

Instantly share code, notes, and snippets.

@katlandreth
Last active November 6, 2022 18:15
Show Gist options
  • Save katlandreth/c89667c9d9bf7b618c5617651a45568c to your computer and use it in GitHub Desktop.
Save katlandreth/c89667c9d9bf7b618c5617651a45568c to your computer and use it in GitHub Desktop.
Create Staging and Production apps on Heroku with corrisponding git branches

##Set-up

  • create a heroku account
  • install the heroku toolbelt: https://toolbelt.heroku.com/
  • log into heroku via the commandline with heroku login and enter your credentials.
  • add your AWS (or other) keys to the heroku environment (I did this from the webapp -from your apps list, click on your appname, then Settings > Reveal Config Vars and add your config variables)

##Create your app

  • in your terminal, cd to your app on your local machine
  • run ruby -v to verify your ruby version - copy this info
  • open your apps Gemfile and add the ruby version to the end of the file ie ruby "2.3.1"
  • commit everything to git
  • run heroku create
  • confirm that the heroku app has been created with git config --list | grep heroku
  • rename the app to your app's name with heroku apps:rename newname where newname is the name you want to give the app
  • 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
  • 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.com, navigating to your app, and clicking the "view app" button

##Fork your app

  • run heroku fork --from appname --to appname-staging where appname is the name of the original app and appname-staging is the name you want to give the staging version of the app. You could do this the other way around I suppose and fork the production app from the staging app.
  • give the new fork a git remote - first 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 branch git push staging staging:master. (Don't include all of th hash marks around the url you copied - I just put them there there to highlight where it goes.)
  • 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), and one for your github repo's "origin".
  • migrate your staging apps DB with heroku run rake db:migrate --remote staging

##Rename the "heroku" remote to "production"

It will be confusing to remember that "heroku" really means "production" when I use commands like git push heroku master so I'm renaming the remote to "production". That way all of my remote names are more semantic.

  • grab the production app's github url again by running heroku info -a appname where appname is the name of your production app (not the name of the remote) and copy the github url.
  • run git remote rm heroku
  • then git remote add production ####the url you copied### where production is the name you want to give your production remote. Don't include the hashes around the url you copied, I only included them to highlight where the url goes.

##Deploy from semantic branches

I want to be able to deploy from separate branches that relate to either the staging or production environments.

  • run git push staging staging:master to push from a branch called staging to the staging app's master branch.
  • run git push production production:master to push from a branch called production to the production remote's master branch.

There's a way to make local git branches track specific remotes so you could just run "git push" from staging branch and it would push to the staging remote's master branch. However, I'm a little leary of making it that easy and that general. If I'm pushing anything to any deployed app, I think I want to do it very deliberately.

##Quick Reference

  • migrate to a specific remote heroku run rake db:migrate --remote remotename ie heroku run rake db:migrate --remote staging
  • check remotes git remote -v
  • push to a remote from a specific branch git push remotename branchname:master ie git push staging staging:master

##Resources:

Deploying a Rails 4 App to Heroku https://devcenter.heroku.com/articles/getting-started-with-rails4#migrate-your-database

Manage multiple heroku environments: https://devcenter.heroku.com/articles/multiple-environments#managing-staging-and-production-configurations

Renaming Heroku Apps and Git Remotes https://devcenter.heroku.com/articles/renaming-apps

How to create a staging environment in Heroku https://agilewarrior.wordpress.com/2014/05/16/how-to-create-a-staging-environment-heroku/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment