How much is deployed while deploying a specific firebase cloud function? - firebase

If I have a firebase cloud function like this:
exports.exampleFunction = function (admin, envKey) {
return functions..https.onRequest((req, res) => {
doSomething(req)
}
}
And I modify the doSomething function and deploy exampleFunction with:
firebase deploy --only functions:exampleFunction
Will the modifications to doSomething be included in the deployment? This is a trivial example - but more generally - does Firebase track the dependencies of a function when it is deployed? Or do I need to deploy all functions in order to push modifications to dependencies?

Firebase has no way to introspect the workings of your code to understand what has changed or hasn't, so every deploy deploys all files in the functions directory to be rebuilt. Using the --only flag controls which functions will actually be updated, but the same source code is used to build and deploy each function regardless of flags.

Related

Will Firebase replace all my cloud functions?

In our organization, we have a repo for all cloud-functions. They are all in Python. We've had to create another cloud-function and we've decided to write it in NodeJS.
This new cloud-function is in a new repo, in a new project. There's no relationship between the old functions and the new one.
I've read that if I use firebase deploy --only functions, it will deploy all functions in the index.js file, but I'm not sure if it will remove the other ones, since there is nothing in my index.js file about the other cloud-functions written in Python.
Is it safe to deploy my new function with firebase deploy --only functions? I don't want to replace the other functions, just add my new function.
Thanks!
If you run firebase deploy or firebase deploy --only functions, Firebase will likely replace all the Cloud Functions in the project.
If you want to only deploy a single function, or a group of functions, you can specify that as an additional argument:
firebase deploy --only functions:yourNewFunction
Also see the Firebase documentation on deploying specific functions.
I just tested this out, whenever deploying functions by using firebase deploy or firebase deploy --only functions and the index.js does not find the name of a function it will display this message:
The following functions are found in your project but do not exist in your local source code:
<function_name>(<region>)
If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss. For more info, visit https://firebase.google.com/docs/functions/manage-functions#modify
If a function has the same name as one of the Cloud Functions already deployed it will replace the older function. But if it has a different name it should not be replaced and you should be safe. If you still feel unsure feel free to test this out with quick start functions.

Firebase emulator for Cloud Functions not updating the code

I'm deploying the following cloud function using firebase deploy --only functions :
export const testFunction = functions.https.onCall((data, context) => {
return {"data": "hello"};
});
and when call it from the client app using the code
var testFunction = firebase.functions().httpsCallable("testFunction");
testFunction().then((result) => {
// Read result of the Cloud Function.
this.data = result.data;
});
it works as expected.
Now, I want to continue developing the function testing it on the local emulator so, following the documentation, I added this line to the web app code (before the function)
firebase.functions().useEmulator("localhost", 5001); // for local simulator
and I run the local emulator with:
firebase emulators:start --only functions
If I run the client app now, I correctly see the call going through the local emulator instead of the remote cloud function.
Problem: If modify the code the local function doesn't get updated. I need to run firebase deploy again in order to see the change in the response. How can I just deploy locally?
This stackoverflow solves the issue.
Solution in short:
After every change in your code, run npm run build to recomplie the code.
To auto-compile for every change in your code.
Change "build": "tsc" to "build": "tsc -w" in your package.json
file, and run the emulator in an another terminal window.
You can use the functions shell as described in this documentation here in order to deploy, run and test functions locally. I find the documentation quite confusing, as it only mentions how to use .sh scripts to test locally.
firebase functions:shell
Note that if you use the shell, you don't need to run the emulators separately; the shell will run them for you. The shell will automatically update based on changes to the index.js functions file, but if you're using TypeScript you may have to rebuild after you make changes. This is typically done by going into the functions directory and running npm run build. You can also automate this as well, according to this answer, but I haven't tried this approach yet.

How do you deploy to Firebase without Firebase functions?

I experimented with firebase functions for the init part and it has created a folder for me. Now when I deploy each time it also picks up the functions folder. When I remove the functions folder I get an error, when deploying. How do I deploy everything but functions?
firebase deploy has a parameter --except . So to deploy everything except functions you can run:
firebase deploy --except functions
You can use the only option/flag, for example firebase deploy --only hosting,storage, see the CLI doc.
You can also use deploy targets which "are short-name identifiers (that you define yourself) for Firebase resources in your Firebase project".
Check to make sure that you have removed the implicit reference to the function in your index.js and then running firebase deploy should do the trick. If you want to explicitly delete the function completely, you can use the firebase functions:delete <myFunction> and then delete the functions folder. You can find more details in the doc here

Firebase deploy only functions process

If I have two Firebase functions (fire1 and fire2) that share a helper function (doSum), will deploying with firebase deploy --only functions:fire1 change the environment of fire2?
No, each function is fully indepndent of each other. Each deployed function gets its own copy of the entire deployed project. A deploy of a single function will not in any way affect the code deployed for any other function.

How to deploy some functions to Cloud Functions for Firebase without affecting some other functions?

When I run
firebase deploy --only functions
it reads the index.js file and updates all functions exported from that file. If on the previous deploy there was a function named a, and in the current deploy there is no such function, a will be deleted.
In other words, the effect is the same as if all existing functions were deleted and then the all functions from the current index.js file were added.
Is it possible to add/update/delete individual functions?
Firebase CLI tools 3.8.0 has added the ability to deploy specific functions.
firebase deploy --only functions:func1,functions:func2
--only <targets>
only deploy to specified, comma-separated targets (e.g. "hosting,storage"). For functions,
can specify filters with colons to scope function deploys to only those functions (e.g. "--only functions:func1,functions:func2").
When filtering based on export groups (the exported module object keys), use dots to specify group names
(e.g. "--only functions:group1.subgroup1,functions:group2)"
The following way worked for me to deploy a particular function without affecting my other functions, where specificFunctionName is the function I wanted to deploy
firebase deploy --only functions:specificFunctionName
To copy the command quickly in terminal:
firebase deploy --only functions:
firebaser here
There is currently no way to deploy a single function with the Firebase CLI. Running `firebase deploy` will deploy all functions.
We've recently discussed deploying subsets of the functions, but it's not available at the moment - nor can we give a ballpark of if/when it might be.
Update Since Firebase CLI release the ability to deploy single functions is available. See yuku's answer.
firebase deploy --only "functions:<fileName>.<functionName>"
example folder structure:
functions
node_modules
index.js
smsNotification.js
...
You can redeploy just a function in a file with
firebase deploy --only "functions:smsNotification.sendChatNotif"
You can redploy all functions in a file with
firebase deploy --only "functions:smsNotification"
I never managed to get it working (and have no codebase property in my firebase.json) until I tried what #Sergey Mell mentioned in his comment :
firebase deploy --only "functions:func1,functions:func2"
The surrounding double quotes was what solved the issue.
In case anyone still can't make it work using firebase deploy --only functions:func1,functions:func2, it's probably because you're probably using codebases, "codebase": "my-codebase", in your firebase.json. Took me a while to diagnose but after removing that codebase property, deploying only some functions using the --only flag worked for me
This one gave me the missing package. Although package was included but I had to reinstall the mentioned package.
firebase --debug deploy --only functions:[functionName]

Resources