A simpler way to deploy to GitHub Pages

Hey guys, rather than messing about with submodules or the public folder. I developed a much simpler shell script which may be run and deploys to GitHub pages with one click (no setup required):

Disclaimer: This methodology assumes you are working in a branch called “source” and intend to deploy to the “master” branch as per docs here. This will overwrite your master branch so use with care please! I can’t be held responsible if your computer blows up and your cat meows loudly as a result.

#!/bin/bash

# Generate a new temporary directory for our deployment
deploy_dir=$(mktemp -d -t hugo-deploy)
echo "Using deploy directory ${deploy_dir}"

# Determine the remote origin Git location
git_remote=$(git remote get-url --push origin)
echo "Determined that Git remote is ${git_remote}"

# Update git submodules
echo "Updating Git submodules"
git submodule init
git submodule update

# Build the site in the deploy directory
echo
echo "Building Hugo site"
hugo -d "$deploy_dir"

# Initialise the deploy directory as a Git repository and add content
cd "$deploy_dir"

echo
echo "Creating a new Git repository and adding content"
git init
git remote add origin "$git_remote"
git checkout --orphan master
git add .
git commit -m "Site updated at $(date -u "+%Y-%m-%d %H:%M:%S") UTC"

echo
echo "Deploying code to the master branch"
git push --force origin master

Hope this helps :smile:

2 Likes