I have some GCP Cloud Functions that belong to the same API system, so I've grouped them within the same GitHub repo.
My whole setup looks like this:
GitHub repo: source of truth where all the API cloud functions are. All the functions in Typescript inside /src
and the compiled JS into /out
Mirrored GCP Cloud Source Repository: Same GitHub repo data, but on GCP (this is necessary for the cloud functions)
GCP Cloud Functions: with their Source
set to the Cloud Source Repo
, their Directory with source code
to their compiled JS within the /out
dir of their package name within the GitHub repo and their own Entry point
. For example:
func1
func2
Cloud Build Trigger: listening to the changes of the GitHub's repo master
branch that has this .yaml
file:
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'functions'
- 'deploy'
- '--trigger-http'
- '--entry-point'
- 'func1'
- '--runtime'
- 'nodejs20'
dir: 'out/func-1'
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'functions'
- 'deploy'
- '--trigger-http'
- '--entry-point'
- 'func2'
- '--runtime'
- 'nodejs20'
dir: 'out/func-2'
The problem is, when I push to master
, I am re-deploying all the functions, regardless if one changed, but the other didn't. The flow goes like this:
master
branchmaster
branch was updated, so does the Cloud Source Repo
's master
branch, because it's mirrored.master
branch was updated, the Cloud Build Trigger is fired and runs the commands of the .yaml
which re-deploys the all Cloud Functions with the new code from the Cloud Source Repo
So my question would be on step 3, how do I set the .yaml
file to change only the functions that changed in the PR? Is there another approach to this problem? Maybe GitHub actions or Substitution variables for the Cloud Build? Not sure how to set either.
I would really like to maintain the setup, so all the related cloud functions stay within the same repo.
One way you could do it is a build trigger for each function. Set a filter for the Cloud Functions directory then instead of a yaml file, use "Inline" to use just the targeted function's build steps. It's not ideal, but it should work.
EDIT: Instead of using "Inline" you could also have a yaml file for each function. If you are creating separate build triggers per function, you can specify a different yaml file location too.