Class: Capistrano::GitCopy::SCM

Inherits:
SCM::Plugin
  • Object
show all
Includes:
SCMHelpers
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)


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

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

#checkObject

Check if repository is accessible

Returns:

  • void



37
38
39
# File 'lib/capistrano/git_copy/scm.rb', line 37

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

#cleanupObject

Cleanup repo cache

Returns:

  • void



113
114
115
116
117
# File 'lib/capistrano/git_copy/scm.rb', line 113

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

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

#cloneObject

Clone repo to cache

Returns:

  • void



55
56
57
58
59
# File 'lib/capistrano/git_copy/scm.rb', line 55

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

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

#define_tasksObject

define plugin tasks



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

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

#fetch_revisionObject

Set deployed revision

Returns:

  • void



106
107
108
# File 'lib/capistrano/git_copy/scm.rb', line 106

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

#prepare_releaseObject

Create tar archive

Returns:

  • void



84
85
86
87
88
# File 'lib/capistrano/git_copy/scm.rb', line 84

def prepare_release
  package_release_archive

  exclude_files_from_archive if fetch(:git_excludes, []).length.positive?
end

#register_hooksObject

register capistrano hooks



28
29
30
31
32
# File 'lib/capistrano/git_copy/scm.rb', line 28

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



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

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)

  extract_archive_on_remote(remote_archive_path)
end

#repo_cache_pathString

Path to repository cache

Returns:

  • (String)


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

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

#set_defaultsObject

set default values



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

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



44
45
46
47
48
49
50
# File 'lib/capistrano/git_copy/scm.rb', line 44

def test
  if backend.test("[ -d #{repo_cache_path} ]")
    check_repo_cache_path
  else
    false
  end
end

#tmp_pathString

Temporary path for all git-copy operations

Returns:

  • (String)


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

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

#updateObject

Update repo and submodules to branch

Returns:

  • void



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/capistrano/git_copy/scm.rb', line 64

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
  git(:clean, '-d', '-f') if fetch(:with_clean)

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