2

I would like to setup one GitHub repo which will hold all backend Google Cloud Functions. But I have 2 questions:

  1. How can I set it up so that GCP knows that there are multiple Cloud Functions to be deployed?
  2. If I only change code for one Cloud Functions, how can I setup so that GCP will only deploy the modified Cloud Functions, and NOT to redeployed the unchanged onces?
Cupid Chan
  • 496
  • 4
  • 15
  • The simple answer is you cannot. Cloud Functions does not try to figure out what changes were made to a repository. Update the repository and the entire function(s) are redeployed. Your question lacks details on how you are deploying Cloud Functions and the CD pipeline that you created. Tip: You can develop common **Functions** code on your desktop. Deploy separate repositories for each function. – John Hanley Jul 31 '21 at 19:12
  • When using a mono-repo, you might have to integrate a tool like bazel into your pipeline to split out independent builds – Cadet Aug 01 '21 at 13:26
  • @JohnHanley, I am planning to use GCP Cloud Build for the CI/CD pipeline so that once it will kick off the build when there is a new check in. – Cupid Chan Aug 01 '21 at 14:46

2 Answers2

2

When saving your cloud function to a github repo just make sure to have in the gitignore file the proper settings. Exclude node_modules and such folder and files you don't want to commit.

Usualy all cloud functions are deployed trough a single index.js file. So here you need to make sure you have that and all files you import into it.

If you want to deploy a single function you can use this command:

firebase deploy --only functions:your_function_name

If you want to have a more structure solution you can read this article to learn more about it.

Tarik Huber
  • 7,061
  • 2
  • 12
  • 18
  • 1
    I am planning to use GCP Cloud Build for the CI/CD pipeline so that once it will kick off the build when there is a new check in. But I don't want the build to redeploy 10 functions, if I only modified one. – Cupid Chan Aug 01 '21 at 14:48
  • If you deploy a function as explained in the answer only that one will be deployed and the other will stay as they are (they won't be redepoyed) – Tarik Huber Aug 01 '21 at 15:31
  • your article is very well written and clear! It explains how should the index.js be formed to automatically get all functions in place. However, let say I have 10 functions and they all deployed in Firebase. Now if I change only ONE function and check in, Cloud Build will still deploy all 10 functions, right? If so, how can configure so that it will only deploy the modified one? – Cupid Chan Aug 02 '21 at 10:56
  • If you run the command `firebase deploy --only functions` it will deploy all (but I think that Firebase checks if there are changes and don't deploy those that don't have any changes - but I'm not 100% sure). If you run `firebase deploy --only functions:your_function_name` it will deploy only that function and the other would stay untouched. If you want to automate that process you would need to setup your CD system to detect witch function has changed and run those commands only for the changed functions. – Tarik Huber Aug 02 '21 at 11:30
  • Thanks @Tarik Huber, that makes perfect sense. Is there any way to check if Firebase will not deploy unchanged functions? Also if CD is really required, do you have any materials how to set that up? – Cupid Chan Aug 02 '21 at 14:32
  • Here is an example I use in all of my web projects: https://github.com/TarikHuber/react-most-wanted/blob/master/packages/rmw-shell/cra-template-rmw/template/.travis.yml it is made with Travis. – Tarik Huber Aug 02 '21 at 19:39
1

As I understand, you would like to store cloud functions code in GitHub, and I assume you would like to trigger deployment on some action - push or pull request. At the same time, you would like only modified cloud functions be redeployed, leaving others without redeployment.

Every cloud function is your set is (re)deployed separately, - I mean the for each of them you might need to provide plenty of specific parameters, and those parameters are different for different functions. Details of those parameters are described in the gcloud functions deploy command documentation page.

Most likely the --entry-point - the name of the cloud function as defined in source code - is to be different for each of them. Thus, you might have some kind of a "loop" through all cloud functions for deployment with different parameters (including the name, entry point, etc.).

Such "loop" (or "set") may be implemented by using Cloud Build or Terraform or both of them together or some other tool.

An example how to deploy only modified cloud functions is provided in the How can I deploy google cloud functions in CI/CD without redeploying unchanged SO question/answer. That example can be extended into an arbitrary number of cloud functions. If you don't want to use Terraform, the similar mechanism (based not he same idea) can be implemented by using pure Cloud Build.

al-dann
  • 2,545
  • 1
  • 12
  • 22
  • I think your shared post aligns with the direction I want to achieve. However, there are still some details I am missing. Let's say I have 10 Cloud Functions in a GitHub repo and in this check in I have only 1 modified function. If I deploy manually, of course I know what deploy command I need to run for that one function. But if I check in in GitHub and rely on a trigger for Cloud Build to deploy, how can Cloud Build know which one is the modified one? – Cupid Chan Aug 02 '21 at 11:28
  • I think there are a few options. Depends on you context and requirements. For example - (a) handle relevant inclusion mask in a cloud build trigger (but it means that for each cloud function you need a dedicated trigger); (b) use TF sets and lists (i.e. instead of one zip file - you get a set of zip file with cardinalities according to the number of cloud functions). – al-dann Aug 02 '21 at 14:20
  • Thanks @al-dann, but option a) will not be scalable. Can you elaborate a bit on option b) please? The requirement is, the build process (Cloud Build) can automatically detect which functions has changed and which ones don't, so that it will only deploy the modified function. – Cupid Chan Aug 02 '21 at 14:36
  • As we don't know in advance, which cloud functions (from the whole manifold) are modified, there should be a "process" to go though all of them (in terms of code) and check. To go through all of them - means there should be a variable (for example) - a set of their names. So we go through that set - (1) a set of zip archives, then (2) a set of SHA hashes, so we get a set of GCS names, then (3) upload all of them (as a terraform set again) into a bucket. Finally - TF cloud function is also a set... - this is very shortly. The drawback - very little room for individual configuration of each CF – al-dann Aug 02 '21 at 14:48
  • Thanks @al-dann! So, what's the best practices for handling this? Is that normal for organization to just redeploy ALL Cloud Functions, even though majority of them were not changed? – Cupid Chan Aug 05 '21 at 11:37
  • Not at all... In my experience - the Cloud Build runs Terraform. And Terraform checks which clud functions were modified (either code or other parameters), and redeploys only those, which have been modified. – al-dann Aug 05 '21 at 12:23
  • Thanks again @al-dann! Do you have any chance know where can I find some sample of those Terraform code as an example? – Cupid Chan Aug 05 '21 at 20:10