Get release information when deploying with Capistrano

Branch master (at 3183af7) deployed as release 20140811160455 by Matthew;

After a colleague asked if there was an easy way to see current release information from our deployed code I came across the following two methods, both of which have their own appeals.

In git enabled environments (e.g. where your stage's server runs the git client) it is possible to use rev-parse to print the SHA1 hash of a given release. This is particularly useful if you quickly want to jump to a commit in your own client.

execute "cd #{deploy_to}/repo && git rev-parse HEAD" 
# use --short if desired
# 3183af7

A second and more informative option is to make use of Capistrano's in-built revision log. See below the output of tac ${deploy_to}/revisions.log | head -n 5.

Branch master (at 3183af7) deployed as release 20140811160455 by Matthew;
Branch master (at 2234eef) deployed as release 20140810163353 by Matthew;
Matthew; rolled back to release 20140810152258
Branch master (at c5e70b9) deployed as release 20140810160725 by Matthew;
Branch master (at 977da81) deployed as release 20140810152258 by Matthew;

On each deploy Capistrano appends release information to your deployment directory. To make use of this in your application, we can pull the last release information into our path as part of the deployment routine.

execute "tac #{deploy_to}/revisions.log | sed -n 1p > #{release_path}/revision"

# Will write:
# Branch master (at 3183af7) deployed as release 20140811160455 by Matthew;

As a task:

after :finished, :head_revision_log do
    on roles(:app) do
        execute "tac #{deploy_to}/revisions.log | sed -n 1p > #{release_path}/revision"
    end
end
Posted on Jul 9
Written by Matthew Jewell