Multi Environment Credentials for Production and staging

Public
Adds production and staging credentials
Icons/chart bar
Used 16 times
Created by
d dpaluy

Usage
Keeping your keys secured separated by different environments

1. Set an ENV(RAILS_MASTER_KEY) in your server with the key mentioned in config/credentials/production.key or staging.key
Example: heroku config:set RAILS_MASTER_KEY=XXXXXXXXX --remote production
2. Set an ENV(PIPE_ENV) to your environment
Example: heroku config:set PIPE_ENV=production --remote production
3. Edit credentials: `rails credentials:edit -e production`

Reference:
https://stackoverflow.com/questions/61582133/rails-encrypted-credentials-with-heroku-staging-environment-using-production-se?answertab=active#tab-top

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/x7ms3E"
Template Source

Review the code before running this template on your machine.

run "rails credentials:edit --environment production"

if yes?('Do you want to add staging credentials?')
  run "rails credentials:edit --environment staging"
end

application(nil, env: "production") do
<<-CODE
config.require_master_key = true
if ENV["PIPE_ENV"].present?
  Rails.application.config.credentials.content_path = Rails.root.join("config/credentials/\#{ENV["PIPE_ENV"]}.yml.enc")
end
CODE
end

file_content = <<~EOF
module Rails
  class << self
    def pipe_env
      @_pipe_env = ActiveSupport::StringInquirer.new(ENV["PIPE_ENV"].presence || Rails.env)
    end
  end
end
EOF

create_file 'config/initializers/pipe_env.rb', file_content
Comments

Sign up or Login to leave a comment.