Skip to content

Instantly share code, notes, and snippets.

@ace-subido
Last active June 23, 2017 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ace-subido/51e1f12cd29fd94363f0 to your computer and use it in GitHub Desktop.
Save ace-subido/51e1f12cd29fd94363f0 to your computer and use it in GitHub Desktop.
Rakefile to publish Jekyll to a User Page

Disclaimer

This Rakefile will abuse your master branch. It will go against the usual git flow, if that doesn't ride well with you, don't use this.

Why did you do this? Why not just ride with the Github Pages Jekyll build server?

Too much wrangling with the dependencies on the Gemfile. My user page isn't some big project that everyone would use. I figured it's worth enough to sacrifice "best practices" for version control. It's just a user page, I don't want to pull my hair out trying to figure out dependencies in the build server when I add additional gems.

Setup

The Jekyll source of my site currently resides on another branch. master only contains the /_site contents.

Usage

  • Make sure your source directory is on another branch other than master.
  • Add /_site to .gitignore. Remove it in the git history if it's already added
  • rake build is the same as jekyll build
  • rake publish fires off a jekyll build and a git push origin master on your /_site folder.

How rake publish works

  • Goes into your /_site folder and initialize a git folder there.
  • Adds your git remote as it's git remote.
  • git add .'s that entire folder as part of a push into origin/master

Credits

This Rakefile is based off of the middleman-gh-pages gem.

Why not use middleman? You're already using it on other projects and you've based this off entirely of that gh-pages gem.

The power of Jekyll Tumblr Import is too good to pass up :)

require 'fileutils'
def remote_name
ENV.fetch("REMOTE_NAME", "origin")
end
PROJECT_ROOT = `git rev-parse --show-toplevel`.strip
BUILD_DIR = File.join(PROJECT_ROOT, "_site")
USER_PAGE_REF = File.join(BUILD_DIR, ".git/refs/remotes/#{remote_name}/master")
directory BUILD_DIR
file USER_PAGE_REF => BUILD_DIR do
repo_url = nil
cd PROJECT_ROOT do
repo_url = `git config --get remote.#{remote_name}.url`.strip
end
cd BUILD_DIR do
sh "git init"
sh "git remote add #{remote_name} #{repo_url}"
sh "git fetch #{remote_name}"
if `git branch -r` =~ /master/
sh "git checkout master"
else
sh "git checkout --orphan master"
sh "touch index.html"
sh "git add ."
sh "git commit -m 'initial master commit'"
sh "git push #{remote_name} master"
end
end
end
task :prepare_git_remote_in_build_dir => USER_PAGE_REF
task :sync do
cd BUILD_DIR do
sh "git fetch #{remote_name}"
sh "git reset --hard #{remote_name}/master"
end
end
# Prevent accidental publishing before committing changes
task :not_dirty do
puts "***#{ENV['ALLOW_DIRTY']}***"
unless ENV['ALLOW_DIRTY']
fail "Directory not clean" if /nothing to commit/ !~ `git status`
end
end
desc "Compile all files into the build directory"
task :build do
cd PROJECT_ROOT do
sh "bundle exec jekyll build"
end
end
desc "Build and publish to Github User Page"
task :publish => [:not_dirty, :prepare_git_remote_in_build_dir, :sync, :build] do
message = nil
suffix = ENV["COMMIT_MESSAGE_SUFFIX"]
cd PROJECT_ROOT do
head = `git log --pretty="%h" -n1`.strip
message = ["Site updated to #{head}", suffix].compact.join("\n\n")
end
cd BUILD_DIR do
sh 'git add --all'
if /nothing to commit/ =~ `git status`
puts "No changes to commit."
else
sh "git commit -m \"#{message}\""
end
sh "git push #{remote_name} master -f"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment