kikiki.blue
kikiki.blue

Reputation: 541

How can I clear rails cache after deploy to heroku?

I applied cache to my heroku rails app and it works well. But everytime I deploy to heroku, I also want to clear cache automatically.

so I googled and I found this.

task :after_deploy, :env, :branch do |t, args|
  puts "Deployment Complete"
  puts "Cleaning all cache...."
  FileUtils.cd Rails.root do
    sh %{heroku run console}
    sh %{Rails.cache.clear}
  end
end

but when I raked this script, it just show the heroku console command line but the Rails.cache.clear command does not typed. (I guess that is because heroku console is interactive)

and

system "heroku console Rails.cache.clear"

doesn't work for cedar apps.

How can I solve this problem?

Thanks.

Upvotes: 38

Views: 48031

Answers (8)

lobati
lobati

Reputation: 10215

Heroku has a release phase, which means you can put any deploy-time tasks inside your Procfile:

release: bundle exec rake tmp:clear

You might want to set up your own custom rake task to encapsulate any other tasks that need to happen at deploy, e.g.: db:migrate and so forth. Keep in mind that if the task fails for any reason, Heroku will halt the deploy and the previously deployed version of the code will remain active.

Upvotes: 0

rvirani
rvirani

Reputation: 59

If you want to simply run a rake task after deploy, I would recommend checking out:

https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

We've been using it in production for over 6 months and it's been rock solid.

First, add the buildpack AFTER you already have the Ruby buildpack set. This should happen after your first deploy to the server

heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

Second, set an environment variable called DEPLOY_TASKS with just the names of the rake tasks you want to run separated by spaces.

heroku config:set DEPLOY_TASKS='cache:clear

Upvotes: 5

Jonathan Mui
Jonathan Mui

Reputation: 2511

Heroku doesn't currently support a pipeline of actions to occur after deployment. You'll need something like Codeship or TravisCI to create a recipe of steps that happen during deployment

Disclosure: I am a customer of Codeship.

Upvotes: 0

Alexis Rabago Carvajal
Alexis Rabago Carvajal

Reputation: 375

heroku run rake tmp:cache:clear

http://guides.rubyonrails.org/command_line.html#tmp

Upvotes: 0

nthj
nthj

Reputation: 445

Ruby on Rails has a magical ENV variable called 'RAILS_CACHE_ID'. I set this to the git commit id whenever I deploy: heroku config:set RAILS_CACHE_ID=$CIRCLE_SHA1

Upvotes: 15

gabeodess
gabeodess

Reputation: 2222

Rails has a built in rake task:

rake tmp:clear

Upvotes: 43

Ivar
Ivar

Reputation: 5722

You should be able to create a cache clearing rake task that looks something like this:

namespace :cache do
  desc "Clears Rails cache"
  task :clear => :environment do
    Rails.cache.clear
  end
end

and invoke it directly in one command that you can use in your post deploy hook - like so:

heroku run rake cache:clear

Upvotes: 32

a.ross.cohen
a.ross.cohen

Reputation: 419

The following should work on cedar:

heroku run console

..then wait 5 seconds for heroku console to boot

Rails.cache.clear

Then you should see the cache clear, and you can quit console. Remember you might have to refresh a few times because your local machine will often cache assets and such in your browser until it makes a fresh request.

If it happens to be assets that you're caching though, you don't need to go through a manual clear every time you push, you just need to have the asset pipeline set up and make sure all your js/css(less/sass)/static images are being compiled with hashes at the end of their filenames.

Upvotes: 41

Related Questions