Skip to content

Instantly share code, notes, and snippets.

@jamesstout
Last active May 11, 2020 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesstout/42201bbc498a4b91a9a1f884a17ced8c to your computer and use it in GitHub Desktop.
Save jamesstout/42201bbc498a4b91a9a1f884a17ced8c to your computer and use it in GitHub Desktop.
Jekyll hooks to copy the _site folder somewhere else and commit and push to a remote repo, e.g. GitHub.io
Jekyll::Hooks.register :site, :post_write do |site|
Jekyll.logger.info "site.dest: ", site.dest
Jekyll.logger.info "site.source: ", site.source
ioDest = '/Users/james/Projects/jamesstout.github.io'
Jekyll.logger.info "GitHub.io dest: ", ioDest
hash = `git rev-parse --short HEAD`
Jekyll.logger.info "hash: ", hash
lastHashFile = ioDest + '/lastHash'
if ::File.exist?(lastHashFile)
lastHash = File.read(lastHashFile)
Jekyll.logger.info "lastHash: ", lastHash
if lastHash == hash
Jekyll.logger.info "Hashes equal: ", "not copying, committing or pushing"
else
Jekyll.logger.info "Hashes NOT equal: ", "copying, git commit and pushing"
Jekyll.logger.info "Copying _site to: ", ioDest
FileUtils.cp_r(site.dest + '/.', ioDest, :remove_destination => true)
FileUtils.cd(ioDest)
Jekyll.logger.info "pwd: ", `pwd`
Jekyll.logger.info "git:", "Committing new build"
`git add .`
`git commit -a -m "build"`
Jekyll.logger.info "git: ", "Pushing new build"
`git push`
Jekyll.logger.info "Updating hash: ", hash
File.write(lastHashFile, hash)
end
else
Jekyll.logger.error "lastHash file doesn't exist: ", lastHashFile
Jekyll.logger.error "Solution: ", "Try the build again."
File.write(lastHashFile, 'xxxxxx')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment