简体   繁体   中英

How to hide password in git repository?

I have a very simple Rails app that sends out a welcome email when the user signs up. I'm using my gmail account to send the message, and I have the password for my gmail account stored in the app, as shown below:

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"


I want to hide the password stored in the application.yml file in my git repository. I tried adding the application.yml to my gitignore file, but that just crashes my app.

How do I hide this password in my git repository so that my app still works, and I don't have to put my app into a private repository?

You basically can't. That sort of info is best stored as an environment variable on the server, or in a config file that you don't add to git.

my exemple for 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

on your server edit bash profile:

nano .bash_profile

and add your value:

export SENDGRID_USERNAME=yourusername
export SENDGRID_PASSWORD=yourpassword

after this on my local machine I have to close all console windows and open it again to initialize these env variables and you are good to go. If you do this on vps you probably need to restart your vps, not sure.

Try using dotenv gem https://rubygems.org/gems/dotenv . By mentioning .env files in .gitignore you can achieve your desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM