I am grateful that Hugo and GitHub (and their community of developers) exist; otherwise, I would not have been able to create this website in the first place. However, if you do not spend enough time digging into how everything works, you may end up feeling lost.

I always had a hard time figuring out all the different steps to modify my website locally, push the changes to my repository, and then see them appear (magically) on my GitHub Pages site. To be honest, investing a bit of time understanding how all parts interact with one another is not that difficult. But let’s be honest, I always dirty my hands first until I can’t take it anymore and finally end up reading about how things are built and meant to work. Note to self: investing time studying is always the right starting point.

I think I finally found a way to help myself by automating the process of updating my blog (and hopefully keep writing on it more than before) using a single Bash function.


How I structured the web site

To keep things easy, I have now two repos:

  • One where I develop my website locally using the theme from Wowchemy, which goes under the name blog.
  • One that has the website name valkiii.github.io and where I transfer the public content of the blog repo.

I decided that I wanted a simple command to run all this. To do so, I wrote a simple Bash function in my .bashrc file containing the following:

alt text

Let’s break down what this function does: First it launches hugo to build the website, generating the static files in the public directory. Then I run rsync -a public/* ../valkiii.github.io/. to sync the contents of the public directory to the root of my GitHub Pages repository (valkiii.github.io in this example). I added the -a flag to ensure that all files and directories are copied recursively while preserving permissions and other attributes.

"Investing time studying is always the right starting point"

Then it's time to add all changes in the current directory (my Hugo project directory) for committing with git add --all. There is not a git add without a following git commit -m "$1" with the only exception that the message that we add the commit get provided as the first argument to the function ($1).

These two commands are naturally followed by a git push that pushes the changes to the remote repository, updating the Hugo project repository.

Then I want to actually update my repository on github, to be sure that the day my computer dies, I am still able to continue updating this website. To do so, fI first go back to my repo folder with cd ../valkiii.github.io, then, like before, I git add, commit, and push all changes with git add --all, git commit -m "$1" and git push. Finally, I go back to the blog folder to continue modifying and rendering my website locally with cd ../blog/.

To use this function, save it in your .bashrc or .bash_profile file and source the file using source ~/.bashrc to make the function available in your terminal session. Then, you can simply run update_website “Your commit message” whenever you want to build and deploy your website with a single command.

Note that this script assumes that your blog and valkiii.github.io repositories are in the same parent directory.

By automating the build and deployment process with a Bash function, you can save time and ensure consistency in your website updates. Give it a try and adapt it to fit your own website setup!