Building a Hugo Site and Deploying to Linux using Azure DevOps
Summary (tl;dr)
Building a Hugo site using Azure DevOps and deploying to a Linux server is done by:
- Set up an SSH service connection in Azure DevOps
- Create a pipeline in Azure DevOps. The pipeline will need several steps:
- Install Hugo
- Build the site
- Deploy the built site
Setting up an SSH Service Connection in Azure DevOps
An SSH service connection is what will allow your hosted Azure DevOps agent to connect to your Linux based server using SSH. Service connections can be found under the Project Settings, on the bottom left side of the UI:
Find the Service Connections link under the Pipelines group and click on it:
Create a new SSH service connection type by selecting it from the list of available service connections:
Enter the required information and click save when finished.
At this point your service connection is ready to be used. Make a note of the name you gave your service connection. You will need to use it in the YAML pipeline.
Creating the Azure DevOps Pipeline YAML File
We’ll be using a YAML based pipeline to manage the deployment. Here is what that pipeline looks like:
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- bash: |
wget https://github.com/gohugoio/hugo/releases/download/v0.76.3/hugo_0.76.3_Linux-64bit.deb -O hugo.deb
sudo dpkg -i hugo.deb
hugo version
displayName: Install hugo
- script: hugo
displayName: Build the site
workingDirectory: $(Build.Repository.LocalPath)/hugo
- task: CopyFilesOverSSH@0
inputs:
sshEndpoint: 'the name of the service connection you created earlier goes here'
sourceFolder: $(Build.Repository.LocalPath)/hugo/public
targetFolder: 'path/to/the/folder/to/copy/files/to/on/target/machine'
cleanTargetFolder: true
You can check in this file to the repository you want to trigger the build and deployment or you can copy and paste it when you enable the pipeline below.
Enabling your YAML Pipeline
The last step in the process is enabling / creating the pipeline. How you do that depends on where your code is. Follow the advice on the screen and you should be able to complete the whole process and test the pipeline.
Closing Remarks
This is a basic pipeline that pushes out a hugo based site to a Linux server from Azure DevOps. It can be improved by packaging and compressing the files on the build agent and then decompressing and unpackaging on the target server, which will reduce the amount of time it takes to transfer the files. You may also want to consider copying the files to a different directory each time and switching over to the new directory once the copy is complete so that you minimize site downtime. Notice also that the hugo version is hardcoded in the pipeline. This may or may not be what you want.