Safely recompiling all assets on Rails 4 deployment? - ruby-on-rails

Right now I have occasional issues with the Rails 4 asset pipeline that requires removing all assets and then recreating the assets.
During deployment, after pulling the latest from git, I refresh the assets by running, rake assets:clobber and then rake assets:precompile on my server. The problem is that during the time the assets are removed, the page gets served, not surprisingly, with no assets.
Is there anyway to force Rails to recompile all assets without having to run assets:clobber?

Here is what I usually do, when precompiling the assets.
// At a terminal, remove all assets from the file system
$ rm -rf public/assets
// Precompile the assets
$ rake assets:precompile
Always worked for me.

First run rake assets:precompile then run rake assets:clean. That will only remove old assets. https://github.com/rails/sprockets-rails/blob/master/README.md#rake-task

Related

Asset compilation takes too long in a rails production server. Is it possible to reduce by utilizing local?

Currently we don't use Capistrano for some reason and here are the steps.
SCSS files are edited in the local(my computer), and pushed to git.
git pull at the production (aws)
RAILS_ENV=production bundle exec rake assets:precompile
Then passenger restart.
The problem we have here is that it takes (sometimes) a few minutes to create css and css.gz depending on how many scss files are modified.
Is it possible to compile all js, and scss at local(because it is ok to compile with a long period of time) and push files (css, css.gz, public/assets/.sprocket-manifest-xxx) to git?
You could run RAILS_ENV=production bundle exec rake assets:precompile locally and then commit and push these files. Maybe look into deploying with rsync so you don't have to commit your compiled assets.
Also take a look at what the Rails Guide says about Local Precompilation.

Heroku is serving old assets for Rails 5 application

I've deployed a new version of a Rails 5 app on Heroku, running on cedar-14 stack. It didn't precompile while deploying, so I did heroku run rake assets:precompile manually. Still, I can see it includes old assets while requiring css and js files.
My files are in app/assets so it's not possible that directory isn't in assets compile path.
My config on application.rb and production.rb:
config.assets.compile = true
# I checked the environment variable, it responds to 'enabled',
# which would return true for the option.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Which I changed to expire old assets.
config.assets.version='1.1'
I tried these, but they didn't work:
$ heroku restart
$ heroku run rake assets:precompile
$ heroku run rake assets:clobber
The weird thing with these is that they do not affect assets in heroku server, which I checked with $ heroku run ls public/assets. Even after $ rake assets:precompile, even though it says this:
WRITING /app/public/assets/application-{VERY_LONG_HASH}.js
WRITING /app/public/assets/application-{VERY_LONG_HASH}.js.gz
WRITING /app/public/assets/application-{VERY_LONG_HASH}.css
WRITING /app/public/assets/application-{VERY_LONG_HASH}.css.gz
when I peek with the $ heroku run ls public/assets, I still see old assets staying there.
EDIT: I solved it by deleting all the local assets in public/assets, recompiling them with $ rake assets:clean && rake assets:precompile and including these assets in my git repository. Here is one concern:
Shouldn't heroku be responsible of compiling my assets on the fly? I think I shouldn't be compiling my assets everytime I deploy my application. Thanks.
Run on local
RAILS_ENV=production bundle exec rake assets:precompile
Next git add .
Next got commit -m"assets precompile"
Next git push origin yourBranchName
Deploy on heroku and you are done
You need to commit your changes first.
Then execute git push heroku yourbranch:master

How can one clean old Rails assets when they are being precompiled locally and added to repository?

I use Capistrano for deploying my Rails 4 app. Precompiling assets on the server is very slow so I manually precompile it in development using RAILS_ENV=production bundle exec rake assets:precompile and add it to my git repo.
The problem I am facing is that the assets:precompile task does not remove the old assets. So, I have multiple copies of application-<manifest code>.css and application-<manifest-code>.js in my public assets.
Capistrano has deploy:assets:clean_expired task for this though I am not sure how can I replicate it in my case. I have seen other approaches which use capistrano task to precompile assets and rsync them up. This link - http://keighl.com/post/fast-rails-assets-precompile-capistrano/ - has an approach for Rails 3 using the turbo-sprockets-rails3 gem.
There are similar questions here - Do I have to run rake assets:clean before rake assets:precompile? and Confusion about rake assets:clean / cleanup on the asset pipeline in rails
How can I adapt it for Rails 4?
This is working well for me now - https://gist.github.com/mishrarohit/7802260 It uses git revision history to check if any changes have been made to assets. It will be better if we can use the manifest for this.

"application.css isn't precompiled" error on Heroku with remote assets on CDN

I'm getting the typical error on Heroku, that it appears the solution is to precompile locally and then commit to git.
ActionView::Template::Error (application.css isn't precompiled)
However I have my assets on Rackspace CloudFiles CDN using asset_sync and they are compiled fine, so I don't really want to also have to commit the compiled assets to git.
Any solution?
before you push your app to heroku run the following.
bundle exec rake assets:precompile
That will precompile all the necessary javascript and css.
If you are running the app on your computer after you precompile you will not see any css and js changes until you run.
bundle exec rake assets:clean
that will remove all precompiled files, you need to precompile every time you push your app.

Confusion about rake assets:clean / cleanup on the asset pipeline in rails

Could somebody explain to me what the command rake assets:clean really does? Unfortunately the Rails Guides dont mention it. There is also the command rake assets:cleanup. Whats the difference?
Furthermore could somebody tell me when do I have to run rake assets:precompile in production. Do I run it on the server console after I deployed all my application files to my production server? Or do I precompile on my local machine and then do a deploy of all files?
Thanks all
Note: This answer is rails 3 specific. For rails 4 and later, look at other answers here.
If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.
But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.
In my projects assets are precompiled as a part of deployment. Capistrano makes it very easy.
Also, I never heard of rake assets:cleanup.
Run rake assets:clobber to actually clean the assets.
http://www.dixis.com/?p=735
Sergio's answer was completely correct in Rails 3. rake assets:clean deleted all assets that had been previously precompiled into the public/assets directory.
In Rails 4, you run rake assets:clobber to do the same thing.
If you run rake assets:precompile with the following config (by default turned on in staging and production):
# config/environments/production.rb
config.assets.digest = true
You compiled assets get timestamped. This means you can compile your new assets while leaving the old assets in place. You usually want to do this in production so you website will still access the old files while your running precompile to create your new files (because you've added new css/javascript). You now want to get rid of the old files that are no longer in use. The clean it removes the old versions of the precompiled assets while leaving the new assets in place.
rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.
cap deploy:clean removes old releases, generally from a remote server. It is not rake assets:clean
rake != cap
rake assets:clean is now run by cap deploy:cleanup_assets. Add require 'capistrano/rails/assets' to your Capfile and you get this cap-task. My capistrano version is v3.2.1.
clean up those untracked files with git clean -f for files and git clean -f -d for directories

Resources