Skip to content

Instantly share code, notes, and snippets.

@AngelCrawford
Last active February 20, 2023 09:38
Show Gist options
  • Save AngelCrawford/06dbd50beee9f96064b8dc1d71dfa120 to your computer and use it in GitHub Desktop.
Save AngelCrawford/06dbd50beee9f96064b8dc1d71dfa120 to your computer and use it in GitHub Desktop.
deploy.sh file and Tutorial for Hugo on Github Pages with git subtree and two Repositorys.
#!/bin/bash
Red="\033[0;31m" # Red
Green="\033[0;32m" # Green
Color_Off="\033[0m" # Text Reset
echo -e "$Green Deploying updates to GitHub...$Color_Off"
if [ "$1" ]
then msg="$1"
else
msg="rebuilding site `date`"
fi
if [ "$2" ]
then version="$2"
else
read -p "$(echo -e $Red"Enter Tag Version: "$Color_Off)" version
fi
# Empty the public folder.
rm -rf public/*
# Change the version file
rm version.txt
rm buildDate.txt
echo "$version" >> version.txt
echo "`date +'%a, %Y-%m-%d %T'`" >> buildDate.txt
# Build the project.
hugo
# Add changes to git.
git add --all
# Commit changes.
git commit -am "$msg"
# Add a git tag, to show on the main repository that the site is live.
git tag v$version
# Push source and build repos.
git push origin master --tags
git subtree push --prefix=public git@github.com:[username]/[projectname].git gh-pages

Sources

Information

In my deploy file is a version tag included. This Version is shown on my Page together with the build date. The Version tag shows me in my Hugo repo (not the Github Pages repo) which Website version is live.

Steps

# Create a new orphand branch (no commit history) named gh-pages
git checkout --orphan gh-pages

# Unstage all files
git rm --cached $(git ls-files)

# Grab one file from the master branch so we can make a commit
git checkout master README.md

# Add and commit that file
git add .
git commit -am "INIT: initial commit on gh-pages branch"

# Push to remote gh-pages branch
git push origin gh-pages

# Return to master branch
git checkout master

# Remove the public folder to make room for the gh-pages subtree
rm -rf public
  • Create new empty repo on Github
  • Create CHANGELOG.md on the Github GUI
  • Create branch gh-pages on the new repo with the GUI
# Add the gh-pages branch of the repository. It will look like a folder named public
git subtree add --prefix=public git@github.com:AngelCrawford/profilecard-public.git gh-pages

# Pull down the file we just committed. This helps avoid merge conflicts
git subtree pull --prefix=public git@github.com:AngelCrawford/profilecard-public.git gh-pages

# Run hugo. Generated site will be placed in public directory
hugo

# Add everything and commit
git add --all

# Commit and push to master
git commit -am "Updating site" && git push origin master

# Push the public subtree to the gh-pages branch
git subtree push --prefix=public git@github.com:AngelCrawford/profilecard-public.git gh-pages
  • Create deploy.sh File
  • Use in Terminal bash deploy.sh "my commit msg" "my_tag"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment