Class: GithubHerokuDeployer::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/github_heroku_deployer/git.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Git

Returns a new instance of Git.



7
8
9
10
11
12
13
# File 'lib/github_heroku_deployer/git.rb', line 7

def initialize(options)
  @heroku_repo = options[:heroku_repo]
  @github_repo = options[:github_repo]
  @id_rsa = options[:id_rsa]
  @logger = options[:logger]
  @repo_dir = options[:repo_dir]
end

Instance Method Details

#cloneObject



53
54
55
56
57
58
59
# File 'lib/github_heroku_deployer/git.rb', line 53

def clone
  wrapper = ssh_wrapper
  @logger.info "cloning #{@github_repo} to #{folder}"
  run "env #{wrapper.git_ssh} git clone #{@github_repo} #{folder}"
ensure
  wrapper.unlink
end

#clone_or_pullObject



45
46
47
# File 'lib/github_heroku_deployer/git.rb', line 45

def clone_or_pull
  !exists_locally? ? clone : pull
end

#exists_locally?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/github_heroku_deployer/git.rb', line 49

def exists_locally?
  File.exists?(File.join(folder, ".git", "config"))
end

#folderObject



35
36
37
# File 'lib/github_heroku_deployer/git.rb', line 35

def folder
  @folder ||= setup_folder
end

#id_rsa_pathObject



78
79
80
81
82
83
# File 'lib/github_heroku_deployer/git.rb', line 78

def id_rsa_path
  file = Tempfile.new("id_rsa")
  file.write(@id_rsa)
  file.rewind
  file.path
end

#openObject



70
71
72
# File 'lib/github_heroku_deployer/git.rb', line 70

def open
  ::Git.open(folder)
end

#pullObject



61
62
63
64
65
66
67
68
# File 'lib/github_heroku_deployer/git.rb', line 61

def pull
  wrapper = ssh_wrapper
  dir = Dir.pwd # need to cd back to here
  @logger.info "pulling from #{folder}"
  run "cd #{folder}; env #{wrapper.git_ssh} git pull; cd #{dir}"
ensure
  wrapper.unlink
end

#push_app_to_heroku(remote = "heroku", branch = "master", &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/github_heroku_deployer/git.rb', line 15

def push_app_to_heroku(remote="heroku", branch="master", &block)
  wrapper = ssh_wrapper
  run "cd #{repo.dir}; git remote rm #{remote}" if repo.remote(remote).url
  repo.add_remote(remote, @heroku_repo)
  yield(repo) if block_given?
  @logger.info "deploying #{repo.dir} to #{repo.remote(remote).url} from branch #{branch}"
  run "cd #{repo.dir}; env #{wrapper.git_ssh} git push -f #{remote} #{branch}"
ensure
  wrapper.unlink
end

#repoObject



26
27
28
# File 'lib/github_heroku_deployer/git.rb', line 26

def repo
  @repo ||= setup_repo
end

#run(command) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/github_heroku_deployer/git.rb', line 85

def run(command)
  result = `#{command} 2>&1`
  status = $?.exitstatus
  if status == 0
    @logger.info result
  else
    raise GithubHerokuDeployer::CommandException, result
  end
end

#setup_folderObject



39
40
41
42
43
# File 'lib/github_heroku_deployer/git.rb', line 39

def setup_folder
  folder = File.join(@repo_dir, Zlib.crc32(@github_repo).to_s)
  FileUtils.mkdir_p(folder)
  folder
end

#setup_repoObject



30
31
32
33
# File 'lib/github_heroku_deployer/git.rb', line 30

def setup_repo
  clone_or_pull
  open
end

#ssh_wrapperObject



74
75
76
# File 'lib/github_heroku_deployer/git.rb', line 74

def ssh_wrapper
  GitSSHWrapper.new(private_key_path: id_rsa_path)
end