Access Environment Variables in production.rb in rails - ruby-on-rails

I want to access the environment variables in production.rb , so I have created a .bashfile & write the variable like export key = value.
I am able to get those variables in rails console but sidekiq is not taking them for sending mails , I am using capistrano for deployment.

Related

Setting Rails Credentials For Production Mode

I am trying to store my stripe live key via Credentials as shown in the Securing Rails Application Guide: https://guides.rubyonrails.org/security.html#custom-credentials
I'm not sure what I'm doing wrong, the keys are written to the credentials file and in the test and development environments they work but when pushing to production I get errors that say my API Keys are not set. Here are what my credentials file and production config look like:
Credentials file ->
stripe_live: xxx.xxx.xxx
config/environments/production:
Stripe.api_key = Rails.application.credentials.stripe_live
I've also tried setting the keys with this command
rails credentials:edit --environment production and still no luck in the production environment.
How do I set rails credentials for the production environment? Do I need to set the RAILS_MASTER_KEY as an env variable in my production environemtn?
Set RAILS_MASTER_KEY to the string located within config/master.key . Rails automatically will use this value to decrypt your credentials file. A separate credentials file is optional, but if you do chose to use the separate credentials file you need to be sure that you use that key for the RAILS_MASTER_KEY env variable.

could not get Environment variable in staging server in rails 3 and capistrano 2.15

I am working on rails 3 application . I use capistran 2 for deploying purpose on digital ocean.
Now, I want to store the clients gmail username and password.
I do not want to store it into the code as it is sensitive information.
I want to store it to the server environment variable .
so I make env. variable by following command
export NEW_VAR="Testing export"
I checked it by following command and its saved as env. variable
echo $NEW_VAR
Now I want to access it in my rails application at environment folder in staging.rb and production.rb
I try to use the dotenv gem but it gives me difficulty in getting the env. variable as I am using capistrano 2.
Please help me.
Thanks
Use Figaro gem. Its pretty easy to setup. https://github.com/laserlemon/figaro

Heroku S3 Env variables

I'm trying to use carrierwave to upload images to S3. It works locally but when I go to deploy to heroku I get the following error:
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_
access_key
The keys are definitely set because I can see them when I run heroku:config
I've searched every answer I could find on stack and I searched through every answer on the first 3 pages of Google. None of them have worked.
I know the uploading works so it's not the code that's a problem. What settings or variables do I have to set to make this work?
Please help, I can't move forward with my app until this is done (so I can deploy to heroku again without it being stopped because of this error.)
Some info:
Environment Variables
You've got a problem with the calling of your environment variables in
Heroku. ENV vars are basically variables stored in the OS /
environment, which means you've got to set them for each environment
you attempt to your deploy application
heroku config should should the ENV vars you've set. If you don't see ENV['AWS_ACCESS_KEY'] etc, it means you've not set them correctly, which as explained is as simple as calling the command heroku config:add YOUR_ENV_VAR=VAR
Figaro
I wanted to recommend using Figaro for this
This is a gem which basically stores local ENV vars in config/application.yml. This allows you to store ENV variables locally; but more importantly, allows you to sync them with Heroku using this command:
rake figaro:heroku
This will set your env vars on Heroku, allowing you to use them with Carrierwave as recommended in the other answers
It sounds like you have set the ENV variables on Heroku, but you need to hook those up to CarrierWave.
In config/initializers/fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => Rails.configuration.s3_access_key_id,
:aws_secret_access_key => Rails.configuration.s3_secret_access_key,
}
end
In your environments/<environment>.rb file
Rails.application.configure do
config.s3_access_key_id = ENV['S3_ACCESS_KEY_ID']
config.s3_secret_access_key = ENV['S3_SECRET_ACCESS_KEY']
end
This sets your Rails config to the ENV variables on Heroku which makes them available as Rails.configuration.<key>

capistrano environment variables are not used in rails code

I am using capistrano to deploy a rails application. To set up email using sendgrid I need to setup environment variables.
I have used
set :default_environment, {
'SENDGRID_USERNAME' => "username",
'SENDGRID_PASSWORD' => 'password',
}
checking with
cap shell
cap > printenv
I can see the environment variables being set correctly.
However the app running through unicorn cannot see these variables, as the sending of email fails with SMTP authentication error.
I have also tried to source a file containing the exports using capistrano
run . app/shared/config/env
But environment variables are still not set
The development environment works fine, so I know the smtp credentials are ok.
How to set environment variables correctly so that the app can see them?
I don't know how to do it using Capistrano, but you can try Figaro gem.

Environment variable in Rails console and Pow

I can't access env variables in the Rails console, while in the application they work.
In .powenv I have export SENDGRID_PASSWORD="123"
In config/initializers/mail.rb there is:
ActionMailer::Base.smtp_settings = {
:password => ENV['SENDGRID_PASSWORD']
}
So in the console when I type UserMailer.welcome_mail.deliver there is an error 'ArgumentError: SMTP-AUTH requested but missing secret phrase'. However from the application it sends the mail successfully.
How can I make the env variables available in the console?
try
. .powenv
then
rails c
(dot is a command to run script on current environment)
Your Rails console isn't able to access the environment variable because Pow passes information from the .powenv or .powrc file into Rails ... Rails doesn't read those files on its own.
In other words, you're setting the ENV['SENDGRID_PASSWORD'] variable in the .powenv file, but that file is not being touched when you start the Rails console.
You'll need to set up a before_filter in your Application Controller that sets the ENV['SENDGRID_PASSWORD'] (or come up with another, similar, way of reading in the .powenv file from within that before_filter in your Rails app).
For posterity, you can add something like this to either your environment.rb, development.rb, or an initializer (config/initializers/pow.rb) depending on what load order you want:
# Load pow environment variables into development and test environments
if File.exist?(".powenv")
IO.foreach('.powenv') do |line|
next if !line.include?('export') || line.blank?
key, value = line.gsub('export','').split('=',2)
ENV[key.strip] = value.delete('"\'').strip
end
end

Resources