Last active
May 11, 2020 16:17
-
-
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
This file contains hidden or 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
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