I am building a rails app on heroku, and want to deploy the js, css, and image files to a bucket on Amazon. I haven't found many resources for this, but I am using this (2012) tutorial for guidance; https://firmhouse.com/blog/complete-guide-to-serving-your-rails-assets-over-s3-with-asset_sync
The site is mainly css and js at the moment. Here is my code so far;
production.rb
Rails.application.configure do
config.action_controller.asset_host = "http://localize.s3.amazonaws.com"
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
end
initializers/asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = ENV['FOG_PROVIDER']
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
# Don't delete files from the store
config.existing_remote_files = "delete"
# Automatically replace files with their equivalent gzip compressed version
config.gzip_compression = true
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
config.manifest = true
config.custom_headers = { '.*' => { cache_control: 'max-age=31536000', expires: 1.year.from_now.httpdate } }
end
end
Heroku Vars
AWS_ACCESS_KEY_ID: *****************
AWS_SECRET_ACCESS_KEY: *****************************
FOG_DIRECTORY: localize
FOG_PROVIDER: AWS
FOG_REGION: us-west-2
gemfile
gem 'rails', '4.1.1'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'sdoc', '~> 0.4.0', group: :doc
#aws
gem "fog", "~>1.20"
gem 'asset_sync'
group :development do
gem 'thin'
end
group :production do
gem 'newrelic_rpm'
gem 'rails_12factor'
gem 'pg'
end
I also ran:
heroku config:add FOG_PROVIDER=AWS AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=yyy
Along with
heroku config:add FOG_DIRECTORY=localize
And then when I run
bundle exec rake assets:precompile
Or
RAILS_ENV=production bundle exec rake assets:precompile
I get this output;
rake aborted!
AssetSync::Config::Invalid: Fog directory can't be blank, Aws access key can't be blank, Aws secret access key can't be blank
Anyone who has experience with rails, heroku, and S3 who could guide me in the right direction would be much appreciated. Thanks in advance.
Ok looking at your settings there seems to be a few things wrong, Ill add what i normally use and hopefully it will help you
Production.rb
ExampleApp::Application.configure do
config.action_controller.asset_host = "http://exampleapp.s3.amazonaws.com"
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
end
asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = ENV['FOG_PROVIDER']
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
# Don't delete files from the store
config.existing_remote_files = "delete"
# Automatically replace files with their equivalent gzip compressed version
config.gzip_compression = true
# Use the Rails generated 'manifest.yml' file to produce the list of files to
# upload instead of searching the assets directory.
config.manifest = true
config.custom_headers = { '.*' => { cache_control: 'max-age=31536000', expires: 1.year.from_now.httpdate } }
end
end
Related
I am trying to make some tests with Rspec/Capybara/Poltergeist on my Rails 4 application, Phantomjs is installed (version 2.2.1), but I always get this error :
Failure/Error: visit(perfect_landing_page_path)
Capybara::Poltergeist::StatusFailError:
Request to 'http://127.0.0.1:49623/path' failed to reach server, check DNS
and/or server status
The test i'm working on :
require 'rails_helper'
RSpec.feature 'Subscription', :type => :feature do
let!(:plan) { create(:plan) }
let!(:landing_page) { create(:landing_page) }
before(:each) { landing_page.default_plan = plan }
describe 'landing_page#perfect_show' do
scenario 'form display', js: true do
plan_2 = create(:plan)
plan_3 = create(:plan)
landing_page.plans << plan_2
landing_page.plans << plan_3
visit(perfect_landing_page_path)
expect(page).to have_css(".start-now", count: 3)
first(".start-now").click
expect(page).to have_css("#new_user")
end
end
end
My Gemfile looks like this :
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'guard-rspec'
group :test do
gem 'database_cleaner'
gem 'capybara'
gem 'capybara-screenshot'
gem 'poltergeist'
gem 'selenium-webdriver'
gem 'shoulda-matchers', require: false
gem 'show_me_the_cookies'
end
My spec/support/capybara.rb file :
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require "capybara/poltergeist" # Add this line to require poltergeist
require 'selenium-webdriver'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:js_errors => false, :default_max_wait_time => 30, :timeout => 30, phantomjs_options: [
'--load-images=no',
'--ignore-ssl-errors=true',
'--ssl-protocol=any']})
end
Capybara.register_driver :poltergeist_debug do |app|
Capybara::Poltergeist::Driver.new(app, :inspector => true)
end
Capybara.configure do |config|
config.javascript_driver = :poltergeist
config.ignore_hidden_elements = true
config.default_max_wait_time = 30
end
My spec_helper.rb file :
require 'capybara/rspec'
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.filter_run :focus
config.run_all_when_everything_filtered = true
Did someone encounter this problem before ? Does anyone have a solution for this ? I have been banging my head for days looking on the web...
Thank you very much.
PS : it works fine on my friend's mac (Yosemite or El Capitan), just not on mine.
I was having this problem in CI (CodeShip).
Adding a call to bundle exec rake assets:precompile at the end of the setup commands seemed to fix it:
CodeShip CI Setup Commands:
rvm use 2.3.4 --install
bundle install
export RAILS_ENV=test
bundle exec rake db:schema:load
bundle exec rake assets:precompile
Also had a whitelist per #agbodike but in the rails_helper.rb
config.before(:each, js: true) do
page.driver.browser.url_whitelist = ["127.0.0.1"]
end
I had the same issue and it was due to a 3rd party script timing out. You can prevent loading of 3rd party scripts with a blacklist. For example:
config.before(:each, js: true) do
page.driver.browser.url_blacklist = ["http://use.typekit.net"]
end
would prevent any URL starting with http://use.typekit.net from being called for each test that uses the js driver. More information can be found at:
https://robots.thoughtbot.com/speed-up-javascript-capybara-specs-by-blacklisting-urls
Alternatively you can use a whitelist:
config.before(:each, js: true) do
page.driver.browser.url_whitelist = ["127.0.0.1"]
end
which will block all requests not to 127.0.0.1
I placed the configuration in spec/feature_helper.rb to ensure it was only set for feature specs.
You can avoid these by precompile the assets before tests.
And the code is:
RSpec.configure do |config|
config.before :all do
ENV['PRECOMPILE_ASSETS'] ||= begin
case self.class.metadata[:type]
when :feature, :view
STDOUT.write "Precompiling assets..."
require 'rake'
Rails.application.load_tasks
Rake::Task['assets:precompile'].invoke
STDOUT.puts " done."
Time.now.to_s
end
end
end
end
more info
I have used asset-sync and fog gem for assets upload to aws s3.I can uploaded precompiled assets files to aws-s3 while deploying but it taking lot of time.The deploy is not complete then it was stopped on bundle exec rake assets:precompile command.
Gem file:
gem "fog", "~>1.20", require: "fog/aws/storage"
gem 'asset_sync'
Asset sync configuration file:
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = "Your aws access key"
config.aws_secret_access_key = "Your aws secret access key"
config.fog_directory = ENV['FOGDIRECTORY']
config.fog_region = 'us-west-2'
end
Staging.rb file:
config.assets.enabled = true
config.assets.digest = true
config.action_controller.asset_host = "http://djrcjofcge7nb.cloudfront.net"
config.action_mailer.asset_host = "http://djrcjofcge7nb.cloudfront.net"
config.assets.initialize_on_precompile = true
Add the following configurations in your staging.rb file
config.assets.compile = true
config.eager_load = true
So, here is my Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
#
require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/migrations'
require 'capistrano/rails/assets'
require 'airbrake/capistrano3'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Here's my deploy file:
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'my_app'
set :repo_url, 'git#github.com:org/my-app.git'
# Default branch is :master
set :branch, 'dev'
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/webapps/my_app'
# Default value for :scm is :git
# set :scm, :git
# Default value for :format is :pretty
# set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
# set :pty, true
set :deploy_via, :remote_cache
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml config/secrets.yml}
set :normalize_asset_timestamps, %{public/images public/javascripts public/stylesheets}
# Default value for linked_dirs is []
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
set :bundle_flags, "--deployment --verbose"
# Default value for keep_releases is 5
# set :keep_releases, 5
set :ssh_options, { :forward_agent => true }
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart do
on roles (:app),:except => { :no_release => true } do
puts "\x1B[35m Restarting ... \x1B[0m"
execute "touch #{current_path}/tmp/restart.txt"
puts "\x1B[35m Done. \x1B[0m"
end
end
desc "Tag deployed release"
task :tag do
run_locally do
timestamp = Time.now.strftime("%Y_%m_%d_%H_%M_%S")
tag_name = "#{fetch(:stage)}_#{timestamp}"
latest_revision = fetch(:current_revision)
strategy.git "tag -f #{tag_name} #{latest_revision}"
strategy.git "push -f --tags"
info "[cap-deploy-tagger] Tagged #{latest_revision} with #{tag_name}"
end
end
after :publishing, :restart
after :restart, :cleanup
after :cleanup, 'deploy:tag'
#after 'deploy:finished', 'airbrake:deploy'
end
The issue is that when I run "cap production deploy:check" I get:
LoadError: cannot load such file -- airbrake/capistrano3
/home/dir/dev/my-app/Capfile:24:in `require'
/home/dir/dev/my-app/Capfile:24:in `<top (required)>'
/home/dir/.rvm/gems/ruby-2.1.0/gems/capistrano-3.2.1/lib/capistrano/application.rb:15:in `run'
/home/dir/.rvm/gems/ruby-2.1.0/gems/capistrano-3.2.1/bin/cap:3:in `<top (required)>'
/home/dir/.rvm/gems/ruby-2.1.0/bin/cap:23:in `load'
/home/dir/.rvm/gems/ruby-2.1.0/bin/cap:23:in `<main>'
/home/dir/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `eval'
/home/dir/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
If I change require 'airbrake/capistrano3'to require 'airbrake/capistrano' then I get NoMethodError: undefined methodinstance' for Capistrano::Configuration:Class`
Any ideas on how to fix it?
Thanks a lot!
BTW, here's my gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.4'
# Use mysql as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use unicorn as the app server
# gem 'unicorn'
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'haml'
gem 'devise'
gem 'will_paginate', '~> 3.0'
gem 'thin'
gem 'faye'
gem 'aasm'
gem 'useragent'
gem 'mini_magick'
gem 'carrierwave'
gem 'airbrake'
group :development do
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-rvm'
gem 'rspec-rails'
gem 'pry'
end
For googling folks, if you having this error after upgrading to latest Airbrake v5, there is a change
# Old way
# For Capistrano 2
# Capfile
require 'airbrake/capistrano'
# For Capistrano 3
# Capfile
require 'airbrake/capistrano3'
# config/deploy.rb
after 'deploy:finished', 'airbrake:deploy'
# New way
# For Capistrano 2
require 'airbrake/capistrano/tasks'
# For Capistrano 3
# Capfile
require 'airbrake/capistrano/tasks'
# config/deploy.rb
after :finished, 'airbrake:deploy'
https://github.com/airbrake/airbrake/blob/master/docs/Migration_guide_from_v4_to_v5.md#capistrano
same apply for Sidekiq, and other bunch of gems
Are you sure you have Airbrake 4.1.0 installed?
This is driving me insane! My app works properly on the local server, but when I uploaded to heroku, the css and js files didn't update. I read to precompile using rake assets:precompile to fix the issue, but then I uploaded to heroku again and now all my assets aren't loading at all! I've tried all variations of precompile, installed 12factor gem, turned some config settings from false to true, still not happening! When i look at the console, it says
[Error] ReferenceError: Can't find variable: jQuery
global code (application-38ccb09605964287831a37a0d9faf188.js, line 1)
but I do have Jquery! it works fine on local! I dunno what I'm doing wrong guys.
Gemfile
gem 'bootstrap-sass-rails'
gem 'rails_12factor', group: :production
gem 'rails', '4.0.0'
gem 'sqlite3', :group => [:development, :test]
group :production do
gem 'thin'
gem 'pg'
end
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
running $RAILS_ENV=production bundle exec rake assets:precompile made my heroku app work now. so i guess problem is fixed..
sorry, I don't have an explanation on why it works
Simply run
$ bundle exec rake assets:precompile
$ git add .
$ git commit -am "assets precompiled locally"
$ git push heroku master
twitter.gemspec file is
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'twitter/version'
Gem::Specification.new do |spec|
spec.add_dependency 'faraday', ['~> 0.8', '< 0.10']
spec.add_dependency 'multi_json', '~> 1.0'
spec.add_dependency 'simple_oauth', '~> 0.2'
spec.add_development_dependency 'bundler', '~> 1.0'
spec.authors = ["John Nunemaker", "Wynn Netherland", "Erik Michaels-Ober", "Steve Richert", "Steve Agalloco"]
spec.cert_chain = ['certs/sferik.pem']
spec.description = %q{A Ruby interface to the Twitter API.}
spec.email = ['nunemaker#gmail.com', 'wynn.netherland#gmail.com', 'sferik#gmail.com', 'steve.richert#gmail.com', 'steve.agalloco#gmail.com']
spec.files = %w(.yardopts CHANGELOG.md CONTRIBUTING.md LICENSE.md README.md Rakefile twitter.gemspec)
spec.files += Dir.glob("lib/**/*.rb")
spec.files += Dir.glob("spec/**/*")
spec.homepage = 'http://sferik.github.com/twitter/'
spec.licenses = ['MIT']
spec.name = 'twitter'
spec.require_paths = ['lib']
spec.required_rubygems_version = '>= 1.3.5'
#spec.signing_key = File.expand_path("~/.gem/private_key.pem") if $0 =~ /gem\z/
spec.summary = spec.description
spec.test_files = Dir.glob("spec/**/*")
spec.version = Twitter::Version
end
I want to degrade dependencies faraday 0.8 to 0.7 how can i do this
plase help me
If you just want the latest patch in the 0.7 tree, you just need to change the faraday line to this:
spec.add_dependency 'faraday', '~> 0.7.0'
Or you can specify a specific version if you want:
spec.add_dependency 'faraday', '0.7.1'
For more information I'd recommend reading the Bundler page about Gemfiles http://bundler.io/v1.3/gemfile.html