I'd like to be able to push code to dev.myapp.com
for testing and then to www.myapp.com
for production use. Is this possible with Heroku?
Your interface to Heroku is essentially a Git branch. The Heroku gem does some work through their API, but within your Git repository, it's just a new remote branch.
heroku create yourapp # production
git br -D heroku # delete the default branch
heroku create staging-yourapp # staging
git br -D heroku # delete the default branch
Once you set up multiple applications on Heroku, you should be able to configure your Git repository like this:
git remote add staging [email protected]:staging-yourapp.git
git push origin staging
git remote add production [email protected]:yourapp.git
git push origin production
I usually work in a 'working' branch, and use Github for my master.
Assuming that's the case for you, your deploy workflow would probably look something like:
git co -b working
# do some work
# push to github:
git co master
git merge working
git push
# push to staging:
git co staging
git merge master
git push origin staging
# push to production
git co production
git merge master
git push origin production
heroku create yourapp --remote your-remote
–
Philosopher heroku
commands need to include --app staging
or --app production
. Is there any way to set a default? (Asking as a comment b/c this seems too targeted to be a full-fledged SO question.) –
Atrophy git push staging edge:master
–
Stoss staging
and production
, what purpose does the master
branch serve? Couldn't you simplify and make master
the home for the production code? –
Capacious master
branch on GitHub, the staging
branch on GitHub, and the production
branch on GitHub. –
Ruthannruthanne This explains everything you need to know if your a newbie like me: http://devcenter.heroku.com/articles/multiple-environments
A key part of the original question is about linking up the staging app to a subdomain (dev.myapp.com) of the main app (www.myapp.com). This hasn't been addressed in any of the answers.
Step 1: Configure both production ('myapp') and staging ('staging-myapp') versions of your app as is indicated in the answer by Luke Bayes
Step 2: In your domain management system (e.g. GoDaddy):
Create a CNAME record: dev.myapp.com
that points to: proxy.heroku.com
Step 3: Configure Heroku to route dev.myapp.com to staging-myapp:
heroku domains:add dev.myapp.com --app staging-myapp
After the CNAME record has had time to propagate, you will be able to run your staging app at dev.myapp.com.
before_filter
hook to my application_controller
to catch EVERYTHING in staging and force the user to login as an admin, then set an admin cookie so I can still see the app from the point of view of a 'non-admin'. Working pretty good for me. –
Coligny You should check the heroku_san
It does a pretty good job juggling with environments on heroku.
Things are easier now. Here's how you do it...
Create an app for each environment
$ heroku create myapp --remote production
$ heroku create myapp-staging --remote staging
This will create named remote repos for each app, which you can see in .git/config
.
You can now use either the --app or --remote switches to target a particular app:
$ heroku info --app myapp-staging
$ heroku info --remote staging
Set Rails environments
For Rails apps, Heroku defaults to the "production" environment. If you want your staging app to run in a staging environment, create the environment in your project and set the corresponding RAILS_ENV and RAKE_ENV environment variables on the app:
$ heroku config:set RACK_ENV=staging RAILS_ENV=staging --remote staging
Configure environments
If you have other configuration variables you'll need to pass them in for each environment as well.
$ heroku config:set AWS_KEY=abc --remote staging
$ heroku config:set AWD_SECRET=123 --remote staging
...etc
That's a huge pain though so I just use my snappconfig gem and run
$ rake heroku:config:load[myapp-staging]
to load my project's YAML config files into Heroku.
Deploy
Now you just push to Heroku like this:
$ git push staging master
$ git push production master
and migrate like this:
$ heroku run rake db:migrate --remote staging
$ heroku run rake db:migrate --remote production
(See Managing Multiple Environments for an App | Heroku Dev Center for more info and shortcuts.)
RAILS_ENV
and RACK_ENV
to staging
is discouraged by Heroku: "It may be tempting to create another custom environment such as “staging” and create a config/environments/staging.rb and deploy to a Heroku app with RAILS_ENV=staging. This is not a good practice. Instead we recommend always running in production mode and modifying any behavior by setting your config vars." More on this here: devcenter.heroku.com/articles/… –
Hawse © 2022 - 2024 — McMap. All rights reserved.
git push staging edge work
? – Strainer