Automatically precompile assets before pushing to Heroku - ruby-on-rails

Is it possible to automatically precompile my assets in a Rails app before pushing out to Heroku? I always forget to do it, so it would be nice if when I typed git push heroku master, it would first run rake assets:precompile ; git commit add . ; git commit -a -m "Precompile.", or something to that effect.
Has anyone achieved such a solution? Possibly without hooks? Though I suspect that is the only way.

I finally figured this out. I was indeed on the Cedar stack. The problem was that I had checked my public directory into Git, and when I pushed to Heroku, it realized that public existed, and thus assumed I precompiled. Running git rm -r public and adding public/** to my .gitignore, then pushing, fixes the problem.

It sounds like you might not be on Heroku's Cedar Stack? If you're using the asset pipeline (Rails -v >= 3.1), cedar provides three options for compiling assets.
From the docs:
If you have not compiled assets locally, we will attempt to run the
assets:precompile task during slug compilation.

You could always alias heroku or something similar to rake assets:precompile ; git commit add . ; git commit -a -m "Precompile." ; git push heroku master in your bash profile
ie
#in ~/.bash_profile
alias precompile_push='rake assets:precompile ; git commit add . ; git commit -a -m "Precompile." ; git push heroku master'

On the cedar stack, it will do this during slug compilation. I recommend that.

I've created a gem that run as daemon and automatically pull changes from a Git repo, precompiles assets, commit and push back.
https://github.com/nectify/rails-precompile2git/

Related

Removing Precompiled Assets in Rails before Push to Heroku

I have a Heroku app on Rails 4.0.8.
At some point early on, I realized that my CSS and JS changes wouldn't push to Heroku unless I ran rake assets:precompile and committed the new asset files before pushing (git push heroku master).
I've been doing those asset precompiles for about 14 months now, and I've amassed over 48mb in what appear to be old precompiled assets. However, when I remove them from public/assets manually, my site loses all of its CSS and JS.
How do I remove the old precompiled assets and get Heroku to start compiling on push?
Follow these steps:
rake assets:clean
git add .
git commit -m commit_name
git push heroku branch_name
This will clean your assets locally + pushing on heroku would precompile the assets.
Try the following code.
rake assets:clean

How to undo bundle install --deployment?

I have been working on a rails app and been deploying regularly to heroku using local git depository. I accidentally ran the command:
bundle install --deployment
It seems it downloaded all the gems to the local folder, and now when I want to upload to heroku it is trying to upload many megabytes of gems... How do I undo the command I ran and delete the local gems? How do I prevent bundle install from downloading all gems again?
Wanted to make this an answer because it was jrochkind comment that helped
rm -rf vendor/bundle
The f in -rf ignores the questions asking if you want to really remove this file.
bundle install --no-deployment
The above will disable bundle deployment mode and install needed packages in a non production enviroment
git add .
git commit -m "fixed deployment bundle"
git push heroku master
The above will add all your lock file to git, create a commit with the new update. It will then send your fixed lock file to your master branch to Heroku master branch (It only has a master) along with any other changes you have done.
If you need to send a different branch to Heroku other than your master then instead of git push heroku master run the following code:
git push heroku development:master
The above command will push your development branch to the Heroku master branch. change development to the branch name you want to send to Heroku.
You may try this:
First, remove all your local gems.
rm -r vendor/bundle
Bundle again
bundle install
Run git add --all
Commit and push again to heroku.
Edit Bundler config file at your app root path .bundle/config and remove following line:
BUNDLE_DEPLOYMENT: "true"
Or with oneliner:
sed '/BUNDLE_DEPLOYMENT: "true"/d' .bundle/config

Why am I unable to push my app to Heroku?

I haven't been able to get Heroku to update my app for some reason. This is the first app I've had problems with this happening. I've tried a couple different methods, but everything I've tried just says in the command "everything is up to date." and when I check heroku, it's just an early version of my app. Any idea why it won't update on Heroku?
Here is what I've tried:
$ bundle exec rake test
$ git add -A
$ git commit -m "..."
$ git checkout master
$ git merge blah
$ bundle exec rake test
$ git push
$ git push heroku
$ heroku run rake db:migrate
and I've also tried this as well:
$ git status
$ git add .
$ git commit -m "..."
$ git push heroku master
$ heroku run db:migrate
Heres the git status:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Here's trying to force it
$ git push heroku master --force
Everything up-to-date
Here's with git remote -v
heroku https://git.heroku.com/stupidapp1.git (fetch)
heroku https://git.heroku.com/stupidapp1.git (push)
origin https://github.com/Tyrantt47/stupidapp1.git (fetch)
origin https://github.com/Tyrantt47/stupidapp1.git (push)
Curtis, I feel like this answer could be judged as the antithesis of a good dev, but if you have the code you want to deploy checked into version control and locally, why not just push it to a new Heroku app.
If the name of the repository is any indication of the scale of the application, I would say try it and see the results.
Heroku provides good documentation on migrating a database from one app to another and its add-on services are easy to get setup, if either one of those are a concern.
Food for thought...
I think that the syntax is
git push -f heroku master

Heroku not reflecting codes pushed to it and differs from Localhost

I have done up a web app with Ruby on Rails and made some changes on my codes in my computer. Thereafter, I did git add ., git commit, git push and git push heroku master.
The Localhost:3000 webpage reflected the codes and showed the change but on heroku, the webpage did not reflect the codes/change.
How do I resolve this?
did you precompile? Otherwise you won't see css changes.
RAILS_ENV=production bundle exec rake assets:precompile
Also see https://devcenter.heroku.com/articles/rails-asset-pipeline

Rails Asset Pipeline precompile, how to do it the right way?

I'm truely new on Rails 4.0. I don't understand the precompilation process when I'm pushing on heroku Cedar stack. I do this :
$> rake assests:precompile
$> git add .
$> git commit -m "foobar" && git push && git push heroku
All seems be working correctly, but my images/css don't seem to be "loaded" (Chrome) on the heroku app (but works perfectly locally) :
Failed to load resource: the server responded with a status of 404 (Not Found) http://xxxapp.herokuapp.com/stylesheets/bootstrap.min.css
I tried something, but I've heard that is was not the good solution. I have modified config/environments/production.rb file and changed :
config.assets.compile = false
to
config.assets.compile = true
It works fine, all my images and css are loaded, but I think I missed something.
Also
I tried :
$> rm -rf public/assets
$> git add .
$> git commit -m "foooo" && git push && git push heroku
The precompile process runs well but after, still no images and css loaded on my app.
After edited my Gemfile by adding :
group :production do
gem 'rails_12factor'
end
images are loaded well but no css..
Heroku should compile the assets for you by default.
You don't need to do anything else.
If you already compiled your assets, you may want to delete them from the public directory and then just push to heroku again.
You should precompile on heroku, not on your computer.
heroku run rake assets:precompile
I don't know, if this is the problem, I did this a year ago.

Resources