Last active
March 12, 2021 13:22
-
-
Save razor-x/8166421 to your computer and use it in GitHub Desktop.
Automatic publishing to GitHub Pages with Travis CI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc 'Generate deck from Travis CI and publish to GitHub Pages.' | |
task :travis do | |
# if this is a pull request, do a simple build of the site 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 | |
repo = %x(git config remote.origin.url).gsub(/^git:/, 'https:').strip | |
deploy_url = repo.gsub %r{https://}, "https://#{ENV['GH_TOKEN']}@" | |
deploy_branch = repo.match(/github\.io\.git$/) ? 'master' : 'gh-pages' | |
rev = %x(git rev-parse HEAD).strip | |
Dir.mktmpdir do |dir| | |
dir = File.join dir, 'site' | |
sh 'bundle exec rake build' | |
fail "Build failed." unless Dir.exists? destination | |
sh "git clone --branch #{deploy_branch} #{repo} #{dir}" | |
sh %Q(rsync -rt --del --exclude=".git" --exclude=".nojekyll" #{destination} #{dir}) | |
Dir.chdir dir do | |
# setup credentials so Travis CI can push to GitHub | |
verbose false do | |
sh "git config user.name '#{ENV['GIT_NAME']}'" | |
sh "git config user.email '#{ENV['GIT_EMAIL']}'" | |
end | |
sh 'git add --all' | |
sh "git commit -m 'Built from #{rev}'." | |
verbose false do | |
sh "git push -q #{deploy_url} #{deploy_branch}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm a Ruby newb.. But am I imagining things or have we not defined what 'destination' is on https://gist.github.com/razor-x/8166421#file-rakefile-L18 ? I'm struggling to get this to run 'NameError: undefined local variable or method `destination' for main:Object'