Class: Minestrone::Deploy::SCM::Git

Inherits:
Base
  • Object
show all
Defined in:
lib/minestrone/recipes/deploy/scm/git.rb

Overview

An SCM module for using Git as your source control tool.

Assumes you are using a shared Git repository.

Parts of this plugin borrowed from Scott Chacon’s version, which I found on the Minestrone 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_passphrase Minestrone directives.

CONFIGURATION


Use this plugin by adding the following line in your config/deploy.rb:

set :scm, :git

Set :repository 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 :branch, which is the reference to the branch, tag, or any SHA1 you are deploying, for example:

set :branch, "master"

Otherwise, HEAD is assumed. I strongly suggest you set this. HEAD is not always the best assumption.

You may also set :remote, which will be used as a name for remote tracking of repositories. This option is intended for use with the :remote_cache strategy in a distributed git environment.

For example in the projects config/deploy.rb:

set :repository, "#{scm_user}@somehost:~/projects/project.git"
set :remote, "#{scm_user}"

Then each person with deploy priveledges can add the following to their local ~/.caprc file:

set :scm_user, 'someuser'

Now any time a person deploys the project, their repository will be setup as a remote git repository within the cached repository.

The :scm_command 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"

:scm_passphrase is supported for the public key passphrase.

The remote cache strategy is also supported.

set :repository_cache, "git_master"
set :deploy_via, :remote_cache

For faster clone, you can also use shallow cloning. This will set the ‘–depth’ flag using the depth specified. This cannot be used together with the :remote_cache strategy

set :git_shallow_clone, 1

To deploy from a local repository:

set :repository, "file://."
set :deploy_via, :copy

AUTHORS


Garry Dolley scie.nti.st Contributions by Geoffrey Grosenbach topfunky.com

Scott Chacon http://jointheconversation.org
            Alex Arnell http://twologic.com
                     and Phillip Goldenburg

Instance Attribute Summary

Attributes inherited from Base

#configuration

Instance Method Summary collapse

Methods inherited from Base

#command, default_command, #initialize, #local, #local?, #next_revision, #scm

Constructor Details

This class inherits a constructor from Minestrone::Deploy::SCM::Base

Instance Method Details

#checkout(revision, destination) ⇒ Object

Performs a clone on the remote machine, then checkout on the branch you want to deploy.



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
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 114

def checkout(revision, destination)
  git    = command
  remote = origin

  args = []

  # Add an option for the branch name so :git_shallow_clone works with branches
  args << "-b #{variable(:branch)}" unless variable(:branch).nil? || variable(:branch) == revision
  args << "-o #{remote}" unless remote == 'origin'

  if depth = variable(:git_shallow_clone)
    args << "--depth #{depth}"
  end

  execute = []
  execute << "#{git} clone #{verbose} #{args.join(' ')} #{variable(:repository)} #{destination}"

  # checkout into a local branch rather than a detached HEAD
  execute << "cd #{destination} && #{git} checkout #{verbose} -b deploy #{revision}"

  if variable(:git_enable_submodules)
    execute << "#{git} submodule #{verbose} init"
    execute << "#{git} submodule #{verbose} sync"

    if variable(:git_submodules_recursive) == false
      execute << "#{git} submodule #{verbose} update --init"
    else
      execute << %Q(export GIT_RECURSIVE=$([ ! "`#{git} --version`" \\< "git version 1.6.5" ] && echo --recursive))
      execute << "#{git} submodule #{verbose} update --init $GIT_RECURSIVE"
    end
  end

  execute.compact.join(" && ").gsub(/\s+/, ' ')
end

#diff(from, to = nil) ⇒ Object

Returns a string of diffs between two revisions



199
200
201
202
203
204
205
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 199

def diff(from, to = nil)
  if to
    scm :diff, "#{from}..#{to}"
  else
    scm :diff, from
  end
end

#export(revision, destination) ⇒ Object

An expensive export. Performs a checkout as above, then removes the repo.



151
152
153
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 151

def export(revision, destination)
  checkout(revision, destination) << " && rm -Rf #{destination}/.git"
end

#handle_data(state, stream, text) ⇒ Object

Determines what the response should be for a particular bit of text from the SCM. Password prompts, connection requests, passphrases, etc. are handled here.



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 251

def handle_data(state, stream, text)
  host = state[:channel][:host]
  logger.info "[#{host} :: #{stream}] #{text}"
  case text
  when /\bpassword.*:/i
    # git is prompting for a password
    pass = Minestrone::CLI.password_prompt
    %("#{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 = variable(:scm_passphrase)
      pass = Minestrone::CLI.password_prompt
    end
    %("#{pass}"\n)
  when /accept \(t\)emporarily/
    # git is asking whether to accept the certificate
    "t\n"
  end
end

#headObject

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”).



104
105
106
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 104

def head
  variable(:branch) || 'HEAD'
end

#log(from, to = nil) ⇒ Object

Returns a log of changes between the two revisions (inclusive).



208
209
210
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 208

def log(from, to = nil)
  scm :log, "#{from}..#{to}"
end

#originObject



108
109
110
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 108

def origin
  variable(:remote) || 'origin'
end

#query_revision(revision) ⇒ Object

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

Raises:

  • (ArgumentError)


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 214

def query_revision(revision)
  raise ArgumentError, "Deploying remote branches is no longer supported.  Specify the remote branch as a local branch for the git repository you're deploying from (ie: '#{revision.gsub('origin/', '')}' rather than '#{revision}')." if revision =~ /^origin\//

  return revision if revision =~ /^[0-9a-f]{40}$/

  command = scm('ls-remote', repository, revision)
  result = yield(command)
  revdata = result.split(/[\t\n]/)
  newrev = nil

  revdata.each_slice(2) do |refs|
    rev, ref = *refs
    if ref.sub(/refs\/.*?\//, '').strip == revision.to_s
      newrev = rev
      break
    end
  end

  return newrev if newrev =~ /^[0-9a-f]{40}$/

  # If sha is not found on remote, try expanding from local repository
  command = scm('rev-parse --revs-only', origin + '/' + revision)
  newrev = yield(command).to_s.strip

  # fallback for expected legacy default functionality
  unless newrev =~ /^[0-9a-f]{40}$/
    command = scm('rev-parse --revs-only', revision)
    newrev = yield(command).to_s.strip
  end

  raise "Unable to resolve revision for '#{revision}' on repository '#{repository}'." unless newrev =~ /^[0-9a-f]{40}$/
  return newrev
end

#sync(revision, destination) ⇒ Object

Merges the changes to ‘head’ since the last fetch, for remote_cache deployment strategy



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/minestrone/recipes/deploy/scm/git.rb', line 157

def sync(revision, destination)
  git     = command
  remote  = origin

  execute = []
  execute << "cd #{destination}"

  # Use git-config to setup a remote tracking branches. Could use
  # git-remote but it complains when a remote of the same name already
  # exists, git-config will just silenty overwrite the setting every
  # time. This could cause wierd-ness in the remote cache if the url
  # changes between calls, but as long as the repositories are all
  # based from each other it should still work fine.

  if remote != 'origin'
    execute << "#{git} config remote.#{remote}.url #{variable(:repository)}"
    execute << "#{git} config remote.#{remote}.fetch +refs/heads/*:refs/remotes/#{remote}/*"
  end

  # since we're in a local branch already, just reset to specified revision rather than merge
  execute << "#{git} fetch #{verbose} #{remote} && #{git} fetch --tags #{verbose} #{remote} && #{git} reset #{verbose} --hard #{revision}"

  if variable(:git_enable_submodules)
    execute << "#{git} submodule #{verbose} init"
    execute << "#{git} submodule #{verbose} sync"

    if variable(:git_submodules_recursive) == false
      execute << "#{git} submodule #{verbose} update --init"
    else
      execute << %Q(export GIT_RECURSIVE=$([ ! "`#{git} --version`" \\< "git version 1.6.5" ] && echo --recursive))
      execute << "#{git} submodule #{verbose} update --init $GIT_RECURSIVE"
    end
  end

  # Make sure there's nothing else lying around in the repository (for
  # example, a submodule that has subsequently been removed).
  execute << "#{git} clean #{verbose} -d -x -f"

  execute.join(" && ")
end