# Copyright 2013 Square Inc. # # Licensed under the Apache License, Version 2.0 (the “License”); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an “AS IS” BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

require 'active_support/core_ext/enumerable' require 'active_support/core_ext/object/try'

# Capistrano tasks for Rails apps using Squash.

set :_squash_current_revision, lambda {

rev = nil
on roles(:web), :in => :sequence, :limit => 1 do
  within repo_path do
    origin = capture("git ls-remote #{fetch(:repo_url)}").chomp.lines.map { |l| l.split(/\s+/) }.index_by(&:last)
    rev    = origin["refs/heads/#{fetch :branch}"].try(:first) || capture("git rev-parse #{fetch :branch}").chomp
  end
end
rev

}

namespace :squash do

# USAGE: before 'deploy:publishing', 'squash:write_revision'
desc "Writes a REVISION file to the application's root directory"
task :write_revision do
  # must be run in serial because the fetch for _squash_current_revision results
  # in a deadlock otherwise
  on roles(:app), :in => :sequence, :limit => 1 do
    execute %{echo "#{fetch :_squash_current_revision}" > #{release_path.join('REVISION')}}
  end
end

desc "Notifies Squash of a new deploy"
task :notify do
  on roles(:web), :limit => 1 do |host|
    within release_path do
      with rails_env: fetch(:rails_env) do
        execute :rake, "squash:notify[#{fetch(:current_revision)}]"
      end
    end
  end
end

end

after 'deploy:finishing', 'squash:notify'