【问题标题】:Heroku differentiate between production and staging appHeroku 区分生产和登台应用程序
【发布时间】:2013-09-06 21:52:17
【问题描述】:

我正在创建一个 Play 2.1.3 网络应用,我在 Heroku 中设置了两个环境,一个用于生产环境,一个用于暂存环境。

区分这两种环境的最简单方法是什么?例如,我不希望搜索机器人为我的暂存应用程序编制索引,所以我想添加一个 robots.txt 用于暂存,但在推送存储库时它也会被添加到生产环境中。

我是否应该在我的计算机上有两个本地(app-prodapp-staging)播放应用程序和单独的 git 存储库?然后我必须让这两个应用程序保持同步。在验证 Heroku 上的暂存应用程序成功运行后,我必须将这些更改与我计算机上本地的生产应用程序同步(我如何使用 Git 做到这一点?),然后将这些更改推送到 Heroku 上的生产应用程序。

【问题讨论】:

    标签: git heroku playframework synchronization playframework-2.0


    【解决方案1】:

    您可以创建一个名为 robots.txt 的路由,该路由返回 404 或实际资产。它可以查看 application.conf ,然后它会从您为不同环境设置的环境变量中读取以修改应用行为。

    编辑:

    我应该更好地解释我的解决方案。您可以创建一个控制器以具有一些次要逻辑。这里应该给出返回 404 或实际 robots.txt 文件的逻辑。

    package controllers
    
    import java.io.File
    import play.api.mvc._
    
    object Robots {
    
      def get = Action {
        Ok.sendFile(
          content = new File("/path/to/your/robots.txt"),
          fileName = _ => "robots.txt"
        )
      }
    }
    

    您可以在此处找到有关提供文件的文档:http://www.playframework.com/documentation/2.2.x/ScalaStream

    然后路由文件就会有

    GET /robots.txt   controllers.Robots.get
    

    这将比资产控制器具有更高的优先级

    【讨论】:

    • 感谢您的建议,我尝试了您的建议。 #{if Play.mode.isTest()} GET /robots.txt controllers.Assets.at(path="/public", file) #{/if} #{else} GET /robots.txt 404 #{/else} 但我在 404 行出现编译错误 "Controller method call expected"
    【解决方案2】:

    我不熟悉 Play,但对于我的应用程序,我在它们上使用基本身份验证提示进行暂存,因此我知道在我处理它或编制索引时什么都看不到。

    如果您想使用更传统的 git 流程,最好拥有两个 Heroku 应用程序并使用相应的分支从单个 git 存储库部署到它们 - 当您将存储库连接到 Heroku 时,然后将远程命名为 productionstaging,所以你推的时候更容易区分。 (到目前为止,您可能会习惯于看到git push heroku master

    例如

    git remote add production <git url of production heroku app>
    git remote add staging <git url of staging heroku app>
    

    那么你的部署过程就变成了;

    • git push staging staging:master - 将本地暂存分支部署到 staging 远程主分支
    • 在暂存 Heroku 应用时进行验证
    • git checkout master - 切换到本地 master 分支
    • git merge staging - 将 staging 合并到本地 master
    • git push production master - 将本地主机部署到production 远程

    Heroku 最近还引入了一项名为 Pipelines 的新功能,听起来它可能也适合 https://devcenter.heroku.com/articles/labs-pipelines

    【讨论】:

      猜你喜欢
      • 2019-08-29
      • 2012-11-15
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 2013-03-19
      • 2017-02-20
      相关资源
      最近更新 更多