Background
This project is built with Hugo and uses the Minimal Hugo theme.
This post is part of an ongoing effort to tailor the theme over time.
Specifically, this post is part of a project to build a contact form with Hugo and host it with Netlify.
Objective
In this post we’ll develop Bash scripts to separate the content for two website subdomains that was generated by one Hugo project.
As previously mentioned, we built our solution for both sites inside a single Hugo project.
However, we wish to manage and deploy the content of those sites separately.
We’ll use Bash scripts to separate the files generated by the project for the two subdomains.
Building the solution for the main subdomain
In a previous post, we discussed using Bash scripts to automate our Hugo website publishing workflow.
We’ll alter and extend those scripts to handle our updated workflow.
Creating a new file deletion script
The only change in the current workflow for our main site content is that we want to delete the new contact files before we push changes to our main repository. Since we already have a Bash script that deletes files from a specified directory (sitebuild_02_deletefiles.sh), we’ll start with that and make any necessary alterations. And since we want to keep those files for the other subdomain, we’ll add this new deletion step to the end of our current process and only delete files from the main site publication directory.
We’ll copy the sitebuild_02_deletefiles.sh script, rename it to sitebuild_04_deletedirectory.sh, and update it to the following. (All altered lines are highlighted.)
#!/bin/bash
# VARIABLES
directoryofwebsiteforpublishing=$1
subdirectorytodelete=$2
fulldirectory=$directoryofwebsiteforpublishing/$subdirectorytodelete
# FUNCTION deletefilesanddirectories - deletes all files and directories in a specified directory
deletefilesanddirectories() {
cd $directoryofwebsiteforpublishing
rm --verbose --recursive $subdirectorytodelete
}
# FUNCTION indent - indents a line before outputting
indent() {
sed 's/^/ /';
}
# FUNCTION warningwithprompt - prints a warning that the script will delete files and subdirectories and prompts the user before continuing
warningwithprompt() {
cd $fulldirectory
echo "This script will DELETE the contents of $fulldirectory"
echo
echo "This script will DELETE the following files, subdirectories, and ALL subdirectory contents:"
echo
ls -1 --color | indent
echo
read -n1 -p "Do you wish to proceed? [y/n] " response
# Convert response to lowercase
response=${response,,}
echo
echo
}
# MAIN program flow
echo
echo "Executing $BASH_SOURCE"
echo
warningwithprompt
case $response in
y|ye|yes)
echo Executing script.
echo
deletefilesanddirectories
echo
echo Script completed.
echo
;;
*)
echo Deletion cancelled.
echo
exit
;;
esac
Because we don’t have a file to preserve, we removed all references to that specific functionality.
That also allowed us to use the rm command directly instead of piping a list of files and directories to it.
We also altered the way directory variables were defined to make it a little simpler to reference them in commands.
Updating the calling script for the main content workflow
Next, we only needed to make a few small changes to our main workflow calling script.
We renamed the sitebuild_00_publishsite.sh script to publish-main-site.sh and updated it to the following. (All altered lines are highlighted.)
#!/bin/bash
# VARIABLES
directoryofhugoproject=/home/user/TEMP/test-directory/hugo-project-directory
directoryofhugoprojectfiles=/home/user/TEMP/test-directory/hugo-project-directory/public
directoryofwebsiteforpublishing_main=/home/user/TEMP/main-directory/publishing-directory
filetonotdelete_main=other.html
directorytodelete_main=contact
# FUNCTION script_info - provide contextual output
script_info() {
echo
echo
echo
echo
echo =============================================================
echo ==================== script info - START ====================
echo
echo "Executing $BASH_SOURCE"
echo
echo "This script rebuilds ONLY the MAIN website content."
echo
echo ===================== script info - END =====================
echo =============================================================
echo
echo
}
# FUNCTION sb_rebuildsite - run the sitebuild_01_rebuildsite.sh script with additional output
sb_rebuildsite() {
echo
echo
echo ---------- Step 1: Rebuild site - START ----------
source sitebuild_01_rebuildsite.sh $directoryofhugoproject
echo ---------- Step 1: Rebuild site - END ----------
echo
echo
}
# FUNCTION sb_deleteoldfiles_main - run the sitebuild_02_deletefiles.sh script with additional output
sb_deleteoldfiles_main() {
echo
echo
echo ---------- Step 2: Delete files - START ----------
source sitebuild_02_deletefiles.sh $directoryofwebsiteforpublishing_main $filetonotdelete_main
echo ---------- Step 2: Delete files - END ----------
echo
echo
}
# FUNCTION sb_copyfiles_main - run the sitebuild_03_copyfiles.sh script with additional output
sb_copyfiles_main() {
echo
echo
echo ---------- Step 3: Copy files - START ----------
source sitebuild_03_copyfiles.sh $directoryofhugoprojectfiles $directoryofwebsiteforpublishing_main
echo ---------- Step 3: Copy files - END ----------
echo
echo
}
# FUNCTION sb_deletecontactdirectory_main - run the sitebuild_04_deletedirectory.sh script with additional output
sb_deletecontactdirectory_main() {
echo
echo
echo ---------- Step 4: Delete files - START ----------
source sitebuild_04_deletedirectory.sh $directoryofwebsiteforpublishing_main $directorytodelete_main
echo ---------- Step 4: Delete files - END ----------
echo
echo
}
# MAIN program flow
script_info
sb_rebuildsite && sb_deleteoldfiles_main && sb_copyfiles_main && sb_deletecontactdirectory_main
The first set of changes was to rename several of the variables to include the _main suffix to help distinguish those variables from others that would later reference directories and files for the contact subdomain.
Next, additional visual output was removed from the main program flow and put in a new function called script_info.
The most significant functional change was creating a new function, sb_deletecontactdirectory_main, that calls the sitebuild_04_deletedirectory.sh script and adding the directorytodelete variable to pass to the script.
Building the solution for the contact subdomain
The workflow for building the files of the contact subdomain is almost identical to that of the legacy main subdomain workflow. Only the files to be copied and the target directory are different.
To automate the workflow for the contact domain, we copied the relevant updated scripts for building the main subdomain and made minor alterations.
Creating a new directory copying script
Our current script for copying files is a close match for what we want to accomplish here. So, we’ll copy the sitebuild_03_copyfiles.sh script and rename it to sitebuild_05_copydirectory.sh.
However, for our contact subdomain files we only want to copy a single subdirectory, instead of every subdirectory and file except one. To accomplish that we added a variable to specify the desired subdirectory and altered the call to the cp function to reference that subdirectory.
The updated code is below. (All altered lines are highlighted.)
#!/bin/bash
# VARIABLES
directoryofhugoprojectfiles=$1
directoryofwebsiteforpublishing=$2
directorytocopy=$3
# FUNCTION copyfilesanddirectories - copy all files and directories in a specified directory to another specified directory
copyfilesanddirectories() {
echo "Copying files."
echo
echo "FROM:"
echo "$directoryofhugoprojectfiles/$directorytocopy"
echo
echo "TO:"
echo "$directoryofwebsiteforpublishing"
echo
cp --recursive $directoryofhugoprojectfiles/$directorytocopy $directoryofwebsiteforpublishing/
}
# MAIN program flow
echo
echo "Executing $BASH_SOURCE"
echo
copyfilesanddirectories
Creating a calling script for the contact content workflow
Next, we created a new contact workflow calling script.
We copied the publish-main-site.sh script, renamed it to publish-contact-site.sh, and updated it for this workflow. All the altered lines are highlighted.
#!/bin/bash
# VARIABLES
directoryofhugoproject=/home/user/TEMP/test-directory/hugo-project-directory
directoryofhugoprojectfiles=/home/user/TEMP/test-directory/hugo-project-directory/public
directoryofwebsiteforpublishing_contact=/home/user/TEMP/contact-directory/publishing-directory
filetonotdelete_contact=
directorytocopy_contact=contact/contact
# FUNCTION script_info - provide contextual output
script_info() {
echo
echo
echo
echo
echo =============================================================
echo ==================== script info - START ====================
echo
echo "Executing $BASH_SOURCE"
echo
echo "This script rebuilds ONLY the CONTACT website content."
echo
echo ===================== script info - END =====================
echo =============================================================
echo
echo
}
# FUNCTION sb_rebuildsite - run the sitebuild_01_rebuildsite.sh script with additional output
sb_rebuildsite() {
echo
echo
echo ---------- Step 1: Rebuild site - START ----------
source sitebuild_01_rebuildsite.sh $directoryofhugoproject
echo ---------- Step 1: Rebuild site - END ----------
echo
echo
}
# FUNCTION sb_deleteoldfiles_contact - run the sitebuild_02_deletefiles.sh script with additional output
sb_deleteoldfiles_contact() {
echo
echo
echo ---------- Step 2: Delete files - START ----------
source sitebuild_02_deletefiles.sh $directoryofwebsiteforpublishing_contact $filetonotdelete_contact
echo ---------- Step 2: Delete files - END ----------
echo
echo
}
# FUNCTION sb_copyfiles_contact - run the sitebuild_03_copyfiles.sh script with additional output
sb_copyfiles_contact() {
echo
echo
echo ---------- Step 3: Copy files - START ----------
source sitebuild_05_copydirectory.sh $directoryofhugoprojectfiles $directoryofwebsiteforpublishing_contact $directorytocopy_contact
echo ---------- Step 3: Copy files - END ----------
echo
echo
}
# MAIN program flow
script_info
sb_rebuildsite && sb_deleteoldfiles_contact && sb_copyfiles_contact
As before, the first set of changes was to rename several of the variables to include the _contact suffix to help distinguish those variables from others for the main subdomain.
Similarly, function names and descriptions were amended for the same reason.
The filetonotdelete_contact variable was left blank because there wasn’t a file we wanted to preserve.
The last function that calls the sitebuild_04_deletedirectory.sh script was removed because it isn’t needed for this workflow.
Creating a calling script for the overall workflow
We also created a new overall workflow calling script.
We copied the publish-main-site.sh script, renamed it to publishsite.sh, and updated it to include the revelant elements of both the main and contact workflows.
#!/bin/bash
# VARIABLES
directoryofhugoproject=/home/user/TEMP/test-directory/hugo-project-directory
directoryofhugoprojectfiles=/home/user/TEMP/test-directory/hugo-project-directory/public
directoryofwebsiteforpublishing_main=/home/user/TEMP/main-directory/publishing-directory
filetonotdelete_main=other.html
directorytodelete_main=contact
directoryofwebsiteforpublishing_contact=/home/user/TEMP/contact-directory/publishing-directory
filetonotdelete_contact=
directorytocopy_contact=contact/contact
# FUNCTION script_info - provide contextual output
script_info() {
echo
echo
echo
echo
echo =============================================================
echo ==================== script info - START ====================
echo
echo "Executing $BASH_SOURCE"
echo
echo "This script rebuilds BOTH the MAIN and CONTACT website content."
echo
echo ===================== script info - END =====================
echo =============================================================
echo
echo
}
# FUNCTION sb_rebuildsite - run the sitebuild_01_rebuildsite.sh script with additional output
sb_rebuildsite() {
echo
echo
echo ---------- Step 1: Rebuild site - START ----------
source sitebuild_01_rebuildsite.sh $directoryofhugoproject
echo ---------- Step 1: Rebuild site - END ----------
echo
echo
}
# FUNCTION sb_deleteoldfiles_main - run the sitebuild_02_deletefiles.sh script with additional output
sb_deleteoldfiles_main() {
echo
echo
echo ---------- Step 2: Delete files - START ----------
source sitebuild_02_deletefiles.sh $directoryofwebsiteforpublishing_main $filetonotdelete_main
echo ---------- Step 2: Delete files - END ----------
echo
echo
}
# FUNCTION sb_copyfiles_main - run the sitebuild_03_copyfiles.sh script with additional output
sb_copyfiles_main() {
echo
echo
echo ---------- Step 3: Copy files - START ----------
source sitebuild_03_copyfiles.sh $directoryofhugoprojectfiles $directoryofwebsiteforpublishing_main
echo ---------- Step 3: Copy files - END ----------
echo
echo
}
# FUNCTION sb_deletecontactdirectory_main - run the sitebuild_04_deletedirectory.sh script with additional output
sb_deletecontactdirectory_main() {
echo
echo
echo ---------- Step 4: Delete files - START ----------
source sitebuild_04_deletedirectory.sh $directoryofwebsiteforpublishing_main $directorytodelete_main
echo ---------- Step 4: Delete files - END ----------
echo
echo
}
# FUNCTION sb_deleteoldfiles_contact - run the sitebuild_02_deletefiles.sh script with additional output
sb_deleteoldfiles_contact() {
echo
echo
echo ---------- Step 5: Delete files - START ----------
source sitebuild_02_deletefiles.sh $directoryofwebsiteforpublishing_contact $filetonotdelete_contact
echo ---------- Step 5: Delete files - END ----------
echo
echo
}
# FUNCTION sb_copyfiles_contact - run the sitebuild_03_copyfiles.sh script with additional output
sb_copyfiles_contact() {
echo
echo
echo ---------- Step 6: Copy files - START ----------
source sitebuild_05_copydirectory.sh $directoryofhugoprojectfiles $directoryofwebsiteforpublishing_contact $directorytocopy_contact
echo ---------- Step 6: Copy files - END ----------
echo
echo
}
# MAIN program flow
script_info
sb_rebuildsite && sb_deleteoldfiles_main && sb_copyfiles_main && sb_deletecontactdirectory_main && sb_deleteoldfiles_contact && sb_copyfiles_contact
The only changes that were necessary were updating the script info description, renumbering the last steps, and incorporating the script calls and variables from both the main and contact workflow. All other elements are the same.
Conclusion
We now have three new and/or updated workflows that allow us to rebuild the main site content, the contact site contact, or both at the same time.