Today we go over the process of writing a very simple Bash script. We’ll be able to detect whether a build for a Hugo static site is present, push the files to a Git repository for our website, and automate all the steps involved.
I’ll begin by showing the code, then explain my process:
#!/bin/bash build_dir=/var/www/html/blog-dev/public deploy_dir=/var/www/html/blog-live/jpiedra.github.io if [ -d $build_dir ] then echo "BUILD FOUND, DETAILS:" echo $(stat $build_dir) cd $deploy_dir && rm -rf * cp -rp $build_dir/* $deploy_dir cd $deploy_dir && git status; git add -A; git commit -m "[Scripted Deploy $(date)]"; git push else echo "NO BUILD FOUND" echo " Run 'hugo' to " echo " create latest build" fi
We begin by setting two variables, build_dir and deploy_dir. These store string values indicating the locations of where our built Hugo site’s files reside, and the directory where those files should be copied to, respectively. In this case, deploy_dir is a GitHub Pages repository, the files used therein are the same ones responsible for powering this website!
The bulk of this script begins where the if.. ..fi structure is shown. This conditional structure begins with a test. Now, because we’re writing a Bash script, rather than using the command line, we have a built-in operator available to us, the square brackets [ ]. These are used to perform any test in a Bash script, thus instead of writing test -d /some/directory we can instead write [ -d /some/directory ].
The -d switch allows us to test whether the provided argument is a directory. The test returns 0 or 1, true and false respectively, depending on whether the provided argument is detected as a valid directory. In this script, we test the value stored in $build_dir (thus, the dollar sign) to check whether a build folder exists in our Hugo site directory - which, by default, would be called public and reside in the Hugo site base directory.
Tip: you can run the command compgen -b with the provided -b switch to get a list of all Bash built-ins available to you. Running the command in your terminal, displays the [ ] built-in near the top of the list.
In the event our test for a directory fails (directory doesn’t exist), we simply echo out a diagnostic message (which would best be output to standard error, more on that later). After else our script prints the messages to stdout, so whatever is echo’ed is visible in the terminal.
If the build directory does exist, we can begin performing the required steps to push our files to the GitHub pages directory, indicated by $deploy_dir. We’ll be making repeated use of a central Bash feature here.
Command substitution “literally plugs… command output into another context,” as the aforementioned page phrases it. Indeed, by using certain operators built into the Bash language, we can get the output of a command and do with it whatever we want - this usually means assinging said output to a variable, parsing it using some other commands, or even saving it to another file.
We’ll use command substitution twice here, as indicated by $(…) where the elipses would be replaced by Bash built-ins or Linux commands. Another form that’s available to us uses backticks, but we’ll stick with the former operator instead.
After echo’ing that we’ve found the build folder, we run a few commands when the build directory is detected.
A few notes on step 4. The && operator ensures that the second command, git status is only executed if cd is successful. This is seen elsewhere, in step 2, ensuring we remove the files only if we successfully changed to the desired folder. Also in step 4, we see the final example of command substitution used when we indicate a message for git commit to use. We capture the output of running the Bash built-in time and use that as part of the content for our commit message.
When you run this script, the last part of this deployment process would involve running git push, so you would be expected to provide your credentials in the terminal to push the latest Hugo site files. In summation, having a script such as this can save precious time, by performing various checks for us along the way as well as eliminating the tedium of typing and executing several commands.
Return homeTags: