Class: Capistrano::GitCopy::SCM

Inherits:
SCM::Plugin
  • Object
show all
Defined in:
lib/capistrano/git_copy/scm.rb

Overview

SCM plugin for capistrano uses a local clone and uploads a tar archive to the server

Instance Method Summary collapse

Instance Method Details

#archive_pathString

Path to archive

Returns:

  • (String)


151
152
153
# File 'lib/capistrano/git_copy/scm.rb', line 151

def archive_path
  @_archive_path ||= File.join(tmp_path, 'archive.tar.gz')
end

#checkObject

Check if repository is accessible

Returns:

  • void



32
33
34
# File 'lib/capistrano/git_copy/scm.rb', line 32

def check
  git(:'ls-remote --heads', repo_url)
end

#cleanupObject

Cleanup repo cache

Returns:

  • void



128
129
130
131
132
# File 'lib/capistrano/git_copy/scm.rb', line 128

def cleanup
  backend.execute(:rm, '-rf', tmp_path)

  backend.info('Local repo cache was removed')
end

#cloneObject

Clone repo to cache

Returns:

  • void



58
59
60
61
62
# File 'lib/capistrano/git_copy/scm.rb', line 58

def clone
  backend.execute(:mkdir, '-p', tmp_path)

  git(:clone, fetch(:repo_url), repo_cache_path)
end

#define_tasksObject

define plugin tasks



18
19
20
# File 'lib/capistrano/git_copy/scm.rb', line 18

def define_tasks
  eval_rakefile File.expand_path('../tasks/git_copy.rake', __FILE__)
end

#fetch_revisionObject

Set deployed revision

Returns:

  • void



121
122
123
# File 'lib/capistrano/git_copy/scm.rb', line 121

def fetch_revision
  backend.capture(:git, 'rev-list', '--max-count=1', '--abbrev-commit', commit_hash).strip
end

#prepare_releaseObject

Create tar archive

Returns:

  • void



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/capistrano/git_copy/scm.rb', line 91

def prepare_release
  if fetch(:upload_path) != '.'
    backend.execute(:tar, '-czf', archive_path, '-C', fetch(:upload_path), '.')
  elsif fetch(:with_submodules)
    backend.execute(git_archive_all_bin, "--prefix=''", archive_path)
  else
    git(:archive, '--format=tar', 'HEAD', '|', 'gzip', "> #{archive_path}")
  end

  exclude_files_from_archive if fetch(:git_excludes, []).count > 0
end

#register_hooksObject

register capistrano hooks



23
24
25
26
27
# File 'lib/capistrano/git_copy/scm.rb', line 23

def register_hooks
  after  'deploy:new_release_path',     'git_copy:create_release'
  before 'deploy:check',                'git_copy:check'
  before 'deploy:set_current_revision', 'git_copy:set_current_revision'
end

#releaseObject

Upload and extract release

Returns:

  • void



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/capistrano/git_copy/scm.rb', line 106

def release
  backend.execute :mkdir, '-p', release_path

  remote_archive_path = File.join(fetch(:deploy_to), File.basename(archive_path))

  backend.upload!(archive_path, remote_archive_path)

  backend.execute(:mkdir, '-p', release_path)
  backend.execute(:tar, '-f', remote_archive_path, '-x', '-C', release_path)
  backend.execute(:rm, '-f', remote_archive_path)
end

#repo_cache_pathString

Path to repository cache

Returns:

  • (String)


144
145
146
# File 'lib/capistrano/git_copy/scm.rb', line 144

def repo_cache_path
  @_repo_cache_path ||= fetch(:git_repo_cach_path, File.join(tmp_path, 'repo'))
end

#set_defaultsObject

set default values



10
11
12
13
14
15
# File 'lib/capistrano/git_copy/scm.rb', line 10

def set_defaults
  set_if_empty :with_clean, true
  set_if_empty :with_submodules, true
  set_if_empty :git_excludes,    []
  set_if_empty :upload_path,     '.'
end

#testBoolean

Check if repository cache exists and is valid

Returns:

  • (Boolean)

    indicates if repo cache exists



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capistrano/git_copy/scm.rb', line 39

def test
  if backend.test("[ -d #{repo_cache_path} ]")
    backend.within(repo_cache_path) do
      if backend.test(:git, :status, '>/dev/null 2>/dev/null')
        true
      else
        backend.execute(:rm, '-rf', repo_cache_path)

        false
      end
    end
  else
    false
  end
end

#tmp_pathString

Temporary path for all git-copy operations

Returns:

  • (String)


137
138
139
# File 'lib/capistrano/git_copy/scm.rb', line 137

def tmp_path
  @_tmp_path ||= File.join(Dir.tmpdir, deploy_id)
end

#updateObject

Update repo and submodules to branch

Returns:

  • void



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/capistrano/git_copy/scm.rb', line 67

def update
  git(:remote, :update)
  git(:reset, '--hard', commit_hash)

  # submodules
  if fetch(:with_submodules)
    git(:submodule, :init)
    git(:submodule, :update)
    git(:submodule, :foreach, '--recursive', :git, :submodule, :update, '--init')
  end

  # cleanup
  if fetch(:with_clean)
    git(:clean, '-d', '-f')
  end

  if fetch(:with_submodules)
    git(:submodule, :foreach, '--recursive', :git, :clean, '-d', '-f')
  end
end