TIL how to deploy a static site with a bare git repository
It is possible to deploy a static site without CI/CD by using a bare git repository on a remote server.
- Initialize a bare git repository on the remote server
$ git clone --bare https://github.com/kencx/source.git blog.git
- Create a target worktree directory on the remote server
$ mkdir blog
- Create the following git post-receive hook script inside the bare git repository
This script checks out a copy of the working directory into the directory we
created above, before running zola build to build the static site.
$ touch blog.git/hooks/post-receive
$ cat <<EOF > blog.git/hooks/post-receive
#/bin/sh
set -e
TARGET="/path/to/blog"
GIT_DIR="/path/to/blog.git"
BRANCH="master"
while read oldrev newrev ref; do
if [ "$ref" = "refs/heads/$BRANCH" ]; then
echo "Ref $ref received. Deploying $BRANCH branch..."
git --work-tree="$TARGET" --git-dir="$GIT_DIR" clean -fd
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f "$BRANCH"
cp "$GIT_DIR/refs/heads/$BRANCH" "$TARGET/$BRANCH"
cd "$TARGET" && zola build
else
echo "Ref $ref received. Doing nothing..."
fi
done
EOF
$ chmod u+x blog.git/hooks/post-receive
Note:
This script builds the static files in the directory
$TARGET/public.
- On your local system, setup a new remote in the git repository,
$ cd /path/to/repo
$ git remote add origin git@github.com:kencx/source.git
- Add multiple sources to the same remote
origin
# add the original source as a push remote
$ git remote set-url --add --push origin git@github.com:kencx/source.git
# add the bare git repository in the server on the same remote
$ git remote set-url --add --push origin ssh://foo@my.server/path/to/blog.git
- Pushing to this remote will push to both remotes at the same time
# Pushing to origin will push to both remotes at the same time
$ git push origin master
- Point your web server to
/path/to/blog/publicto serve the static site