Module: Potion::Deployers

Included in:
Site
Defined in:
lib/potion.rb,
lib/potion/extensions/deploy_to_gh_pages.rb

Instance Method Summary collapse

Instance Method Details

#deploy_to_gh_pages(source_dir) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/potion/extensions/deploy_to_gh_pages.rb', line 2

def deploy_to_gh_pages(source_dir)
  original_branch = `git status`.match(/On branch (?<branch>\S*)/)['branch']

  base_dir = `pwd`.strip
  tmp_file_path = "/tmp/potion_#{Time.now.to_i}"
  FileUtils.mkdir_p(tmp_file_path)
  FileUtils.cp_r(File.join(source_dir, "."), tmp_file_path)

  File.open(File.join(tmp_file_path, "potion_deploy.txt"), "w+") do |file|
    file.puts "Built and deployed by Potion on: #{Time.now}"
  end

  switch_branches = `git checkout gh-pages 2>&1`.strip
  raise "Could not switch branches: \n#{switch_branches}" unless switch_branches.include?("Switched to branch 'gh-pages'")

  delete_current_files = `rm -rf * 2>&1`.strip
  raise "Error clearing out old files: \n#{delete_current_files}" unless delete_current_files == ""

  FileUtils.cp_r(File.join(tmp_file_path, "."), base_dir)

  `git add .`
  `git commit -a -m "Automatic commit by Potion as part of deploy: #{Time.now}"`

  puts "*** Pushing to: origin/gh-pages"
  `git push origin gh-pages --force`

  puts

  `git checkout #{original_branch}`
  puts "\n*** Deploy complete."
end