Project Soon

Deployment

While deploying sounds like it is just to generate a couple of files and upload them to the site, there are still a couple of steps to take, and not just the issues along the way, but problems that may arise when not careful enough. Upload to the server automatically; Only upload the changed files; Remove certain files; Build production. Hugo handles some of this already for us, or at least sort of.

The first thing to make sure is to see how Hugo builds. It is a simple as running hugo and it will build production for you. For local development you can just run hugo serve and visit localhost:1313 to see how it will look like, and add the flag -D in order to see drafts as well. However, while all the links in the files generated will only go to the active ones, all previously created files that should be removed will not be removed. It is understandable that this is designed like this to ensure that all links are permanent, and nothing is removed. I myself feel like sometimes you want to remove files for a reason, for instance old templates, security concerns, or old posts that are outright wrong (even for historical reasons they are valuable). Therefore it is best to remove the public folder before running hugo.

For upload I wondered if there was a good way to only upload the changed files, which made me think about rsync 1. After some further research to both find something for Windows and being able to use an SSH key thought ssh-agent, I discovered rclone 2. Rclone is primarily used for backing up and is a much simplified type of rsync, but it still fits my needs perfectly. I can both check the differences and then sync them one way.

Together with this I created a deploy.bat script to make things a bit more simplified. Basically I run either deploy.bat check, to see what has changed, or deploy.bat sync, to sync them to the server. As one can see it will first remove all files and folders in public and then run hugo for production to generate all files, before they are synced to the server.

1
2
3
4
5
6
7
8
@echo off

del /S /Q public\*
rmdir /S /Q public\*

hugo

rclone "%1" public "blog:sites/blog.aposoc.net"

The blog host is just an SSH alias for the blog site with user name and additional necessary flags. This script makes it a lot easier at least for uploading, but also making sure that the files I upload are both new and removed on the other end. The only issue I have is that rclone will create one connection per file, meaning my ssh-agent(KeePass) will nag me multiple times that it has been accessed. I do not mind the notification, as it makes it more secure, but I do care that it is hundreds of them. Though, they are accumulated down to the latest one, so it is not that big of a deal.


  1. https://en.wikipedia.org/wiki/Rsync ↩︎

  2. https://rclone.org/ ↩︎