Timewarp Technologies Blog v2

Leveraging search engines to keep my notes

Apr 29, 2021 - 2 minute read

Automation of the Hugo Upload

Now that we have a few posts under our belt, it is time to automate the process of updating the site. For this I selected powershell as we are doing many small scripts in Azure DevOps using the tool and it is well supported on Windows 10. I decided to use WinSCP to perform the sync of the files, which keeps the transfer times down as it only concerns itself with uploading changed files.

#First, update the content
hugo
#Second, commit the source
git add *
git commit -m "New blog post source"
git push
#Third, commit the created contents, if you have git sync for a site, this is all you need.
Set-Location .\public
git add *
git commit -m "New blog post created"
git push
Set-Location ..\
#To support old school hosting, I use WinSCP to FTP my files
#Finally, FTP the changes
#(Get config)
[xml]$config = Get-Content ".\config.xml"
#(Set up FTP session)
Add-Type -Path "WinSCPnet.dll"
#Note the use of ConvertTo-SecureString, this is the safest way to save configuration data.
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "..."
    UserName = $config.Configuration.UserName
    SecurePassword = ConvertTo-SecureString $config.Configuration.Password
    SshHostKeyFingerprint = "ssh-rsa 2048 7/GM8...u0Oajvo="
}
#Start WinSCP
$session = New-Object WinSCP.Session
#Open the connection
$session.Open($sessionOptions)
#Sync files
$synchronizationResult  = $session.SynchronizeDirectories(
    [WinSCP.SynchronizationMode]::Remote, "./public",".../web/content/", $False
)
#Complain on failure
$synchronizationResult.Check()
git add *
git commit -m "Updated submodule"
git push