So I recently wrote a post about setting up Cloud Build when using private Github repos and submodules.
Since creating this deployment pipeline between Github and Google Cloud Functions, it's been much easier to push updates as well as maintaining an organised coding environment.
^^ me writing code and deploying it really fast
A limitiation with the way I set up this pipeline is that it would deploy the entire github repo to the cloud function. Not terrible - encouragement to keep our code base clean and to ensure each repo is being used for its respective purpose. 😅
Or at least that's what I told myself to mark it as an issue for later. Only that later is now.
But wait, it's easy!
Turns out making a small adjustment so that not only can you deploy just the files you want, but that you can have multiple triggers within the same repo is super easy to do! Here's how -
First - Folderise
Did I just invent that word? Yes.
First step is to reorganise your github repo hosting the source code for multiple cloud functions into folders for each function. Any common functions or files, keep outside or create a common folder.
Next - Create Cloud Build Config Files
For each subfolder containing source code to deploy, create a separate cloudbuild.yaml
file in each folder.
There are two things to do so the Cloud Build file knows what to deploy:
1. Copy Common Files
If you are using any common folders or files like me, add a step to copy any files/folders from the parent repo into the subfolder.
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: 'bash'
dir: 'subfolder1'
args:
- -c
- |
cp -r ../submodule ./
cp ../requirements.txt ./
2. Set directory
For your deployment step, set the directory to the relevant subfolder so that only files within will be included in the deployment using the dir
tag.
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' # Cloud SDK container
dir: 'subfolder1'
args:
- gcloud
- functions
- deploy
etc
Finally - Create Cloud Build triggers
Create a trigger for each subfolder set up within your repo. Key points are:
- Choose 'Cloud Build configuration file (YAML or JSON)' as the configuration type and set to the correct file within subfolder
- Add in 'subfolder/*' for included files so deployments are only triggered when something in the relevant subfolder is updated
And done!
The end result is being able to use a single github repo to host multiple Cloud Functions with their respective Cloud Build triggers set up for easy deployment.