heroku / docker-ruby

Ruby image for the heroku-docker project
17 stars 30 forks source link

Conditionally make assets... #4

Open jmervine opened 9 years ago

jmervine commented 9 years ago

Not 100% sure this will work but I think it's...

# How to conditionally `rake assets:precompile`?
ONBUILD RUN bundle exec rake assets:precompile || true
jmervine commented 9 years ago

That's pretty hacky though.

hone commented 9 years ago

Yeah, if it fails it should break building if you expect it to run.

jmervine commented 9 years ago

Right, so you need to grep the output and return true if there's no task and false if there is... Ugly.

gwmoura commented 9 years ago

guys, I test this:

ONBUILD RUN if [ -f /app/user/Rakefile ]; then bundle exec rake assets:precompile; fi

and work for me. I tested using an simple script with Gemfile without rails or sinatra, only ruby

jmervine commented 9 years ago

@gwmoura I can see quite a few use-cases were an app would have a Rakefile without assets:precompile... how about...

ONBUILD RUN test "$(bundle exec rake -T | grep assets:precompile)" && bundle exec rake assets:precompile
gwmoura commented 9 years ago

@jmervine yes, can be, in my case, I execute a ruby script only, no Rails. Your solution not work for me :(

jmervine commented 9 years ago

@gwmoura Merging the two, you could do something like (untested):

ONBULD RUN test -f /app/user/Rakefile && (test "$(bundle exec rake -T | grep assets:precompile)" && bundle exec rake assets:precompile)
gwmoura commented 9 years ago

@jmervine I tested your script and this worked fine for me. @hone if you want, you can add on Dockerfile.

I tested locally, but I will add in my image based on heroku/ruby

Look my results:

$ ls
app.rb  docker-compose.yml  Dockerfile  Gemfile  Gemfile.lock  run.sh  vendor

$ test -f Rakefile && (test "$(bundle exec rake -T | grep assets:precompile)" && bundle exec rake assets:precompile)

And in a rails app too not work

$ ls
app  config     db       Gemfile.lock  log     Rakefile     test  vendor
bin  config.ru  Gemfile  lib           public  README.rdoc  tmp

$ test -f Rakefile && (test "$(bundle exec rake -T | grep assets:precompile)" && bundle exec rake assets:precompile)
I, [2015-10-01T18:41:24.746492 #2063]  INFO -- : Writing /home/george/workspace/tests/ruby/railsblocks/myproject/public/assets/application-d4d603d0ff049a3ecfbf019b468bf1a62a50b9fdafe41213c300fa071342b600.js
I, [2015-10-01T18:41:24.749187 #2063]  INFO -- : Writing /home/george/workspace/tests/ruby/railsblocks/myproject/public/assets/application-e80e8f2318043e8af94dddc2adad5a4f09739a8ebb323b3ab31cd71d45fd9113.css
3den commented 8 years ago

why cant we just remove the assets:precompile from this Docker file and have users add it if needed?

3den commented 8 years ago

even with the suggested fix it would not work for my team because we need to run bower install before the assets:precompile.

jmervine commented 8 years ago

@3den Perhaps add bower install as a pre-req task to assets:precompile?

3den commented 8 years ago

@jmervine thanks for the reply, what is the best way to do that?

jmervine commented 8 years ago

@3den My rake-fu is not super strong, but the first section here talks about how to build dependency tasks. You'll have to build a custom task for the bower install part, I found an overly complicated example here, but you should be able to pull out the useful bits.

Because assets:precompile is built in to rails, or so I believe, you might have to do some googling around how to customize that task. It's probably not that difficult though.

Cheers!

Edit: This talks about how to do exactly what we're talking about here. I didn't vet it, but it should be a good start.