将 .gitignore 文件推送到特定远程
我制作了一个 Sinatra 应用程序,将托管在 Heroku 上,源代码将发布在 GitHub 上。问题是我有一个包含 API 密钥的文件,当前位于 .gitignore 中。有没有办法,我可以使用密钥文件将我的存储库推送到heroku,并在推送到GitHub时排除该文件?
提前致谢!
I made a Sinatra app, that will be hosted on Heroku, and the source will be up on GitHub. The problem is that i have a file with API keys, that is currently in .gitignore. Is there a way, that I can push my repo to heroku with the key file and exclude the file when pushing to GitHub?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以仅为了部署而维护一个单独的分支,但正确维护它需要很多纪律:
git add -f
以绕过您的排除) )。但是,您必须永远将您的生产分支合并到其他任何分支中,或者基于任何“生产提交”(其祖先包括您的“添加密钥”提交)启动分支。
一个更简单的方法是采用 Heroku 的习惯,即使用环境变量将您的秘密值传达给您的实例。请参阅配置和配置变量上的文档:
然后通过
ENV['KEY1']< 访问值/code> 和
ENV['KEY2']
位于初始化代码中或您需要的任何位置。为了支持非 Heroku 部署,您可以定义相同的环境变量,或者在环境变量不存在时转而读取现有的配置文件。It is possible to maintain a separate branch just for deployment, but it takes much discipline to maintain it properly:
git add -f
to bybass your excludes).However, you must then never merge your production branch into anything else, or start branches based on any “production commit” (one whose ancestry includes your “add the keys” commit).
An easier path is to adopt Heroku’s custom of using environment variables to communicate your secret values to your instances. See the docs on Configuration and Config Vars:
Then access the values via
ENV['KEY1']
andENV['KEY2']
in your initialization code or wherever you need them. To support your non-Heroku deployments, you could either define the same environment variables or fall back to reading your existing config files if the environment variables do not exist.Figaro gem 提供了一种解决此问题的好方法。它基本上在本地模拟 Heroku 的环境变量方法,并且可以轻松地在开发环境和 Heroku 之间保持密钥同步。
The Figaro gem provides a good way to manage this issue. It basically simulates Heroku's environment variable approach locally, and makes it easy to keep your keys in sync between your development environment and Heroku.