Class: GithubBitbucketDeployer::Git
- Inherits:
-
Object
- Object
- GithubBitbucketDeployer::Git
- Defined in:
- lib/github_bitbucket_deployer/git.rb
Instance Method Summary collapse
- #clone ⇒ Object
- #clone_or_pull ⇒ Object
- #exists_locally? ⇒ Boolean
- #folder ⇒ Object
- #id_rsa_path ⇒ Object
-
#initialize(options) ⇒ Git
constructor
A new instance of Git.
- #open ⇒ Object
- #pull ⇒ Object
- #push_app_to_bitbucket(remote = "bitbucket", branch = "master", &block) ⇒ Object
- #repo ⇒ Object
- #run(command) ⇒ Object
- #setup_folder ⇒ Object
- #setup_repo ⇒ Object
- #ssh_wrapper ⇒ Object
Constructor Details
#initialize(options) ⇒ Git
Returns a new instance of Git.
7 8 9 10 11 12 13 |
# File 'lib/github_bitbucket_deployer/git.rb', line 7 def initialize() @bitbucket_repo_url = [:bitbucket_repo_url] @git_repo_name = [:git_repo_name] @id_rsa = [:id_rsa] @logger = [:logger] @repo_dir = [:repo_dir] end |
Instance Method Details
#clone ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/github_bitbucket_deployer/git.rb', line 56 def clone @logger.info "git clone" wrapper = ssh_wrapper @logger.info "cloning #{@bitbucket_repo_url} to #{folder}" run "unset GIT_WORK_TREE; env #{wrapper.git_ssh} git clone #{@bitbucket_repo_url} #{folder}" ensure wrapper.unlink end |
#clone_or_pull ⇒ Object
47 48 49 50 |
# File 'lib/github_bitbucket_deployer/git.rb', line 47 def clone_or_pull @logger.info "clone_or_pull" exists_locally? ? pull : clone end |
#exists_locally? ⇒ Boolean
52 53 54 |
# File 'lib/github_bitbucket_deployer/git.rb', line 52 def exists_locally? File.exists?(File.join(folder, ".git", "config")) end |
#folder ⇒ Object
37 38 39 |
# File 'lib/github_bitbucket_deployer/git.rb', line 37 def folder @folder ||= setup_folder end |
#id_rsa_path ⇒ Object
84 85 86 87 88 89 |
# File 'lib/github_bitbucket_deployer/git.rb', line 84 def id_rsa_path file = Tempfile.new("id_rsa") file.write(@id_rsa) file.rewind file.path end |
#open ⇒ Object
75 76 77 78 |
# File 'lib/github_bitbucket_deployer/git.rb', line 75 def open @logger.info "git open" ::Git.open(folder) end |
#pull ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/github_bitbucket_deployer/git.rb', line 65 def pull @logger.info "git 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_bitbucket(remote = "bitbucket", branch = "master", &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/github_bitbucket_deployer/git.rb', line 15 def push_app_to_bitbucket(remote="bitbucket", branch="master", &block) @logger.info "push_app_to_bitbucket" wrapper = ssh_wrapper run "cd #{repo.dir}; git remote rm #{remote}" if repo.remote(remote).url repo.add_remote(remote, @bitbucket_repo_url) 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 |
#repo ⇒ Object
27 28 29 |
# File 'lib/github_bitbucket_deployer/git.rb', line 27 def repo @repo ||= setup_repo end |
#run(command) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/github_bitbucket_deployer/git.rb', line 91 def run(command) @logger.info "git run command: #{command}" result = system("#{command} 2>&1") sleep 20 if result @logger.info $?.to_s else raise GithubBitbucketDeployer::CommandException, $?.to_s end end |
#setup_folder ⇒ Object
41 42 43 44 45 |
# File 'lib/github_bitbucket_deployer/git.rb', line 41 def setup_folder @logger.info "setup_folder" folder = File.join(@repo_dir, Zlib.crc32(@git_repo_name).to_s) FileUtils.mkdir_p(folder).first end |
#setup_repo ⇒ Object
31 32 33 34 35 |
# File 'lib/github_bitbucket_deployer/git.rb', line 31 def setup_repo @logger.info "setup_repo" clone_or_pull open end |
#ssh_wrapper ⇒ Object
80 81 82 |
# File 'lib/github_bitbucket_deployer/git.rb', line 80 def ssh_wrapper GitSSHWrapper.new(private_key_path: id_rsa_path) end |