Hugo Deployments with Bitbucket Pipelines
Bitbucket has a great feature called Bitbucket Pipelines with that you are able to do tasks automatically after code has been pushed to the repository. This workflow can also be used for Hugo deployments with Bitbucket Pipelines.
Typical tasks for pipelines from my customer projects include e.g.
- run phpunit test suite
- make code syntax checks, e.g. run phpstan, code sniffer, etc.
- generate anonymized dumps for staging environments
Specifically for Hugo projects like this blog I use it to deploy the code automatically to the testing or live servers. The feature is very powerful because you can use any Docker image you like to run the commands. You can either use one of the existing Atlassian images or public images from the Docker registry or create your own one and publish it.
To use pipelines you simply have to activate the Bitbucket Pipelines feature in the configuration settings of the repository and add a file called
bitbucket-pipelines.yml to your repository code.
Here is a sample content of the file for deploying a Hugo project when pushing code to
master branch. The pipeline has 2 steps for build & test and deployment.
The build and test pipeline first loads the current Hugo version from Github and verifies the download and then generates the Hugo build. THe output are simple static files (HTML/CSS/JS/images). The result of this first step is a file called
release.zip that is then deployed to your server.
image: node:latest pipelines: branches: master: - step: name: Build and Test script: - apt-get update -y && apt-get -y install wget git zip - wget -q https://github.com/gohugoio/hugo/releases/download/v0.80.0/hugo_extended_0.80.0_Linux-64bit.deb && echo "702f00f13e68b25ed8d826806aea92f807528584a402c9795de8b9b119b8e9a0 hugo_extended_0.80.0_Linux-64bit.deb" | sha256sum --check - dpkg -i hugo*.deb - git submodule update --init --remote - hugo - (cd public && zip -r ../release.zip .) artifacts: - release.zip - step: name: Deploy to Production deployment: production script: - pipe: atlassian/scp-deploy:0.3.13 variables: USER: $SSH_USER SERVER: $SSH_HOST REMOTE_PATH: $SSH_REMOTE_PATH LOCAL_PATH: 'release.zip' - pipe: atlassian/ssh-run:0.3.0 variables: SSH_USER: $SSH_USER SERVER: $SSH_HOST COMMAND: './deploy.sh'
For this pipeline to work you also have to generate and add the SSH key to the server where you want to deploy to in the pipeline settings in the repository. Additionally you have to add the variables
$SSH_REMOTE_PATH to the environment “Production”.
Also this pipeline assumes that you have a
deploy.sh script on your server that does the unzipping of the release artefact.
To test this pipeline simply push the created
bitbucket-pipelines.yml file to the remote Bitbucket repository. Then go to the repository in the browser and under “Pipelines” you will see the current status of the pipeline as well as any errors: