I use static websites based on Hugo since quite a while now. They are fast, nice to edit with the editor of my choice and can be served from any HTTP server. There are no requirements like PHP, Python or MySQL. Unfortunately I never got used to the deployment process.
The intended way of deployment is a classic FTP/SFTP/SCP upload. You simply copy and overwrite everything on the remote server with the new data from your local machine. Sounds easy and is easy. But what if…
Since another rule in my life is: “Put everything into VC repository” and a few searches on the Internet results with nearly the same idea, it should be the right way to go to put my website into a Git repository and directly deploy from it. Most solutions on the Internet suggest to use a single repository with a generated branch, but I don’t like the idea to have the generated website filling up my source repository with redundant data. That’s why I decided to use two repositories and one Cron-Job. In addition, this gives me the possibility to keep the source- private and the generated-repository public.
Here a small overview with real data of this website:
*/5 * * * * root cd /var/www/mfreiholz.de && git pull
Note: You can host your repositories on your on server and don’t have to use GitHub, of course.
In the next step I will show you, how to setup the environment step-by-step. You might stop reading now, if you already know how to setup the above scenario.
I’m not going to show you, how to create the Git repositories. You should already know that and have a place to store them. I will use the above mentioned URLs for this example.
In the first setup, we need to checkout the repository to the configured virtual-host DocumentRoot of your Apache/NGINX server.
mkdir /var/www/mfreiholz.de
cd /var/www/mfreiholz.de
git clone https://github.com/mfreiholz/mfreiholz.de.git
Now setup cron-job for auto update every 5 minutes. Open your /etc/crontab
and add the following line:
*/5 * * * * root cd /var/www/mfreiholz.de && git pull
Restart cron with service cron restart
and you’re done. That’s all you have to do on your server. Everything else happens on client side.
I use to checkout everything flat into my main source directory, e.g.: C:\Source
on Windows:
cd C:\Source
git clone https://github.com/mfreiholz/mfreiholz.de.git
git clone https://github.com/mfreiholz/mfreiholz.de-rendered.git
You do all your work in the first repository and as soon as you’re done with your changes, validated and committed them in the first repository you can generate your website into the second repository with:
cd C:\Source\mfreiholz.de
hugo --cleanDestinationDir --destination ..\mfreiholz.de-rendered
Next you can publish your page with a simple Git commit
and push
command.
cd C:\Source\mfreiholz.de-rendered
git add -A
git commit -m "changes..."
git push
That’s all. Your server will automatically pull the changes from the rendered repository and make them public. Based on the cron-job setup this may take a minute.