如何在git存储库中隐藏密码?

时间:2013-07-14 18:27:07

标签: ruby-on-rails git github

我有一个非常简单的Rails应用程序,当用户注册时会发送一封欢迎电子邮件。我正在使用我的Gmail帐户发送邮件,并且我的应用程序中存储了我的Gmail帐户密码,如下所示:

application.rb中

require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

ENV.update YAML.load(File.read(File.expand_path('../application.yml', __FILE__)))

module TestMailApp
  class Application < Rails::Application

    config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "my address",
      :user_name            => "my_gmail_name",
      :password             => ENV["MAIL_PASSWORD"],
      :authentication       => :plain,
      :enable_starttls_auto => true
    }

    config.action_mailer.default_url_options = {
      :host => "my address"
    }   


application.yml

MAIL_PASSWORD: "my_password"


我想隐藏存储在我的git存储库中的application.yml文件中的密码。我尝试将application.yml添加到我的gitignore文件中,但这只是崩溃我的应用程序。

如何在我的git存储库中隐藏此密码,以便我的应用程序仍然有效,而且我不必将我的应用程序放入私有存储库?

3 个答案:

答案 0 :(得分:4)

你基本上不能。这种信息最好存储在服务器上的环境变量中,或者存储在你没有添加到git的配置文件中。

答案 1 :(得分:1)

我对sendgrid的例子:

<强> development.rb

  if ENV['SENDGRID_USERNAME']
    config.action_mailer.smtp_settings = {
        :address => 'smtp.sendgrid.net',
        :domain => 'heroku.com',
        :port => '587',
        :authentication => :plain,
        :user_name => ENV['SENDGRID_USERNAME'],
        :password => ENV['SENDGRID_PASSWORD']
    }
  end

在您的服务器上编辑bash配置文件:

nano .bash_profile

并添加您的值:

export SENDGRID_USERNAME=yourusername
export SENDGRID_PASSWORD=yourpassword

在我的本地机器上之后,我必须关闭所有控制台窗口并再次打开它以初始化这些env variables,你很高兴。如果你在vps上执行此操作,则可能需要重新启动vps,不确定。

答案 2 :(得分:0)

尝试使用dotenv gem https://rubygems.org/gems/dotenv。通过提及.gitignore中的.env文件,您可以获得所需的结果。