Module: JekyllGitDeploy

Includes:
CommonTasks
Defined in:
lib/jekyll_git_deploy.rb

Constant Summary

Constants included from CommonTasks

CommonTasks::CONFIG_DEFAULTS, CommonTasks::CONFIG_FILE, CommonTasks::REQUIRED_CONFIGS

Instance Method Summary collapse

Methods included from CommonTasks

#checkout_target_branch, #commit_newest_site, #current_branch, included, #read_configs

Instance Method Details

#deployObject

Build and push(deploy) the newest generated site



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jekyll_git_deploy.rb', line 49

def deploy
  # Pull the newest deploy data
  Dir.chdir destination do
    unless `git branch`.empty?
      checkout_target_branch

      puts "Updating remote deployed versions...".green
      `git pull #{deploy_remote_name} #{deploy_branch}`
    end
  end

  puts "\nGenerating the newest site".yellow
  `jekyll build`

  # This step is only for sites requiring some special file to detect running environment
  unless destination.empty?
    puts "\nTouching Staticfile".yellow
    `touch #{destination}/Staticfile`
  end

  puts "\nruning `cd #{destination}`".yellow

  Dir.chdir destination do
    if `git branch`.empty?  # Fully new git repo
      commit_newest_site

      puts "\nThis is a new git repo, creating new branch #{deploy_branch} now".yellow
      `git checkout -b #{deploy_branch} &> /dev/null && git merge #{current_branch}`
    else
      checkout_target_branch
      commit_newest_site
    end

    puts "\nPushing newest generated site:".yellow
    push_result = `git push -u #{deploy_remote_name} #{deploy_branch} 2>&1`
    if push_result.include?("Updates were rejected because the remote contains work")
      print "Oops! It seems there are some updates exist in the remote repo so that your pushing has been rejected, do you want to force to update?[Y/N]".red
      if STDIN.gets.chomp.to_s.downcase == 'y'
        puts "Try to force to update remote repo...".yellow
        `git push -f #{deploy_remote_name} #{deploy_branch}`
      else
        puts "You have canceled the deploy process, you need to manually merge remote updates before deploy!".yellow
        exit
      end
    else
      puts push_result
    end

    puts "\nThe deploy is finished!".green
    baseurl = read_configs['baseurl'] || ""
    `open "#{baseurl}"` if baseurl =~ /https?:\/\/\S+/
  end
end

#initObject

Initialize the deploy environment



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jekyll_git_deploy.rb', line 10

def init
  configs = read_configs

  puts "Start to initialize the git repo at #{destination}".yellow
  unless deploy_repo && !deploy_repo.strip.empty?
    raise "You haven't specify any deploy repo in _config.yml file, the action can not continue!".red
  end

  file = File.open('.gitignore', 'a+')
  existed_line = file.readlines.select{|line| line.strip == destination}
  unless existed_line
    puts "\nadd #{destination} directory to .gitignore".yellow
    file.puts destination
  end
  file.close

  `mkdir -p #{Dir.pwd}/#{destination}`
  puts "\nruning `cd #{destination}`".yellow
  Dir.chdir destination do
    # Determine if there has been any git repo existed
    unless File.directory? ".git"
      `git init`
    end

    puts "\nStart to add remote url".yellow
    `git remote add #{deploy_remote_name} #{deploy_repo} 2> /dev/null`

    puts "\nCurrent git remotes:\n==========================".yellow
    puts `git remote -v`

    puts "Fetch remote codes...".yellow
    `git pull #{deploy_remote_name} #{deploy_branch}`

    puts "\nFinished deploy initialization for the current site".green
  end
end