Retry on failure Firebase Function deploy failing using Github Action - firebase

I have a firebase function that is triggers on firestore doc OnCreate with failurePolicy set to true like so:
const webhookHandler = functions
.runWith({
failurePolicy: true,
memory: '512MB',
timeoutSeconds: 60,
}).firestore.document(`${COLLECTIONS.eventsQueue}/{event_queue_id}`)
.onCreate(async (snap, context) => {
//some code here...
})
Locally, using Firebase CLI i can deploy successfully, but requires I acknowledge that this function will retried on failure, do you still want to deploy? Yes/No.
once Yes is selected the function deploys successfully.
I'm using Github Actions to deploy the firebase functions automatically on PR merge. and Once i added the above function the github action deploy fails.
My assumption is that github action deploy is failing because the prompt.
My github deploy github action script does have --non-interactive and also tried with --force and it keeps failing.
Seems the --force does not enforce on this function deploy locally or in github deploy. Any suggestions to resolve this.

You can use the yes command included with most linux distros.
yes | firebase deploy ...

credit to #nVitus helped solve this.
the exact script that solves it:
yes | firebase deploy --only functions --force

Related

Firebase task function failed to deploy

I havae the following firebase cloud function codes and want to deploy to my project.
exports.bgTask = functions.region(Defs.SERVER_AREA)
.tasks.taskQueue({
retryConfig: {
maxAttempts: 5,
minBackoffSeconds: 60
},
rateLimits: {
maxConcurrentDispatches: 6
},
}).onDispatch(async (data) => {
});
Obviously, the above code just copied from firebase cloud function sample and it can be run under emulator, but when I tried to deploy to server and had no luck.
I tried to run firebase functions:log command and see the following error log.
So, it seems that Firebase cli cannot recognize the taskQueue property.
I googled a lot and had no luck. The following screen shot shows the corresponding library what I am using.
Anyone has any ideas on this?
Thanks in advance!
As mentioned by #Xavier, He managed to fix the issue by creating a new project and initialize Firebase function and deployed successfully by including the packages that are needed and run the command npm install --save firebase-functions#latest for the latest version of Firebase Functions.

How can I deploy a single Firebase Cloud Function in a group and a region?

I have created a group of Firebase Cloud Functions (v2) that are deployed in a region (europe-west1).
#index.ts
import * as apiV2 from './v2';
export const v2 = apiV2;
#v2.ts
export const addTextMessage = functions.region('europe-west1').onCall(
...
)
I want only to deploy the addTextMessage function.
I tried:
firebase deploy --only functions:v2-addTextMessage
# or
firebase deploy --only "functions:v2-addTextMessage(europe-west1)"
However the function is not deployed:
✔ functions: functions folder uploaded successfully
i functions: current functions in project: v2-addTextMessage(europe-west1)
⚠ functions: the following filters were specified but do not match any functions in the project: v2-addTextMessage(europe-west1)
What command should I use?
Try to replace the "-" by ".".
You should use :
firebase deploy --only functions:groupName.functionName
In your case :
firebase deploy --only functions:v2.addTextMessage
It's true that the CLI terminal log is misleading because if you've exceeded your deployment quota and the CLI detects that the name of your function is for instance v2-addTextMessage(europe-west-1), it will print a message suggesting you to use the command firebase deploy --only functions:v2-addTextMessage to deploy this function only, which doesn't work.
See the full Firebase CLI documentation here
You are using the correct command, however, you have not exported the addTextMessage function to your index.ts file, without that the deployment cannot find the funtion to deploy. You can export it by adding the following code to your index.ts:
export const v2-addTextMessage = apiV2.addTextMessage
Also, you cannot use the functions parameter and the function name as a String. So your command on this case would have to be:
firebase deploy --only functions:v2-addTextMessage
For Specifying region on deployment, as you already added to your code on the edited version of the question, you cannot do it on the FirebaseCLI command, thanks to #Doug Stevenson for pointing that out on the comment section.
Ideally, as you can see on this video, you would have to specify that in your cloud function code, before deployment by adding the following:
exports.v2-addTextMessage = functions
.region('europe-west1')
.storage.object().onFinalize((object) => { });

Firebase CLI: the following filters were specified but do not match any functions in the project

I use Firebase Cloud Functions in my project, and I have a plenty of them so when I run the regular firebase deploy I exceed the quota, so one option is to deploy only the function that was modified by using firebase deploy --only functions:function1 as mentioned in this web page. This method works only with functions that start with: exports.funcName but when I try to use it with a function like this:
function doesNotStartWithExports() {
// Does something.
}
It doesn't work. I use firebase deploy --only functions:doesNotStartWithExports but I get this output:
⚠ functions: the following filters were specified but do not match any functions in the project: doesNotStartWithExports
The Question: How to deploy Firebase functions that does not start with exports?
I faced a very similar error while trying to delete some deprecated functions:
firebase functions:delete mymodule-helloWorld --region us-central1
Output:
Error: The specified filters do not match any existing functions in project my-firebase-project.
Turns out that if I replace the '-' in namespaced/grouped (module) functions with '.', the error goes away. Weird.
firebase functions:delete mymodule.helloWorld --region us-central1
Output:
? You are about to delete the following Cloud Functions:
mymodule-helloWorld(us-central1)
Are you sure? Yes
i functions: deleting function mymodule-helloWorld(us-central1)...
✔ functions[mymodule-helloWorld(us-central1)]: Successful delete operation.
The solution is adapted from this github thread
I actually found the solution, and it's by deploying one of the function that starts with exports and uses the function that doesn't start with exports, for example:
function doesNotStartWithExports() {
// I want to deploy only this function but I can't
}
exports.anotherFunction = functions.https.onRequest((request, response) => {
// This functions uses the one that I want to deploy.
doesNotStartWithExports()
})
To update doesNotStartWithExports I use this command:
firebase deploy --only functions:anotherFunction.
I also found that if you have a regular function exported somewhere with the same name as the Firestore function you will get this error:
export myFunction() {
// do somethig here
}
exports.myFunction = functions.https.onCall((data, context) => {
// function
})
renaming either of them to a unique name will solve the issue.
This works:
But this does not work (extra lines preceding function)

Deploying cloud functions to firebase successful in the CLI but none appear in the dashboard

I've been following the codelabs tutorial here to deploy my first functions to firebase. I've made it to step 8 of the tutorial ("Welcome new users").
The deploy looks successful when I run firebase deploy --only functions from within the functions subdirectory:
Marks-MacBook-Air-3:functions mf$ firebase deploy --only functions
=== Deploying to 'friendlychat-21221'...
i deploying functions Running command: npm --prefix "$RESOURCE_DIR"
run lint
functions# lint /Users/mf/Desktop/friendlychat-web/cloud-functions-start/functions
eslint .
✔ functions: Finished running predeploy script. i functions:
ensuring necessary APIs are enabled...
✔ functions: all necessary
APIs are enabled
i functions: preparing functions directory for
uploading...
✔ Deploy complete!
But looking at my firebase dashboard, it doesn't look like they deployed after all:
I'm not even sure where to begin troubleshooting, since the logs in the cloud functions tab is empty.
Has anyone encountered this before and/or have a good troubleshooting strategy?
Update 1:15 PM Friday 25 May, 2018: This is my index.js file in the functions subdirectory:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// TODO(DEVELOPER): Write the addWelcomeMessages Function here.
// Adds a message that welcomes new users into the chat.
exports.addWelcomeMessages = functions.auth.user().onCreate(user => {
console.log('A new user signed in for the first time.');
const fullName = user.displayName || 'Anonymous';
// Saves the new welcome message into the database
// which then displays it in the FriendlyChat clients.
return admin.database().ref('messages').push({
name: 'Firebase Bot',
photoUrl: '/images/firebase-logo.png', // Firebase logo
text: `${fullName} signed in for the first time! Welcome!`, // Using back-ticks.
}).then(() => {
console.log('Welcome message written to database.');
});
});
// TODO(DEVELOPER): Write the blurOffensiveImages Function here.
// TODO(DEVELOPER): Write the sendNotifications Function here.
Here are the contents of the functions subdirectory:
I ended up getting it to work after changing two things (and I'm not sure which one fixed this issue; perhaps both):
run npm install from inside the functions directory before you deploy the functions (which should be done in the parent directory).
Be aware that when you run firebase init and cause an overwrite of your index.js file, that file may just have a commented out 'helloWorld' function...
Try Firebase list to list all the project that you have in firebase under the signed in account and see if the project you are deploying to shows up. In the case where it doesn't show try this:
firebase logout && firebase login
firebase list
firebase use <alias_or_project_id>
firebase deploy --only functions or firebase deploy --only functions:<function_name>
Hope this helps!

Firebase cloud function deploy error

irregularly my firebase deployment get stuck at this log:
i functions: updating function [FUNCTION NAME]...
After canceling the deploy and retrying it throws the following error message:
⚠ functions: failed to update function resetBadgeCount
⚠ functions: HTTP Error: 400, An operation on function [FUNCTION NAME]
in region us-central1 in project [PROJECT NAME] is already in progress.
Please try again later.
So it seams like that the deploy got stuck and kept in the pipeline blocking further deploys. After a while it let me deploy the functions normally again.
But is there an explanation for this? Or maybe even a word around?
Go to Google cloud functions console and see if there is red exclamation mark against your function. Then select that particular function and try to delete. once it gets deleted from there, you can deploy again successfully. if it is showing spinner, then wait till it shows red mark.
Try this
You can fix the issue much easier by examining the actual logs using this command to open the log
firebase functions:log
The specific issue will be visible there. I sometimes even had errors as simple as a missing package in package.json
You can temporarily rename your function:
$ firebase deploy --only functions
...
i functions: deleting function onSameDataChanged...
i functions: creating function onSameDataChanged1...
...
✔ functions: all functions deployed successfully!
✔ Deploy complete!
Comment or cut your function
Deploy
Uncomment or paste back the function
Rename the function
Deploy
Rename the function back
Deploy
also you can wait a few minutes and you will get an error with {"code":10,"message":"ABORTED"}, then you can deploy again.
just copy your index.js to some where else and delete function form firebasa function console
firebase init -and overe write all file again
past index.js text again
deploy...
For me it was the node version. Turns out I had the 15.x on my machine and the 12.x on the server. Just updating it solved my upload issue
Make sure you've installed dependencies in the functions directory.
for more information about you function you can go to this page
Set your directory to your project directory \functions then run this command:
npm install -g firebase-tools

Resources