git 部署 + 配置文件 + Heroku [英] GIt Deployment + Configuration Files + Heroku

查看:35
本文介绍了git 部署 + 配置文件 + Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Heroku 来托管 Rails 应用程序,这意味着使用 Git 部署到 Heroku.由于 Heroku 上的纯 Git 工作流程",任何需要上游到服务器的内容都必须在我的本地机器上进行相同的配置.

I'm using Heroku to host a Rails app, which means using Git to deploy to Heroku. Because of the "pure Git workflow" on Heroku, anything that needs to go upstream to the server has to be configured identically on my local box.

但是,根据我是在本地设置中还是部署在 Heroku 上,我需要让某些配置文件有所不同.同样,由于 Heroku 使用的部署方法,我无法使用 .gitignore 和模板(正如我多次看到的建议,并在其他项目中使用过).

However I need to have certain configuration files be different depending on whether I'm in the local setup or deployed on Heroku. Again, because of the deployment method Heroku uses I can't use .gitignore and a template (as I have seen suggested many times, and have used in other projects).

我需要的是 git 以某种方式跟踪文件的更改,但有选择地告诉 git 在从特定存储库中提取时不要覆盖某些文件 - 基本上只进行单向更改.

What I need is for git to somehow track changes on a file, but selectively tell git not to override certain files when pulling from a particular repo -- basically to make certain changes one-way only.

这能做到吗?我很感激任何建议!

Can this be done? I'd appreciate any suggestions!

推荐答案

您可以将配置变量持久存储在每个 heroku 应用程序的本地设置中,这样它们就不必出现在您的代码中!所以相同的代码可以在多个 heroku 站点上运行,但具有不同的配置.它非常简单、容易、优雅......

You can have config vars persistently stored ON each heroku app's local setup so they do not have to be in your code at all! so the same code can run on multiple heroku sites but with different configuration. It very simple, easy, elegant...

这是我们使用的方法.(我们将它用于相同的事情......我们在 Heroku 有多个相同应用程序的克隆,但我们只想要 github 上的一个源,在我们的开发本地目录中,我们执行 PUSH to ORIGIN (github),然后当我们有按照我们喜欢的方式,我们 CD 到 prod 本地目录,该目录转到 SAME github 存储库,我们只从 GITHUB 中PULL 到这个目录,从不推送(例如,所有推送到 github 来在我们的 dev 目录中,prod 目录只是另一个 heroku 应用程序的暂存区.)

It's the approach we used. (We used it for the SAME thing... we have multiple clones of the SAME app at Heroku, but we want only ONE source at github, in our dev local directory we do the PUSH to ORIGIN (github), then when we have it the way we like it, we CD to the prod local directory, which goes to the SAME github repository, and we ONLY PULL from GITHUB into this directory, never push (eg, all pushes to github come from our dev directory, the prod directory is just a staging area for the other heroku app.)

通过在不同的 HEROKU 站点上使用不同的配置(如下所述),完全相同的代码适用于两个 heroku 站点.

By having the different configs ON the different HEROKU sites (as explained below), the EXACT SAME CODE works on BOTH heroku sites.

所以我们的工作流程是:(关键是两个目录都指向同一个github repo)

So our workflow is: (the key is that BOTH directories point to SAME github repo)

cd myDEVdir
*....develop away....*
git add .
git commit -am "another day, another push"
git push origin  *(to our SINGLE github repo)*
git push heroku  *(test it out on heroku #1)*

cd ../myPRODdir
git pull         *(grabs SAME code as used on other site *)
git push heroku  *(now the SAME code runs on Heroku #2)*

就是这样!

现在这里是如何在 heroku 站点上保留站点特定的配置变量:

Now here's how you keep your site-specific config vars ON the heroku site:

http://docs.heroku.com/config-vars

在本地命令行上,对于两个本地目录中的每一个,执行:

on your local command line, for EACH of your two local directories, do:

$ heroku config:add FIRST_CONFIGVAR=fooheroku1
Adding config vars:
  FIRST_CONFIGVAR => fooheroku1

$ heroku config:add SECOND_CONFIGVAR=barheroku1
Adding config vars:
  SECOND_CONFIGVAR => barheroku1

查看您定义的那些:

$ heroku config
FIRST_CONFIGVAR => fooheroku1
SECOND_CONFIGVAR => barheroku1

然后 cd 到您的另一个目录 myPRODdir 并执行相同的操作,只需将相同的远程 heroku vars 设置为 fooheroku2 和 barheroku2.

then cd to your other directory myPRODdir and do the SAME thing, only set the same remote heroku vars to fooheroku2 and barheroku2.

然后在您的 rails 应用程序中,您可以像这样简单地引用它们:

then in your rails app you simple refer to them like so:

a = ENV['FIRST_CONFIGVAR']

一个应用程序将读取fooheroku1",另一个应用程序将读取fooheroku2"

One app will read 'fooheroku1' the other app will read 'fooheroku2'

最后,在您以 DEV 模式运行的本地目录 myDEVdir 上,将相同的配置命令放入您的 config/environment/development.rb 文件中,您的开发"版本的配置变量将被设置为任何它们应该是:

And finally, on your LOCAL directory myDEVdir, where you run in DEV mode, put the same config commands in your config/environment/development.rb file your 'dev' version of the config vars will be set to whatever they should be:

ENV['FIRST_CONFIGVAR'] = "foodev"
ENV['SECOND_CONFIGVAR'] = "bardev"

简单、优雅.谢谢,Heroku!

Easy, elegant. Thanks, Heroku!

这篇关于git 部署 + 配置文件 + Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆