I have a Rails 3.2.3 app that I host on github .Today I deployed it on Heroku.
During deployment, I made some changes to the Gemfile.
Run rake assets:precompile which generate a /public/assets folder.
I had to add and commit those change to local repository in order to
push to heroku by running git push heroku master
The deoployment was fine but now my development environment is like:
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
Those are the changes I made for deployment on heroku (run precompile and some other stuffs) that I don't want to exist on my github repo. How do I do to continue my development (be able to push on github) and keep updating the change on heroku) ?
Clarification: public/assets is generated and should be added to local repo in order to push to heroku. So I can't put it in .gitignore But I don't want it to be pushed on github. What is the best practice?
I'm not sure why you're running rake assets:precompile in development. Are you debugging something? In general, it's best not to commit compiled assets to your repo since they are generated content.
On Heroku, you have the option to precompile your assets during slug compilation. This makes for a cleaner repo albeit a slightly slower deploy. Just remove public/assets so Heroku knows what to do. Also, make sure you add the following line to config/application.rb:
config.assets.initialize_on_precompile = false
https://devcenter.heroku.com/articles/rails3x-asset-pipeline-cedar
Related
I built a Rails 6 app and deployed it to Heroku. But any changes I make to the stylesheet are not reflected. All the Heroku documentation and SO questions/answers appear to no longer be relevant to the current Rails setup in this regard. I could precompile the assets before pushing to heroku but I'd prefer not to. And actually I did find a "solution" but it feels more like a hack than a real solution. If I open config/initializers/assets.rb and change the statement:
Rails.application.config.assets.version = '1.0'
to
Rails.application.config.assets.version = '1.1'
then it will update the assets. But that means if I am experimenting with the look of the site I would be changing the version all the time. I mean if that's they way it's supposed to work I'll live with it, but it doesn't seem right. Anyone know a way to get Heroku to just update it on every push?
You've to compile the assets from the heroku end(i.e. in your server on Heroku), so after pushing your code to Heroku, run:
$ heroku run rake assets:precompile
# If above doesn't work
$ heroku run RAILS_ENV=production rake assets:precompile
Also, it's a good practice to add public/assets(or whatever folder that precompiles to) folder to your .gitignore file, so incase if you precompile assets in your local environment this will not mess up with your production env when you push to Heroku. But everytime you change some CSS and push to heroku, you'll need to precomile assets on Heroku end from the above command.
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 have a very small app that even uses sqlite3 in production because there are never going to be any issues with multiple writes, etc. I want to use capistrano to quickly and painlessly deploy updates to this app. But when I run cap production deploy it dumps the entire app into a release folder and symlinks it to current. I know I can include the production.sqlite3 file in the deploy.rb to keep the data but it still seems overkill to clone the entire repo every time I want to push an update.
I couldn't find anything in the capistrano documentation for updates.
Essentially all I need cap to do is
make sure my local git HEAD is the same as master
SSH into the prod server and do a git pull
Run rake db:migrate if necessary
Run rake assets:clean assets:precompile
Restart Phusion Passenger
How would I accomplish that?
Just write your own bash or ruby script that does this. I think you are missing the point of Capistrano. Cloning the whole repo allows you to do deploy rollbacks, leaving the previous version as is. It takes into account deploys that fail, and will not mess with your production site during the deploy process.
Every time I deploy my Rails 3.2 project to Heroku, rake assets:precompile is run:
$ git push heroku master
...
----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (189.17s)
...
Sometimes I want to make a push that I know does not change any assets, such as a quick hotfix to a controller. Is it possible to skip the asset:precompile step for a single git push to Heroku?
Thanks.
Sure! You'll need to create a manifest.yml in your_app/pubilc/assets directory.
The file can be blank. But ideally, you precompile everything locally, so deploys to Heroku would be much faster.
Make sure that you also committed the manifest.yml file when you're pushing to Heroku. Something like git add -f your_app/pubilc/assets/manifest.yml and a git push heroku master should suffice.
This worked for me. manifest.yml did nothing for me on my rails 4 project.
https://gist.github.com/Geesu/d0b58488cfae51f361c6
Just precompile locally with rake assets: precompile, check in the resulting assets that are in public/assets, and push to heroku.
This will automatically create the manifest-.yml or json file in your public/assets directory; then heroku will detect that and report Detected manifest file, assuming assets were compiled locally.
Note 1: Some people have a line in development.rb that makes these go to public/dev-assets instead; if so, you need to rename dev-assets to just assets)
Note 2: Make sure your .gitignore file is not excluding the public/assets directory.
In rails 4, create the file manifest-<md5 hash>.json instead of manifest.yml
When I try to access my site, then check my Heroku logs, I see this error:
ActionView::Template::Error (couldn't find file 'reset'
2012-06-13T02:31:43+00:00 app[web.1]: (in
/app/app/assets/stylesheets/application.css:4)):
(application.css contains the line *= require reset)
Then I thought to run "heroku run bundle exec rake assets:precompile:all" but this gives a similar error:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
couldn't find file 'main/first.js.coffee'
(in /tmp/build_3428u21sggsoc/app/assets/javascripts/application.js:1)
Tasks: TOP => assets:precompile:primary
(That file is the first one required from my application.js, which has first line "//= require main/first.js.coffee")
In summary: my application runs fine locally, but when I deploy to Heroku, the files can no longer be found. Any ideas why?
Edit: here is the project tree. (There is one more directory before the app one, and that is the main project directory that also contains config, db, log, etc)
Another edit: there is no problem with .gitignore, or .slugignore.
At first, I would suggest you to run your application in production mode on your local computer. There are some errors (in assets but I also found some in routing) which can have impact only for production environment so you can test and fix them locally instead of having to do it from the production server.
About the asset precompilation on Heroku, the solution given by akjoe should result in compiled assets tracked in git repo : with this option, you should disable the asset precompilation which happen on Heroku and let Rails serve you assets (set config.serve_static_assets = true in your production.rb file) but this is not the best way to deal with the asset pipeline as you lost one of his major benefice which is freeing your rails application of request for asset.
To make it working properly, you should setup something like heroku explain : Using Rack::Cache with Memcached for Static Asset Caching in Rails 3.1+
I would also suggest you to try the assets precompilation locally in production environment RAILS_ENV=production bundle exec rake assets:precompile. To see if you got any error.
Finally you may want to check this different links to find useful information :
Rails 3.1+ Asset Pipeline on Heroku Cedar
Railscasts : #279 Understanding the Asset Pipeline
Rails Guide
I've had almost exactly the same problem and similar errors with stylesheets edits not taking effect... I found that I would edit css (or as in your case references to css files) which seemed to be ignored by Heroku. Turns out Heroku was ONLY referencing the stylesheets in the public/assets directory. I cleared this directory and was able to get it working.
I later found that you need to precompile your assets directory BEFORE you checkin to git. You would do this as follows:
Precompile assets directory: rake assets:precompile
Add the project files to the current Git repository: git add .
Checkin the file changes to the current Git repository: git commit
-am "description goes here"
Push the files to Heroku: git push heroku master (substitute
'master' for the branch you wish to push to Heroku).
Hope that helps!