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:

  1. Set up an SSH service connection in Azure DevOps
  2. Create a pipeline in Azure DevOps. The pipeline will need several steps:
    1. Install Hugo
    2. Build the site
    3. 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:

Project Settings

Find the Service Connections link under the Pipelines group and click on it:

Service Connections

Create a new SSH service connection type by selecting it from the list of available service connections:

New Service Connection

Enter the required information and click save when finished.

Save New Service Connection

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:

- master

  vmImage: 'ubuntu-latest'

- bash: |
    wget -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
    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.