Deploying a Hugo Blog with Cloud Build
Introduction
Build, test, and deploy on the Google Cloud serverless CI/CD platform. Cloud Build scales up and scales down in response to load with no need to pre-provision servers or pay in advance for additional capacity. Pay only for what you use.
In this blog post, Cloud Build is used for the automation of Hugo builds. The Hugo automation works in isolation so this technique can be repeated for pretty much any deployment. Therefore, while I use Hugo in the example, feel free to substitute any binary as part of the workflow.
- Reference Blogging with Hugo to get started with Hugo.
Google Cloud Project
Definitely check out Free Tier to try Google Cloud products. Cloud Build is included in the free tier. To use Cloud Build ensure:
- The Cloud Build API (i.e.
cloudbuild.googleapis.com
) is enabled
Cloud Build Configuration
In this section we reference the Cloud Build community edition of Hugo. The assumption is you are aware of how to build this image and push it to Container Registry. How to build this type of artifact was covered in a prior blog post Building an image with Cloud Build.
To configure Cloud Build create a YAML
configuration file.
- Create the file
cloudbuild.yaml
1steps:
2- name: 'gcr.io/[PROJECT_ID]/hugo'
3 dir: '[DIRECTORY]'
PROJECT_ID
: The Project ID for Google Cloud projectDIRECTORY
: The directory in which the code will be accessedSTORAGE_BUCKET
: The GCS destination bucket
NOTE: Cloudbuild will place you in the repo directory by default. So the directory parameter is only required to access a different location e.g. a sub-directory
The cloudbuild.yaml
needs to be added to your repository (preferably in the root directory).
It will be referenced by the Cloud Build Trigger we create in the next step.
Cloud Build Trigger
Triggers are used to initate when an event is fired. In this instance we can ask Cloud Build to perform a task when a commit is made in GitHub.
From Cloud Build select Create trigger
and add these fields:
Field | Description | Example |
---|---|---|
Name | Unique (project wide) name for the Trigger | my-awesome-trigger |
Event | Event to invoke the trigger | Push to a branch |
Source | Repository - connect to a new repo or use existing | joeblogs/my-repo |
Configuration | Config used for the build | cloudbuild.yaml |
- Set the details for the Trigger
- Set the Event information
NOTE: Connecting a repostory to Cloud Build is done through the Cloud Build app
- Set the Configuration information
NOTE: The location of the cloudbuild.yaml is denoted as "
/cloudbuild.yaml
" meaning it is in the root folder. If you have the cloudbuild.yaml in a subdirectory then use "/subdirectory/cloudbuild.yaml
" to reference the file.
Cloud Build Invoke
To invoke the Cloud Build Trigger commit content to the repo referenced in the trigger. The trigger is defined to look at the all commits, unless otherwise specified.
- Once invoked the build are listed under Cloud Build section
Cloud Build allows specific conditions to be used for invocations. It also allows the use of regular expressions.
Cloud Build Workflow
- Avoid working on the main branch e.g. use a development branch
- Be specific with the Cloud Build Trigger condition
- Raise a PR to merge changes back to the mainline