-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
148 lines (127 loc) · 4.07 KB
/
Rakefile
File metadata and controls
148 lines (127 loc) · 4.07 KB
1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#############################################################################
#
# Modified version of mfenner/jekyll-travis Rakefile
# https://github.com/mfenner/jekyll-travis/blob/master/Rakefile
#
#############################################################################
require 'rake'
require 'date'
require 'yaml'
require 'html/proofer'
CONFIG = YAML.load(File.read('_config.yml'))
USERNAME = CONFIG['username'] || ENV['GIT_NAME']
REPO = CONFIG['repo'] || "#{USERNAME}.github.io"
# Determine source and destination branch
if REPO == "#{USERNAME}.github.io"
SOURCE_BRANCH = CONFIG['branch'] || 'source'
DESTINATION_BRANCH = 'master'
else
SOURCE_BRANCH = 'master'
DESTINATION_BRANCH = 'gh-pages'
end
#############################################################################
#
# Helper functions
#
#############################################################################
# Print error and exit
def die (string)
puts string
exit
end
# Configure git
def configure_git
sh "git config --global user.name '#{ENV['GIT_NAME']}'"
sh "git config --global user.email '#{ENV['GIT_EMAIL']}'"
sh 'git config --global push.default simple'
end
# If destination folder exists, sync it with the remote. Otherwise, clone it
# from the remote. Then, clear its contents.
def pull_destination
if Dir.exist? CONFIG['destination']
Dir.chdir CONFIG['destination'] { sh 'git pull --rebase' }
else
# Use GH_TOKEN environment variable only if in Travis CI
if ENV['TRAVIS']
sh "git clone https://#{USERNAME}:#{ENV['GH_TOKEN']}@github.com/#{USERNAME}/#{REPO}.git #{CONFIG['destination']}"
else
sh "git clone git@github.com:#{USERNAME}/#{REPO}.git #{CONFIG['destination']}"
end
end
# Create destination branch if necessary.
Dir.chdir CONFIG['destination'] do
branches = `git branch -a --list | cut -c3-`.split("\n")
# If remote destination branch exists, checkout to that branch. Otherwise,
# create an orphan branch.
if branches.include? "remotes/origin/#{DESTINATION_BRANCH}"
sh "git checkout -b #{DESTINATION_BRANCH} origin/#{DESTINATION_BRANCH}"
else
sh "git checkout --orphan #{DESTINATION_BRANCH}"
end
sh 'touch .keep'
sh 'git add .keep'
# Wipe the slate clean to ensure clean builds.
sh 'git rm -fr .'
sh 'rm -fr *'
end
end
# Generate the site
def build_destination
sh "git checkout #{SOURCE_BRANCH}"
sh 'bundle exec jekyll build'
# Write CNAME based on URL key in _config.yml
File.open("#{CONFIG['destination']}/CNAME", 'w') do |f|
f.write CONFIG['url'][/http:\/\/(.*\.com)/, 1]
end
end
# HTML proof the site
def validate_destination
opts = {
disable_external: true,
href_ignore: [/.*/],
}
HTML::Proofer.new(CONFIG['destination'], opts).run
end
# Commit the contents of the destination folder into the destination branch and
# push the commit to the remote.
def push_destination
sha = `git log`.match(/[a-z0-9]{40}/)[0]
Dir.chdir CONFIG["destination"] do
sh 'git add --all .'
sh "git commit -m 'Update to #{USERNAME}/#{REPO}@#{sha}'"
sh "git push --quiet origin #{DESTINATION_BRANCH}"
puts "Pushed updated branch #{DESTINATION_BRANCH} to GitHub Pages"
end
end
#############################################################################
#
# Site tasks
#
#############################################################################
namespace :site do
desc 'Generate the site'
task :build do
build_destination
end
desc 'Generate the site and serve locally'
task :serve do
sh 'bundle exec jekyll serve'
end
desc 'Generate the site, serve locally and watch for changes'
task :watch do
sh 'bundle exec jekyll serve --watch'
end
desc 'Generate the site and validate HTML'
task :validate do
validate_destination
end
desc 'Generate the site, validate HTML and push to destination branch'
task :deploy do
die 'Pull request detected. Not proceeding with deploy.' if ENV['TRAVIS_PULL_REQUEST'].to_s.to_i > 0
configure_git if ENV['TRAVIS']
pull_destination
build_destination
validate_destination
push_destination
end
end