Published on

Creating a json version file during capistrano deployment


To get some more insight into the different code applications deployed to different servers (e.g. staging, acceptance production) I wanted to be able to see the version hash and deploy date in a small file that gets deployed with the application.

I found out about the version routine in the capistrano-helpers repository by @westarete, which prints the currently deployed git hash to a VERSION file. I've adjusted the code slightly to create a nice json formatted file which includes both the git hash and the deploydate.

# insert this code snippet in your deploy.rb capistrano config

namespace :deploy do
desc "write version number to app-readable file"
task :write_versionfile, :roles => :app do
versionobject = {
:version => "#{capture("cd #{release_path} && git describe --always")}",
:deploydate =>
versionobjectjson = versionobject.to_json.gsub(/\\n/,"")
run "echo '#{versionobjectjson}' > #{release_path}/web/VERSION"

desc "setup the cache folder to create folders and directories"
task :setup_group do
run "chown -R :#{group} #{deploy_to} && chmod -R g+s #{deploy_to}"

I use the resulting version file to remotely retrieve the servers version for our status dashboard, which is pretty neat and gives you a quick overview of your servers statuses.