我希望能够将代码推送到dev.myapp.com
测试,然后再推送到www.myapp.com
生产使用。Heroku 有可能吗?
5 回答
Heroku 的接口本质上是一个 Git 分支。Heroku gem 通过他们的 API 做一些工作,但是在你的 Git 存储库中,它只是一个新的远程分支。
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
一旦你在 Heroku 上设置了多个应用程序,你应该能够像这样配置你的 Git 存储库:
git remote add staging git@heroku.com:staging-yourapp.git
git push origin staging
git remote add production git@heroku.com:yourapp.git
git push origin production
我通常在“工作”分支中工作,并使用 Github 作为我的主人。
假设您是这种情况,您的部署工作流程可能类似于:
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
这解释了如果您是像我这样的新手,您需要知道的一切:http: //devcenter.heroku.com/articles/multiple-environments
原始问题的一个关键部分是将登台应用程序链接到主应用程序 (www.myapp.com) 的子域 (dev.myapp.com)。任何答案都没有解决这个问题。
第 1 步:配置应用程序的生产('myapp')和暂存('staging-myapp')版本,如 Luke Bayes 的回答中所示
第 2 步:在您的域管理系统(例如 GoDaddy)中:
Create a CNAME record: dev.myapp.com
that points to: proxy.heroku.com
第 3 步:配置 Heroku 以将 dev.myapp.com 路由到 staging-myapp:
heroku domains:add dev.myapp.com --app staging-myapp
在 CNAME 记录有时间传播之后,您将能够在 dev.myapp.com 上运行您的暂存应用程序。
你应该检查heroku_san
它在处理 heroku 上的环境方面做得很好。
现在事情变得更容易了。这是你如何做到的......
为每个环境创建一个应用程序
$ heroku create myapp --remote production
$ heroku create myapp-staging --remote staging
这将为每个应用程序创建命名远程存储库,您可以在.git/config
.
您现在可以使用--app或--remote开关来定位特定应用程序:
$ heroku info --app myapp-staging
$ heroku info --remote staging
设置 Rails 环境
对于 Rails 应用程序,Heroku默认为“生产”环境。如果您希望您的暂存应用程序在暂存环境中运行,请在您的项目中创建环境并在应用程序上设置相应的RAILS_ENV和RAKE_ENV环境变量:
$ heroku config:set RACK_ENV=staging RAILS_ENV=staging --remote staging
配置环境
如果您有其他配置变量,则还需要为每个环境传递它们。
$ heroku config:set AWS_KEY=abc --remote staging
$ heroku config:set AWD_SECRET=123 --remote staging
...etc
不过那是一个巨大的痛苦,所以我只使用我的snappconfig gem 并运行
$ rake heroku:config:load[myapp-staging]
将我的项目的 YAML 配置文件加载到 Heroku。
部署
现在你只需像这样推送到 Heroku:
$ git push staging master
$ git push production master
并像这样迁移:
$ heroku run rake db:migrate --remote staging
$ heroku run rake db:migrate --remote production
(有关更多信息和快捷方式,请参阅管理应用程序的多个环境 | Heroku 开发中心。)