Last deployment date with Rails and Capistrano

When you are developing an app or a website for your client, it is pretty common to setup a sandbox environment where your client can test and see the application while it is still in development. One thing I find useful is to display the last deployment date on the home page.
Here is a quick and easy way to automate the process :
Step #1 (we can have lots of fun)
I will consider that you use capistrano for deploying your app. The only thing you have to do in your capistrano recipe is to “touch” a dummy file to update its modification date.

namespace :deploy do
  desc 'Restart My App'
  task :restart, :roles => :app do
    run "touch #{current_path}/last_deploy"
  end
end

Step #2 (there’s so much we can do)
Now in application_controller, add a before_filter like this

before_filter :last_deploy
def last_deploy
  @last_deploy = File.new("last_deploy").atime rescue Time.now
end

Step #3 (it’s just you and me)
Display the date in the layout or in the view of your choice

<%= "Last deployment : #{@last_deploy}"%>

Edit : Oh, dear readers, I just want to let you know that RubyFleebie is now on Twitter.

8 thoughts on “Last deployment date with Rails and Capistrano

  1. I think you could also use the REVISION file that capistrano places by default during a deploy. You could also get the SVN/git revision from this file by reading it.
    Thanks for the tip!

  2. You could also pull the timestamp from the most recent folder within the releases directory, as Capistrano uses timestamped folder names as a way of separating revisions of code over different deploys. Nice idea 🙂

  3. When using passenger, why not check for the last change on tmp/restart.tmp?
    File.atime(“#{Rails.root}/tmp/restart.txt”).strftime(“%Y-%m-%d at %H:%M”)
    will work with just this single line 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *