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
/srcand the compiled JS into/outMirrored GCP Cloud Source Repository: Same GitHub repo data, but on GCP (this is necessary for the cloud functions)
GCP Cloud Functions: with their
Sourceset to theCloud Source Repo, theirDirectory with source codeto their compiled JS within the/outdir of their package name within the GitHub repo and their ownEntry point. For example:- func-1
- Directory with source code: /out/func-1
- Entry point:
func1
- func-2
- Directory with source code: /out/func-2
- Entry point:
func2
- func-1
Cloud Build Trigger: listening to the changes of the GitHub's repo
masterbranch that has this.yamlfile:
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:
- A PR is approved and merged into
masterbranch - Because the GitHub's repo
masterbranch was updated, so does theCloud Source Repo'smasterbranch, because it's mirrored. - Also, because the GitHub's repo
masterbranch was updated, the Cloud Build Trigger is fired and runs the commands of the.yamlwhich re-deploys the all Cloud Functions with the new code from theCloud 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.