I’m currently deploying my Rails application on Amazon and I’m facing a problem with environment variables.
I'm using the dotenv gem on development and testing and it works just fine while trying to access my environment variables, but in production it does not seem to work. I read that the dotenv gem isn't meant to work on production. I have to set almost 20 different environment variables including API keys, etc., I'm deploying with rubber/capistrano.
How can I get this working in a clean way?
The dotenv-deployment readme mentions how you can use it in a production environment:
If you're using Capistrano 3+:
Just add .env to your list of linked_files:
set :linked_files, %w{.env}
If you're using Capistrano 2.x.x version:
In your config/deploy.rb file:
require "dotenv/capistrano"
It will symlink the .env located in /path/to/shared in the new release.
Remember to add :production group to the dotenv-rails gem in your application's Gemfile:
gem 'dotenv-rails', :groups => [:development, :test, :production]
You could use the figaro gem. I am using this and it works fine in production.
In Capistrano 3 add require "dotenv/rails-now" to your Capfile.
This will make sure that capistrano has access to the environment as well.
(We had issues with capistrano accessing the API token for appsignal, so capistrano wasn't able to notify appsignal when a new deploy was done)
Related
I have a Rails app with some private gems used for testing locally; I do not need to access them in production, on Heroku. They are loaded in the gemfile as source block, e.g:
group :development, :test do
source "https://myprivaterepo" do
gem "mycustomgem", "~> 1.0"
end
end
When I try to deploy to Heroku, the build fails ('could not fetch specs from https://myprivaterepo') because Heroku cannot access the gem source. I have set a BUNDLE_WITHOUT config var to ignore development and test gems, but this still doesn't prevent the build trying to fetch gems from this source. How can I prevent this, so that Heroku just ignores these gems and their source completely?
Using Ruby 2.2.4, Rails 4.2, Heroku-16 stack.
try this: heroku config:set BUNDLE_WITHOUT="development:test"
or
gem 'mycustomgem', source: "https://myprivaterepo.com" if ENV['RAILS_ENV'] != 'production'
I'm trying to set an auth token for my gemfile to access a private git repo.
i.e.
gem 'mygem', git: "https://ENV['GITHUB_AUTH_TOKEN']:x-oauth-basic#github.com/my_account/my_repo.git", tag: "0.0.1"
I can't work out how to store this in Figaro but make it accessible to bundle when I run bundle install.
Very similar to
This question
Except that rather than having a config/heroku_env.rb I have an config/application.yml file.
I'm sure the answer is ridiculously straightforward.
I'd like to keep it in that file as it keeps everything neatly in one place, but if not I can put it somewhere specific so long as it lines up with heroku nicely.
Any ideas?
I have found one way to do it that works, it's slightly annoying in that you have to keep the credentials in two different places.
.bundle
BUNDLE_GITHUB__COM: <auth_token>:x-oauth-basic
Gemfile
gem 'mygem', git: "https://github.com/my_account/my_repo.git", tag: "0.0.1"
# Note that you don't put anything in here, bundler sorts it out automagically
And then
heroku config:set BUNDLE_GITHUB__COM=<auth_token>:x-oauth-basic
Works.
Annoying because now application.yml has different content to my heroku file. But so be it.
Update:
Better solution
Just put
BUNDLE_GITHUB__COM: <auth_token>:x-oauth-basic
into both your application.yml and heroku config.
I wish someone had documented that somewhere, would have saved me a ton of trouble...
I think you need to run
figaro heroku:set -e production
for it to set set the environment variables in your config/application.yml file as Heroku environment variables.
I am trying to deploy a Rails project on Heroku. My Rails application uses mysql2.
I have tried using the taps gem but its not working. I get the following error when I run the command taps server mysql://root#localhost/heroku_ex tempuser tempass:
Failed to connect to database: Sequel::AdapterNotFound -> LoadError: cannot load such file -- mysql
Is there any way to deploy my application on Heroku? I would prefer to only use free add-ons if possible.
There are a few moving parts here.
First of all, Heroku doesn't support MySQL by default. If you want to use MySQL instead of PostgreSQL you will have to provision an add-on that provides it. There are currently at least two add-ons that provide MySQL or MariaDB¹ support with a free tier².
Next, Heroku doesn't run database servers on localhost. How can you handle different database configurations between your development machine and your Heroku server?
One strategy that is endorsed by Heroku is to store configuration in the environment. Following this model lets you alter your application's configuration by modifying environment variables instead of editing files. Luckily, Rails appears to override config/database.yml with configuration from the DATABASE_URL variable by default, so this approach should be a good fit.
Very often database add-ons will automatically set an environment variable for you. For example, the JawsDB Maria add-on sets JAWSDB_MARIA_URL when it is provisioned. This isn't the variable that Rails looks for, so you'll either have to tell Rails to look for JAWSDB_MARIA_URL instead of DATABASE_URL or manually set DATABASE_URL to contain the same URL that JawsDB provides in JAWSDB_MARIA_URL.
¹MariaDB is a fork of MySQL that aims to be fully-compatible.
²Note that the free tier may be quite limited, e.g. by only providing 5MB of storage. You may have to upgrade to a paid database tier as you continue to develop your application.
I have solved this by adding the pg gem in production and mysql2 in development environment.
group :development, :test do
gem 'mysql2'
end
group :production do
gem 'listen', '~> 3.0.5'
gem 'pg'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
I am developing both an app and a gem for it. The gem will be available from Gemfury when the app will be in production. But while developing, I would like to use the local path of the gem, so that I can modify both the gem and the app and see changes faster. How can I do this?
I know there is bundle config local.GEM GEM_PATH, but this only works for git sources, not for Gemfury.
I can set an env var and conditionally specify gems in the Gemfile, but I hope there is a better approach to this.
if ENV['RAILS_ENV'] == 'development'
gem 'your_gem', path: '/path/to/gem'
else
gem 'your_gem'
end
Then, locally, run
RAILS_ENV=development bundle install
It's a hack, for sure, but then again, so is all of this :)
I'm working with Ruby On Rails (v. 3.0.10) and using Compass (0.11.5).
I'm using two development environments:
The standard Rails' development environment, defined in config/environments/development.rb connected to a PostgreSQL database.
A copy of the development environment, dev-sqlite, defined in config/environments/dev-sqlite.rb, with the only difference being the attached database (this time a local SQLite when I'm on the run and I can't reach my development database server).
My issue with Compass is that when I'm running Rails in my dev-sqlite environment (using RAILS_ENV='dev-sqlite' before running any Rails command, Compass seems to work in production mode, and it does not regenerate my CSS files when I change the SCSS ones as it does when I'm in development environment. This makes my development work a lot harder...
I've tried to add this line to the config/compass.rb file and restart my local Rails server (with rails s), without success:
environment = :development if Rails.env == 'dev-sqlite'
In fact, even environment = :development doesn't change a thing.
Thanks in advance for the help!
I added the following to the bottom of my config file and it works well for me:
# Enable Debugging (Line Comments, FireSass)
# Invoke from command line: compass watch -e development --force
if environment == :development
output_style = :expanded
sass_options = {:debug_info => true}
end
Then you just need to specify that you are in development environment when you run compass watch (or compile). You can do this by running compass watch -e development --force. As you can see, I stuck that in a comment in my config file incase I forget.
Also, be sure to enable sass in chrome developer tools to take advantage of sass source maps