Capistrano 2.0 With Git Shared Repository

As a follow-up to my previous article, Capistrano with Git shared repository, I will now post an updated version of my Git SCM module that works with Capistrano 2.0. As with all modules that worked with 1.x, modifications are necessary to support 2.0.

Parts of this plugin borrowed from Scott Chacon’s version, which I found on the Capistrano mailing list but failed to be able to get working. I’d rather eat my own dog food anyway.

Features of this plugin include:

  • Very simple, only requiring 2 lines in your deploy.rb.
  • Can deploy different branches, tags, or any SHA1 easily.
  • Supports prompting for password / passphrase upon checkout.
    (I am amazed at how some plugins don’t do this)
  • Supports :scm_command, :scm_password, :scm_passphrase Capistrano directives.

Install like so:

1
2
3
cd $GEMPATH/capistrano-2.0.0/lib/capistrano/recipes/deploy/scm/
wget http://scie.nti.st/dist/git-2.0.rb
mv git-2.0.rb git.rb
Replace $GEMPATH with the path to where your Gems are installed. Also change “capistrano-2.0.0” to whatever 2.x version you have.

The head of the module includes full documentation on requirements and configuration. The minimum you need is to place the following two lines in your config/deploy.rb:

1
2
set :scm, :git
set :repository, "someuser@somehost:/home/myproject"

Refer to the head of the module for further configuration options.

Here’s the full code:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'capistrano/recipes/deploy/scm/base'

module Capistrano
  module Deploy
    module SCM

      # An SCM module for using Git as your source control tool with Capistrano
      # 2.0.  If you are using Capistrano 1.x, use this plugin instead:
      #
      #   http://scie.nti.st/2007/3/16/capistrano-with-git-shared-repository
      #
      # Assumes you are using a shared Git repository. 
      #
      # Parts of this plugin borrowed from Scott Chacon's version, which I
      # found on the Capistrano mailing list but failed to be able to get
      # working.
      #
      # FEATURES:
      #
      #   * Very simple, only requiring 2 lines in your deploy.rb.
      #   * Can deploy different branches, tags, or any SHA1 easily.
      #   * Supports prompting for password / passphrase upon checkout. 
      #     (I am amazed at how some plugins don't do this)
      #   * Supports :scm_command, :scm_password, :scm_passphrase Capistrano
      #     directives.
      #
      # REQUIREMENTS
      # ------------
      #
      # Git is required to be installed on your remote machine(s), because a
      # clone and checkout is done to get the code up there.  This is the way
      # I prefer to deploy; there is no alternative to this, so :deploy_via
      # is ignored.
      #
      # INSTALLATION
      # ------------
      #
      # Place this file in:
      #
      #   $GEMPATH/capistrano-2.x.x/lib/capistrano/recipes/deploy/scm/
      #
      # CONFIGURATION
      # -------------
      #
      # Use this plugin by adding the following line in your config/deploy.rb:
      #
      #   set :scm, :git
      #
      # Set <tt>:repository</tt> to the path of your Git repo:
      #
      #   set :repository, "someuser@somehost:/home/myproject"
      #
      # The above two options are required to be set, the ones below are
      # optional.
      #
      # You may set <tt>:branch</tt>, which is the reference to the branch, tag, 
      # or any SHA1 you are deploying, for example:
      # 
      #   set :branch, "origin/master"
      #
      # Otherwise, HEAD is assumed.  I strongly suggest you set this.  HEAD is
      # not always the best assumption.
      #
      # The <tt>:scm_command</tt> configuration variable, if specified, will
      # be used as the full path to the git executable on the *remote* machine:
      #
      #   set :scm_command, "/opt/local/bin/git"
      #
      # For compatibility with deploy scripts that may have used the 1.x
      # version of this plugin before upgrading, <tt>:git</tt> is still
      # recognized as an alias for :scm_command.
      #
      # Set <tt>:scm_password</tt> to the password needed to clone your repo
      # if you don't have password-less (public key) entry:
      #
      #   set :scm_password, "my_secret'
      #
      # Otherwise, you will be prompted for a password.
      #
      # <tt>:scm_passphrase</tt> is also supported.

      class Git < Base
        # Sets the default command name for this SCM on your *local* machine.
        # Users may override this by setting the :scm_command variable.
        default_command "git"

        # When referencing "head", use the branch we want to deploy or, by
        # default, Git's reference of HEAD (the latest changeset in the default
        # branch, usually called "master").
        def head
          configuration[:branch] || 'HEAD'
        end

        # Performs a clone on the remote machine, then checkout on the branch
        # you want to deploy.
        def checkout(revision, destination)
          git      = command

          branch   = head

          fail "No branch specified, use for example 'set :branch, \"origin/master\"' in your deploy.rb" unless branch

          execute  = "#{git} clone #{configuration[:repository]} #{destination} && "
          execute += "cd #{destination} && #{git} checkout -b deploy #{branch}"

          execute
        end

        # Returns a string of diffs between two revisions
        def diff(from, to=nil)
          from << "..#{to}" if to
          scm :diff, from
        end

        # Returns a log of changes between the two revisions (inclusive).
        def log(from, to=nil)
          from << "..#{to}" if to
          scm :log, from
        end

        # Getting the actual commit id, in case we were passed a tag
        # or partial sha or something - it will return the sha if you pass a sha, too
        def query_revision(revision)
          yield(scm('rev-parse', revision)).chomp
        end

        def command
          # For backwards compatibility with 1.x version of this module
          configuration[:git] || super
        end

        # Determines what the response should be for a particular bit of text
        # from the SCM. Password prompts, connection requests, passphrases,
        # etc. are handled here.
        def handle_data(state, stream, text)
          logger.info "[#{stream}] #{text}"
          case text
          when /\bpassword.*:/i
            # git is prompting for a password
            unless pass = configuration[:scm_password]
              pass = Capistrano::CLI.password_prompt
            end
            "#{pass}\n"
          when %r{\(yes/no\)}
            # git is asking whether or not to connect
            "yes\n"
          when /passphrase/i
            # git is asking for the passphrase for the user's key
            unless pass = configuration[:scm_passphrase]
              pass = Capistrano::CLI.password_prompt
            end
            "#{pass}\n"
          when /accept \(t\)emporarily/
            # git is asking whether to accept the certificate
            "t\n"
          end
        end
      end
    end
  end
end

Enjoy!

Next I will tackle a Git plugin for Vlad the Deployer. Hit the green subscribe link at the top of this page to stay tuned!

Comments, suggestions, bugs? What did I miss? Please leave a comment.

Update (09-23-07): I’ve just modified this module to correct my use of :scm_command (it is for the *remote* machine, not local). Additionally, an error in the log() method was fixed. Anyone who downloads the module as instructed above will now get the new version. I feel confident this is the 1.0 version and as such I’ve submitted a patch to Capistrano: http://dev.rubyonrails.org/ticket/9635

If this module works for you, please visit the ticket and leave a comment that says “+1”. This shows the core team that people are verifying the patch works, and before long, it should be included with Capistrano.

Comments