Skip to content

Instantly share code, notes, and snippets.

@rylio
Last active May 10, 2017 10:27
Show Gist options
  • Save rylio/5d1395d4ccc338a8ca80577de47fb054 to your computer and use it in GitHub Desktop.
Save rylio/5d1395d4ccc338a8ca80577de47fb054 to your computer and use it in GitHub Desktop.
Deploy hugo site to flynn
#!/bin/bash
while getopts ":r:b:" opt; do
case $opt in
r) remote=$OPTARG ;;
b) branch=$OPTARG ;;
\?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
# set default remote
if [ -z $remote ]; then
remote="flynn"
fi
# set default branch
if [ -z $branch ]; then
branch="public_html"
fi
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Create branch if it doesn't exist
git checkout -b "$branch" 2> /dev/null
if [ $? -eq 0 ]; then
git commit -m --allow-empty "Initial commit"
# switch back to previous branch
git checkout "$current_branch" || exit 1
fi
# Generate site
hugo || exit 1
temp_dir=$(mktemp -d)
function cleanup {
rm -rf $temp_dir
git worktree prune
}
# Remove temp_dir when finished
trap cleanup SIGINT SIGTERM EXIT
# Create a new worktree in temp dir
git worktree add "$temp_dir" "$branch" || exit 1
# Remove previous files
rm -rf "$temp_dir/*"
# Create Staticfile for buildpack
touch "$temp_dir/Staticfile"
# Copy generated files
cp -r public/* $temp_dir
git -C "$temp_dir" add . || exit 1
git -C "$temp_dir" commit --allow-empty -m "Generated content - $(date +%Y-%m-%d:%H:%M:%S)" || exit 1
# Deploy to flynn
git -C "$temp_dir" push "$remote" "$branch:master" --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment