How to update in firebase cloud function source code - firebase

I have a firebase project which has cloud functions, i don't have the original source code and i want to update in the source code.
i tried to follow the documentation of firebase but it create new folder, i don't know how to get the current source code to update it then deploy it.
also i get the source code using "https://console.cloud.google.com/functions" but can't know how to deploy the code after update on it.

Here is the solution which i follow:
Go to the Cloud Console > Cloud Functions list.
Select any of the functions.
Inside the function's detail, click on the Source tab.
Then at the bottom, you'll see the "Download.zip" button.
Then you can make changes,i.e add new function >> add the code for that function and update the index.js (append your work on it to avoid deleting the old functions) then follow the firebase documentation to deploy only one function

Related

Firebase functions change names when deploying

I have been working with functions for a while, recently I made a function called "unPublishStore" in my index.js file.
When calling firebase deploy --only functions every thing runs flawlessly, but the function's name gets changed, where in the cmd it shows the following:
and in the firebase's functions list the name is also changed:
This feels very much like a typo error.
Please check you index file, ensure the definition name on the left side:
exports.functionNameToDeploy = functionYouWrote;
The functionNameToDeploy will exactly be the name shown in cloud instead of the functionYouWrote
Probably you can search globally in your project to see if there is a function called upPublishStore?

Firebase deploy failed even without changing the function from onCallable to onRequest

Error: [functionName(us-central1)] Changing from a callable function to an HTTPS function is not allowed. Please delete your function and create a new one instead.
Any advice and insight is appreciated.
Which version of Firebase CLI (firebase --version) are you using? Last night I updated firebase-tools package to 10.3.0 and functions deployment started giving me the error you mention. I downgraded to 10.2.2 and functions deployment started working as before.
Update:
Firebase team confirmed there is an issue with 10.3.0 firebase-tools. They are working on a fix:
https://github.com/firebase/firebase-tools/issues/4307
Solution 01:
I think the main problem lies inside whether you are using a valid way to use .env file. Since I just recently have this kind of error. If you do use the .env file inside the structure of your folder, then
You need to declare the path to .env file in all files which uses process.env.VARIABLE_NAME. Example way is like this:
require("dotenv").config({path: "../.env"});
After that try to delete all existing Cloud Functions in Google Cloud Console
Try deploy again: firebase deploy --only functions
I added a path to the .env file, since I checked Firebase Cloud Function Logs and it gave me error for all Cloud Functions which uses process.env.VARIABLE_NAME. This .env file must be located inside the root project and placed inside folder functions/. Give it a try. Hope it works.
Solution no. 2:
You should check if there is a variable or function which doesn't give return value, like example below:
const key = async () => {
const response = await pk.accessSecret("PRIVATEKEY");
return response;
};
I forgot to add a return value from the variable const key. Therefore I get so many errors like you do in all of my functions. And that errors cause firebase cli to state"
Changing from a callable function to an HTTPS function is not allowed.
Please delete your function and create a new one instead.
this happened to me as well. npm i -g firebase-tools#latest did the trick.

Firebase cloud function inline code editor

I'm trying to reduce the cold start time on my firebase cloud function. I have around 30 functions that use different imports.
As in an info video mentioned, it's better to use only the imports that your cloud function needs.
In the google cloud console, you can view your code.
But if I scroll down the LIB/INDEX.JS contains all my functions.
There's an option to edit the code.
Would it be harmful to delete all other functions & the imports that aren't used (for that specific function in LIB/INDEX.JS) with the inline code editor? (Even though I made my functions via typescript in visual studio code).
Thanks!
No, it would not be harmful to the other functions. If you are editing the code of a Cloud Function in the console, it will not modify the code used by other Cloud Functions. Each function is fully isolated from each other, even if they share the same deployment. The code is copied between each function.
That said, editing functions deployed by the Firebase CLI should only be done in experimentation. When it comes time to actually deploy code, you should again use the CLI to finalize everything.

CLI question: "The following functions are found in your project but do not exist in your local source code" - how to default answer No?

Everytime I deploy a function, I have the annoyance that I need to answer the question:
The following functions are found in your project but do not exist in your local source code
with "No".
I have the following structure of my functions. I have a folder with the Typescript functions and then a different folder with the Javascript functions. They are in separate folders, because I am too dumb to get it to work in the same folder.
I initially created both folders using the Firebase CLI and choosing Typescript and Javascript respectively.
I am aware of the --force operator, but I think it will force "Yes" and I don't want the functions to be deleted.
As detailed in the documentation:
You could only target the specific functions you want to deploy, like:
$ firebase deploy --only functions:function1,functions:function2
Or you could group your Cloud Functions into export groups in your index.js/index.ts files.
Grouping functions allows you to deploy multiple functions using a
single command.
See the same doc for more detail.
And yes, the --force operator bypasses the confirmation prompt, but it is the confirmation prompt for deletion, not for deployment (the --force operator is to be used with functions:delete). Therefore it will not help in your case.

How do you un-deploy Cloud Functions for Firebase?

I was experimenting with Cloud Functions for Firebase and deployed one of the samples in the functions-samples repo and linked it to an existing project using firebase use --add.
Since I was just playing around, these files were not added to my version control, and I no longer have access to the machine this was done on. I would like to build on this function or create new ones, but I am unsure how to either 1) modify and re-deploy over the original function or 2) un-use --add and start fresh. I see the function in my firebase console but no 'delete' or 'remove' button and have read through the CLI docs looking for clues to no avail.
Thanks for adding functions to the firebase 'stack.' Any guidance is much appreciated.
Functions get deleted when they aren't present during a firebase deploy. Most commonly that'd be because because you removed the function from your index.js, but if you've removed the whole functions directory that'll work too.
To continue work on a function for which you don't have the source anymore, the easiest is to start fresh. The function(s) you deploy will replace the ones you deployed previously.
Alternatively, if you're partial to using the Google Cloud Console instead of the Firebase Console, the Cloud Console will show you the code for the currently-deployed function, so you can copy-paste it onto your local machine.
The Cloud Console also has a 'delete' button for every function, and even a web editor. Be aware when editing functions from the Cloud Console though: your next firebase deploy will overwrite any changes.
To delete a function explicitly use the following command:
$ firebase functions:delete myFunction
Replace myFunction with your function name. For detailed info. follow this link: https://firebase.google.com/docs/functions/manage-functions
Go to Google Cloud Console Cloud Functions and select project, then select the function you want to undeploy or delete. Select it and click delete.
To delete/undeploy a single function, you can delete the code for your function and then run the following in command line:
firebase deploy --only functions:YourFunctionName
Replace YourFunctionName with the name of your function
In the case that you are working on other functions that you are not ready to deploy or do not want to deploy all of your functions for any reason, then the code above can be handy. This also feels a bit safer since you're not redeploying everything :)
Now you can simply delete a function from the function dashboard as follows. Click on the three dots mark of relevant function in the right side corner.

Resources