管理 Heroku 的生产和登台环境

每个 Heroku 应用程序至少在两个环境中运行:Heroku(我们称之为生产)和本地机器(开发)。如果有多个人正在使用该应用程序,那么你将拥有多个开发环境 - 通常每台计算机一个。通常,每个开发人员还将拥有运行测试的测试环境。不幸的是,随着环境变得不那么相似,这种方法就会崩溃。例如,Windows 和 Mac 都提供与 Heroku 上的 Linux 堆栈不同的环境,因此你无法始终确保在本地开发环境中运行的代码在将其部署到生产环境时的工作方式相同。

解决方案是使分段环境尽可能与生产类似。这可以通过创建托管登台应用程序的第二个 Heroku 应用程序来实现。通过暂存,你可以在影响实际用户之前,在类似生产的设置中检查代码。

从头开始

假设你在本地计算机上运行了一个应用程序,并且你已准备好将其推送到 Heroku。我们需要创建远程环境,分段和生产。为了养成先推进升级的习惯,我们将从这开始:

$ heroku create --remote staging
Creating strong-river-216.... done
http://strong-river-216.heroku.com/ | https://git.heroku.com/strong-river-216.git
Git remote staging added

默认情况下,heroku CLI 使用 heroku git 远程创建项目。在这里,我们使用 –remote 标志指定一个不同的名称,因此将代码推送到 Heroku 并对应用程序运行命令看起来与普通的 git push heroku master 略有不同:

 $ git push staging master
...
 $ heroku ps --remote staging
=== web: `bundle exec puma -C config/puma.rb``
web.1: up for 21s

一旦你的暂存应用程序启动并正常运行,你就可以创建生产应用程序:

$ heroku create --remote production
Creating fierce-ice-327.... done
http://fierce-ice-327.heroku.com/ | https://git.heroku.com/fierce-ice-327.git
Git remote production added
$ git push production master
...
$ heroku ps --remote production
=== web: `bundle exec puma -C config/puma.rb
web.1: up for 16s

有了这个,你就可以像两个独立的 Heroku 应用程序一样运行相同的代码库 - 一个分段和一个生产,设置相同。请记住,你必须指定你将在日常工作中使用哪个应用程序。你可以使用标记’–remote’或使用 git config 指定默认应用程序。