I recently added the turbo sprockets gem https://github.com/ndbroadbent/turbo-sprockets-rails3
to my rails application, I'm using capistrano to deploy to Amazon EC2.
I'm a little confused on how I can make this work.
assets:precompile worked on my local machine, but on the amazon instance it didn't.
Long story short capistrano makes a new release directory for each deployment and the public/assets
directory is empty, so every time it creates a new one and when running assets:precompile it precompiles all of the assets.
Should I precompile localy and add them to git or copy the public/assets directory from the last deployment before capistrano runs assets:precompile?
What would be the cleanest/best practice solution?
Or should I keep the compiled assets in a shared directory?
The simplest solution I could think of is using the shared/assets directory to store
my assets and make a symbolic link to the the release public/assets directory before the assets are being compiled .
task :assets_precompile do
run "ln -s #{shared_path}/assets #{release_path}/public/assets"
run "cd #{release_path} && RAILS_ENV=production bundle exec rake assets:precompile"
end
Edit: Anjan pointed out that if you use deploy:assets ( If you have load 'deploy/assets' in your Capfile ) this is done by default so that's a cleaner solution .
Lesce's answer has greater weight if the deploy process includes symlinking to static files.
Example: config/database.yml is something you don't tweak, nor want a deploy to necessarily change on you...
Having that in shared/assets will lead to failure when assets:precompile gets called.
It also works for cases with user loaded images, attachments...
Related
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.
Is there a situation where it is not necessary to run this command? i.e. I don't have static images in the assets directory
Heroku includes asset precompilation as a step in the deploy process. As I see you're using Heroku, that means that you don't have to run rake assets:precompile before committing or pushing. Just make sure that the code that needs to be preprocessed is included in the commit that you're pushing (i.e. the files in /app/assets/(javascripts|stylesheets) etc are up to date).
If you're only deploying to Heroku and working locally in the "development" environment, then you might consider adding /public/assets to your .gitignore and deleting that folder.
See the Heroku docs on this.
I precompiled assets on my dev environment (by mistake!) now any changes done on js/css files are not reflecting on browsing site locally.
I removed assets folder from public directory but then no css/js was available.
How do I get rid of this?
As a temporary solution I just cloned project into new directory and it works.
The question is: why do you need asset precompilation in the development environment? It's not meant to work like this.
The asset pipeline allows working in development with the uncompressed, unminified versions of your JS files. It also reloads them each time you refresh your browser, so you can develop your application with ease.
In production, though, the asset pipeline precompiles the JS files / assets you have into one single, minified file. This allows for better performance on the client as the files are smaller and are fetched in one single request.
So precompiling assets in development makes no sense at all.
In case if I understood you correctly, generally, when you precompile by default assets directory is being created inside public directory. To get your assets back, you can precompile again.
There is also a cache in tmp directory that you might consider removing.
Later you could use a $ (bundle exec) rake assets:precompile in combination with $ (bundle exec) rake assets:clean instead of $ rm -r public/assets so that new assets would be in effect rake way.
A one line command to look at new changes after your commits in environment would be
$ RAILS_ENV=(environment) rake assets:clean assets:precompile
but generally in development assets are not meant to be served as in production mode, so running previous with RAILS_ENV=production and starting a local server in production mode would be considered as a way to check (but not to make sure) if your assets would be served upon deployment in real production.
I went into the exact same problem and resolved it following #dashi's advice: 'remove directory assets in public, then start server in development mode.' everything is back to normal.
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
I was compiling my asset pipeline for my production environment and it did for all my environments. How can I uncompile my asset pipeline for my development environment?
I have checked my config/development environment and cannot find a fix.
Thanks in advance for any help...
To remove precompiled assets use:
rake assets:clean
What this basically does is remove the public/assets directory. You may need to include the RAILS_ENV variable if you need to run it for a certain environment.
Try using
rake assets:clobber
worked for me in rails 4
When you run the compile task locally (on your development machine) the assets are compiled in the Rails production environment, but are written to the public folder.
This means that even when you run in development mode it'll use the compiled assets instead of sending requests to the pipeline. This is normal behavor - requests only go to the pipeline if the file does not exists in public/assets.
The compile task should generally only be used when deploying, and on the remote (production) machine.
If you have compiled locally, you can delete all the files in the public/assets folder and development will behave as before. If you checked these files into source control you'll need to remove them.
Once removed things should work fine.
s
One final tip: if this is an upgraded app check your config settings against those in the last section of the Rails asset pipeline guide.
For Rails 5:
$ RAILS_ENV=development bin/rake assets:clobber