forked from strykes/Docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
51 lines (44 loc) · 1.7 KB
/
Rakefile
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
require 'middleman-gh-pages'
require 'tmpdir'
desc 'Generate site from Travis CI and publish site to GitHub Pages'
task :travis do
# If this is a pull request, do a simple build and stop
if ENV['TRAVIS_PULL_REQUEST'].to_s.to_i > 0
puts 'Pull request detected. Executing build only.'
sh 'bundle exec rake build'
next
end
# Set repo, branch, and commit revision for later use
repo = `git config remote.origin.url`.gsub(/^git:/, 'https:').strip
branch = repo =~ /github\.com\.git$/ ? 'master' : 'gh-pages'
rev = `git rev-parse HEAD`.strip[0..6]
# Build the project and set build location
sh 'bundle exec rake build'
origin = "#{Dir.getwd}/build"
# Make temporary directory an copy files
Dir.mktmpdir do |dir|
Dir.chdir dir do
fail "Deployment failed." unless Dir.exists? origin
sh "git clone --branch #{branch} #{repo} ."
sh %(rsync -rt --del --exclude=".git" "#{origin}/" .)
# Setup credentials so Travis CI can push to GitHub
verbose false do
sh "git remote set-url --push origin #{repo}"
sh "git remote set-branches --add origin #{branch}"
sh "git config user.name Travis"
sh "git config user.email [email protected]"
sh 'git config credential.helper "store --file=.git/credentials"'
File.open('.git/credentials', 'w') do |f|
f << "https://#{ENV['GITHUB_TOKEN']}:@github.com"
end
end
# Add files, create commit, and push to GitHub
sh 'git add --all'
sh "git commit -m 'Built from https://github.com/#{ENV['TRAVIS_REPO_SLUG']}/commit/#{rev}'"
verbose false do
sh "git push -q #{repo} #{branch}"
end
File.delete '.git/credentials'
end
end
end