Deploying Hugo site using Bitbucket pipelines
I have a spare Mac mini running which has all sorts of projects running on it. One of them had a hook into a git repository of a Hugo website which would build it once I committed it to the master and coopy it over to my webserver. It was tedious and sometimes it broke, last week I had enoughof it and looked for a solution that is stable and has less maintenace. The solution I ended up with are the Bitbucket Pipelines
The setup in the end was quite easy, I only had to gather the required examples, configuration and settings from several diffent places. So for prosperity and for other people in the same situation I’m describing my solution here.
First in bitbucket you have to login and go to the repository where you store your Hugo website (source and such) then on the left and bottom go to “Repository settings” in the new itemlist go to the bottom en select “Settings” where you can “Enable Pipelines”.
Then goto “Repository variables” where you can define variables that can be used in your later scripts. I use a variable for the username, server address and the version of Hugo required.
USER my username on the destination machine
SERVER name of the destination server.
HUGO_VERION the version of Hugo, in my case 0.108.0
Then goto “SSH Keys” where you generate a keypair and copy the public key for later use. At the bottom add you destination server to the list of “Known hosts”
The public key is something you should add to the destination server. Log in with the user you set in your variable earlier and add the public key to the file ~/.ssh/authorized_keys
. This will enable access to the destination server.
The last item is to add the file bitbucket-pipelines
to the root of your repository. The content will look like:
bitbucket-pipelines.yml
image: atlassian/default-image:3
options:
# run the script for a maximum of 5 minutes
max-time: 5
pipelines:
default:
- step:
name: Build Hugo
script:
- apt-get update -y && apt-get install wget
- apt-get -y install git
- echo Hugo version is $HUGO_VERSION
- export HUGO_ENV=production
- wget https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-amd64.deb
- dpkg -i hugo*.deb
- git submodule update --init --remote
- hugo --minify
artifacts:
- public/**
- step:
name: Deploy artifacts using SCP to PROD
deployment: production
script:
- pipe: atlassian/scp-deploy:1.2.1
variables:
USER: $USER
SERVER: $SERVER
REMOTE_PATH: '/destination'
LOCAL_PATH: 'public/*'
That’s it, then commit these files to your repository and things should start moving.