Copy Figaro's application.yml with Capistrano Deploy - Rails - ruby-on-rails

I am trying to deploy my Rails application to a staging server with Capistrano, nginx and Puma. I am using the Figaro gem to setting ENV variables. When deploying, the standard Capistrano configuration does not look for and copy the application.yml file before deploying the app, and of course the deployment fails because of missing credentials. I have found a few Gists with a snippet to insert into the deploy.rb. These looked promising but I was insure about the before/after actions because the ones they were using, didn't match up with the actions used in my deploy.rb file. I also tried the capistrano-figaro-yml gem which seamed to copy the file but would not update the file if it was changed locally.
Is there a more standard way to trigger a copy of the application.yml file with the Capistrano deployment? Thanks

Adding set :linked_files, %w{config/application.yml} to deploy.rb got it going.

Related

Figaro Environment Variables in Gemfile

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.

Rails require statements in environment config files

In my config\environments\development.rb and config\environments\production.rb files, I set some global variables. In the example below, I have a a Redis instance that points to our cache, and a Statsd instance that points to the DataDog agent.
config.x.cache = Redis.new(url: ENV["CACHE"])
config.x.statsd = Statsd.new('localhost', 8125)
In the case of Redis, I added gem 'redis' to my gem file, ran bundle install and everything worked fine. In the case of StatsD, however, it seems that I need to also add require 'statsd' at the top of the development.rb and production.rb files in order to be able to create the instance. Of course, I also added gem 'dogstatsd-ruby' to my gem file and ran bundle install, but that didn't seem to be enough. If I don't add the require statement at the top of the config files, I get the following error when I try to run my Rails app:
uninitialized constant Statsd (NameError)
Can anyone explain why I have to add the require statement only in this particular case (StatsD), or is there is a better way to do this? Thanks!

Environment variables on production

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)

Most of my assets suddenly return 404 after a push to heroku

I have deployed this app (rails 3.2.11) a million times, I haven't messed with any settings, but now I'm greeted with this:
Why did this happen out of the blue? My conents of application.rb include config.assets.enabled = true - never had any issues.
In fact running it locally on port 3000 seems to not have any issues whatsoever.
After deploying to heroku this morning, it seems that it loads nothing that's inside /assets/
Interestingly, after copying the files over to try and just make a new app, git commit results in all the stuff you'd expect as well as a LONG list of these which I think might be related:
Edit: Interestingly enough SOME of the assets have loaded, like the logo and the background, but the rest as you can see return 404.
put line in config/environments/production.rb
config.assets.compile = true
it worked as it will compile the assets on runtime, just like in development environment, but its makes application slow, the best way is to either compile the assets locally in production environment with rake task(RAILS_ENV=production bundle exec rake assets:precompile) and commit your generated assets in public/assets and then do deployment. or, heroku run rake assets:precompile
I had this problem today with rails 4 on heroku. The article provided by #Jeff is a little bit old but, the gem repository has a good readme.
Summarizing, you will need to add two gems to your Gemfile:
gem 'rails_serve_static_assets' (it will solve the static assets problem) and
gem 'rails_stdout_logging' (which the previous one depends on).
Heroku released a gem to handle the assets without needing to turn off compilation or to manually compile.
https://devcenter.heroku.com/articles/ruby-support#static-assets
Just add this to your Gemfile and redeploy.
gem 'rails_serve_static_assets', group: [:production]
For Rails 4, use:
config.serve_static_assets = true
The default was false. We needed this after removing the rails_12factor gem.
Rails recommended that this setting config.serve_static_assets by default should be disabled i.e. set to false. Here is the default configuration in config/environments/production.rb generated in rails app
Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
So if you are setting it to true in your local app then that's still fine. But if you are deploying your app on Apache or ngix or anything other than heroku then its not advisable to make config.serve_static_assets=true in your production.rb config file. Here is the documentation from Rails guides.
config.serve_static_files configures Rails itself to serve static
files. Defaults to true, but in the production environment is turned
off as the server software (e.g. NGINX or Apache) used to run the
application should serve static assets instead. Unlike the default
setting set this to true when running (absolutely not recommended!) or
testing your app in production mode using WEBrick. Otherwise you won't
be able use page caching and requests for files that exist regularly
under the public directory will anyway hit your Rails app.
URL - http://guides.rubyonrails.org/configuring.html
To make the assets to load with the corresponding fingerprint of each file verify the configuration config/environments/production.rb has the instruction:
ruby
# Load assets with fingerprint behavior
config.assets.digest = true

How do I tell compass this Rails' environment is a development one?

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

Resources