So after getting bored by deploying the blog to the server. I came to the conclusion that this can probably be done in a more efficient manner. After some looking around I decided to try to use a git hook to do it. Also spent some time with this book1 in the weekend
The setup is the following A --> B --> C
where A is the computer where I do the editing, B is the computer hosting my bare git repo, and C is the web-server hosting this page.
The web-page is made with hugo.
I found script2 on the internet i will base my work on.
The most promising hook seems to post-receive, since it executes once for the receive operation.
Getting the hook to run was simple. Just name it post-receive place it in the hooks directory (on the git server) and set the executable bit chmod +x
However to be able to understand what the script is doing (or let’s say debug it) I added a lot of echo "XYZ" >> hooks.log
to the script
The plan is to not have a git on the web-server, but rather make git server at home upload it to the external web-server
Using3 as base of operations and giving up on understanding bash black magic… (Maybe the next post?)
Ran in to a bunch of trouble with running git archive HEAD
giving me the error fatal: not a valid object name: HEAD
after some googling i found the command git symbolic-ref HEAD
that showed the path refs/heads/main
but the file didn’t exist!
It was named refs/head/master
.
So a name change of the master-branch to main solved it git branch -m master main
and the then a git push origin HEAD
got the data to the server.
Then some issues arises with naming and that I had both master and main branch on remote.
git push origin -d master
deleted the master branch
git branch --unset-upstream
made my local repo to stop complaining.
And so the complete script is done, it now deploys the site to the web.
Hold on, why do I get build errors from the hugo build? Turns out the sub-modules are not archived with git archive
. That’s not a problem we have git submodule foreach
4
you would think that this can work:
git submodule foreach --recursive 'git archive --verbose --prefix=repo/$path/ --format=tar main
It works perfectly on on your local repo but not on a bare server repo..
git submodule foreach
that gives me an error about no git-tree. My assumption is that is because its a bare repository. I have to this point not found a solution to this issue yet. Any suggestions? please e-mail me.
So how can the same functionally be solved in a much less elegant way? You guess it, git clone
that will always give me the latest template so the I need to remember to always update the sub-module on my local machine when doing development.
I wouldn’t suggest this for a production environment where bugs in production cannot happen.
And yes the the script could be more elegantly written.
#!/bin/bash
BRANCH="main"
while read oldrev newrev ref
do
if [ "$ref" = "refs/heads/$BRANCH" ];
then
temp_dir=$(mktemp -d) || { echo "Failed to create tmp directory"; exit2;}
git archive --format=tar --output="$temp_dir/web.tar" HEAD
cd "$temp_dir"
tar xf web.tar
cd "$temp_dir/themes"
rm -rf hugo-coder
git clone https://github.com/luizdepra/hugo-coder.git
cd "$temp_dir"
hugo
# Do your transport magic here...
rm -rf "$temp_dir"
echo "Deployment done"
else
echo "Do nothing"
fi
done