ArchiveAbout

Kyle Pericak

"It works in my environment"

Created: 2019-08-06Updated: 2019-12-05

Google Cloud Build: Basics

Category:cloudTags:gcpdockerci/cd;
An introduction to the Google Cloud Build, showing how its used for this site.

Table of Contents

Pricing

Current pricing data can be found here.

  • For the first 120 builds in a day, cloud build is free.
  • each build after the free quota costs $0.003 US / build-minute when using the single core build server with no additional SSD storage

Write the Cloud Build file

Google Cloud Build uses a file, cloudbuild.yml, to define the tasks that will be executed when a build is triggered.

The online references for the syntax are pretty good:

Below are some example configuration files used for this site.

IAM Roles

Official Documentation

  • Cloud Build Editor
  • Cloud Build Viewer

Docker Image to Cloud Registry

Cloud Build is great for keeping your Docker Registry images aligned with your version control code.

In this example I use Cloud Build to build and push a pelican image to the Google Container Registry.

Be sure to keep the Dockerfile code in version control. Aside from being a good idea in general, Cloud Build can also use Git commits as a build trigger. Here's my Pelican Docker image repository.

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/pelican', '.' ]
images:
- 'gcr.io/$PROJECT_ID/pelican'

Static Website to Google Cloud Storage

Another use for Cloud Builder is to generate static website files and then sync them up with Cloud Storage every time the source control files or template are changed.

Here's an example that triggers when I commit code to my blog content git repository. It copies the content into /workspace and uses the above pelican image to render the content and output it to the output/ directory.

Once the site content is generated, the next step uses the gcloud image's gsutil -m rsync command to upload the files to the Google Storage Bucket.


Define Cloud Build Triggers

Here's the procedure I used to link a Cloud Build trigger to my Pelican image.

  1. Go to Google Cloud-Build.
  2. On the left side-bar, navigate to Triggers
  3. Add Trigger
  4. Select source: GitHub
  5. Authenticate: Log in if needed
  6. Select Repository: This is the repo that will be watched for changes
  7. Name: This is the name of the trigger
  8. Description: Something like Push to master branch
  9. Trigger Type: Branch
  10. Branch: master
  11. Build Configuration: cloudbuild.yaml
  12. Create Trigger

With this trigger in place, all pushes to the pelican project will trigger the rebuilding of its image, followed by updating the image in GCR with the latest changes.

Tags
ansible
Blog code last updated on 2024-02-18: 5ab386de2324c1884556552d0f043a42f2f726ab