Mascote

notes to self.

Task to Show Deployed Commits, Revisions and Branch With Capistrano 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
desc "Show the difference between deployed revisions on server."
task :diff_report do
  on roles(:app) do
    current, previous, branch = fetch(:current_revision), fetch(:previous_revision), fetch(:branch)
    puts "\n" << "-"*63
    puts "===== [ \033[1;36m#{fetch(:application).capitalize} - #{fetch(:stage).capitalize}\033[0m ]"
    puts "=== Deployed Branch: \033[1;32m#{branch}\033[0m"
    puts "=== Deployed Revision: \033[1;32m#{current}\033[0m"
    puts "=== Previous Revision: \033[1;32m#{previous}\033[0m\n\n"

    # If deployed master branch, show the difference between the last 2 deployments
    # or show the difference between master and the deployed branch.
    base_rev, new_rev = branch != "master" ? ["master", branch] : [previous, current]

    # Show difference between master and deployed revisions.
    if (diff = `git log #{base_rev}..#{new_rev} --oneline`) != ""
      # Colorize refs
      diff.gsub!(/^([a-f0-9]+) /, "\033[1;32m\\1\033[0m - ")
      diff = "    " << diff.gsub("\n", "\n    ") << "\n"
      # Indent commit messages nicely, max 80 chars per line, line has to end with space.
      diff = diff.split("\n").map{|l|l.scan(/.{1,120}/).join("\n"<<" "*14).gsub(/([^ ]*)\n {14}/m,"\n"<<" "*14<<"\\1")}.join("\n")
      puts "=== Difference between current revision and deployed revision:\n\n"
      puts diff
    else
      puts "=== Deployed the last revision again.\n\n"
    end
  end
end

after :log_revision, :diff_report

Kudos for @ndbroadbent for the original post.