I have a Hugo site that I’ve been deploying by running Hugo on the server. But this isn’t the only way about it.
If you use Git and it’s on the same server as your site, and owned by the same user, it’s remarkably easy to do this.
First make sure the public
folder in your Hugo repository is being tracked. Yes, this can make your repository a little large but that’s not something to worry about too much. Space is cheap, or it should be. Next make a folder in tmp
– I called mine library
– to store the Git output in.
The new post-update
code then looks like this:
#!/bin/sh SRC_DIR=$HOME/tmp/library/public/ DST_DIR=$HOME/public_html/library/ export GIT_WORK_TREE=$HOME/tmp/library/ git checkout -f rsync -a --delete $SRC_DIR $DST_DIR exit
What this does is checkout the Git repository and then copy it over. The format of the sync will delete anything not found. Done.
The benefit of this method is that you don’t need to install GoLang or Hugo on your server, and everything is pure and simple Git and copy. Rsync is a delightful way to copy everything over as well. You can delete the temp folder when you’re done, but the checkout process handles things for you. Another nice trick is you can specify what branch to checkout, so if you have a special one for publishing, just use that.
But could this be even easier? Yes and no. You see, what I’m going is checking out the whole thing and then copying over folders. What if I could tell Git to just checkout the code in that one folder?
There’s a think called a ‘sparse checkout’ where in I can tell Git “Only checkout this folder.” Then all I have to do is go into that folder and checkout the content I wanted. The problem there is it literally checked out the folder ‘public’ and what I wanted was the content of the public folder. Which means while it’s ‘easier’ in that I’ve only checked out the code I need, I can’t just checkout it out into where I want. I will always have to have a little extra move.
To set up my folder, I did this:
cd ~/tmp/library/ git init git remote add -f origin ~/repositories/library.git git config core.sparsecheckout true echo public/ >> .git/info/sparse-checkout git checkout master
And then my script remains the same. But! This is going to be a faster checkout since it’s only ever going to be exporting and seeing the folders it needs.