I've been away from firebase for about 2 years and just got back trying to fix couple of things in my firebase-functions. There was a plenty to update with my packages, but I got to the point where I am not sure what to do next to deploy the fixed function.
Here is the error that happens after trying to deploy a function in terminal:
lukasstateczny#Lukass-MacBook-Pro admin-pukinn % firebase deploy --only functions:mpiEnrollment
=== Deploying to 'pukinn2'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> tslint --project tsconfig.json
Tried to lint /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/functions/src/index.ts but found no valid, enabled rules for this file type and file path in the resolved configuration.
Running command: npm --prefix "$RESOURCE_DIR" run build
> build
> tsc
✔ functions: Finished running predeploy script.
i functions: preparing codebase default for deployment
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error: Cannot find module '../config/pukinn2-1b14e9a65ea1.json'
Require stack:
- /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/functions/lib/index.js
- /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/node_modules/firebase-functions/lib/runtime/loader.js
- /Users/lukasstateczny/Desktop/Pukinn/admin-pukinn/node_modules/firebase-functions/lib/bin/firebase-functions.js
lukasstateczny#Lukass-MacBook-Pro admin-pukinn %
Now the config folder exists, but there is no file with this name. Also I'm not sure about these weird numbers after the app name, but I guess it's probably something like a cashed file. I don't know at this point how to get this file and what values should be in it.
ANSWER: After a few helpful comments I realize I was missing serviceAccount.json file that can be found in Firebase Console -> App Settings -> Service Accounts [Tab]
I thought first firebaseConfig was enough but I was wrong.
Also couple of dependencies were outdated, so I needed to upgrade that as well, but after including the .json file the deployment was smooth as ever.
Service account template in case you are not sure how it should look:
{
"type": "service_account",
"project_id": "yourProjectName",
"private_key_id": "...a89fds980asddfs890dsa...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...ahjk8123hdjak91hjkdsa...\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-0000f#yourProject.iam.gserviceaccount.com",
"client_id": "...1234567...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/000/firebase-adminsdk-0000%40yourproject.iam.gserviceaccount.com"
}
Obviously make sure not to share real values with anyone.
I would try removing your .firebase folder which may contain a very old hosting cache file and retry the deployment if you are able to build the app locally just fine.
Additionally, prior to deploying those functions, I would verify in development that they are working as you intend using your Functions Emulator. In the React client you obviously need to use the connectEmulator function for development to be able to test you functions if you haven't already.
You also may want to go inside your functions folder, remove the build folder, run npm run build and ensure your functions are building just fine.
Related
I am trying to run a github actions workflow on each push to the main branch. The workflow is supposed to deploy firebase cloud functions (in the /functions dir).
The whole workflow gets triggered by the push correctly and runs without errors, but the functions do not get deployed.
deploy-cloud-functions-merge.yml
name: Deploy Cloud Functions
on:
workflow_dispatch:
push:
branches:
- main
paths:
- 'functions/**'
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- uses: actions/setup-node#v3
with:
node-version: 16.x
- name: Build Cloud Functions
run: cd functions && npm ci
- name: Create SA key
run: echo '${{ secrets.CLOUD_FUNCTIONS_SERVICE_ACCOUNT }}' > $HOME/gcloud.json
- name: Deploy Cloud Functions
run: export GOOGLE_APPLICATION_CREDENTIALS=$HOME/gcloud.json && cd functions && npx firebase-tools deploy --only functions --project default --json && npx firebase-tools --version
The output in github actions from the Deploy Cloud Functions step:
Run export GOOGLE_APPLICATION_CREDENTIALS=$HOME/gcloud.json && cd functions && npx firebase-tools deploy --only functions --project default --json && npx firebase-tools --version
npm WARN exec The following package was not found and will be installed: firebase-tools#11.23.0
npm WARN deprecated uuid#3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
npm WARN deprecated request#2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator#5.1.3: this library is no longer supported
npm WARN deprecated debug#4.1.1: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
> lint
> eslint
11.23.0
So after the predeploy script (lint + eslint), there is no output, but also no error.
When I run firebase deploy --only functions --project default on the same project locally, this is what the output looks like:
=== Deploying to '[projectname]'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint
✔ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing codebase default for deployment
⚠ functions: package.json indicates an outdated version of firebase-functions. Please upgrade using npm install --save firebase-functions#latest in your functions directory.
i functions: preparing functions directory for uploading...
i functions: packaged /home/stanley/[projectname]/functions (82.34 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: updating Node.js 16 function getFileParts(europe-west1)...
i functions: updating Node.js 16 function getCustomMeta(europe-west1)...
i functions: updating Node.js 16 function getCSVParts(europe-west1)...
i functions: updating Node.js 16 function getTemplates(europe-west1)...
i functions: updating Node.js 16 function getCSVLink(europe-west1)...
✔ functions[getCustomMeta(europe-west1)] Successful update operation.
✔ functions[getTemplates(europe-west1)] Successful update operation.
✔ functions[getCSVParts(europe-west1)] Successful update operation.
✔ functions[getCSVLink(europe-west1)] Successful update operation.
✔ functions[getFileParts(europe-west1)] Successful update operation.
Function URL [functionurl]
Function URL [functionurl]
Function URL [functionurl]
Function URL [functionurl]
Function URL [functionurl]
i functions: cleaning up build files...
✔ Deploy complete!
Project Console: [console-link]
I am using --project default, to fix a previous error:
***
"status": "error",
"error": "No project active, but project aliases are available.\n\nRun \u001b[1mfirebase use <alias>\u001b[22m with one of these options:\n\n default ([projectname])\n staging ([projectname])"
***
this is what my .firebaserc looks like:
{
"projects": {
"default": "[projectname]",
"staging": "[projectname]"
},
"targets": {
"[projectname]": {
"hosting": {
"[projectname]": [
"[otherprojectname]"
]
}
}
},
"etags": {}
}
I am basically out of solutions and would appreciate any suggestions.
When I run firebase deploy I get the following output:
> build
> tsc
✔ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing codebase default for deployment
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error: username is required
Which is odd because the same command has been working for weeks. Is there some "username" field that I need to put into firebase.json or something?
The proper way to initialize Firebase within a Cloud Function would be to use the firebase-admin client as shown here.
I would recommend you try using the below while initializing the app and see if this works:
import {initializeApp} from "firebase-admin/app";
Another thing to check are the firebase function dependencies whether they are still built intact.
As seen per your code, it looks like you're trying to use the Cloud Functions for TypeScript, and npm run build -- --watch takes care of updating the built code as you are coding. Cloud Functions would run with the built JS code and not the TypeScript code.
The problem could be that the setup at the entry point of Cloud Functions is a .ts file and not .js. In your package.json for Functions, you have
"main": "src/index.ts"
Try changing it to:
"main": "lib/index.js"
That way the entry point of Cloud Functions will be a JavaScript file, and also, will be the JavaScript that was built from your TypeScript code. lib/index.js is used because, by default, that is where the tsc command will build your src/index.ts file into.
Also do check this helpful document for troubleshooting the function deploy issues.
Goal
Use the firebase emulator and deploy firebase cloud functions locally for test and debug.
Issue
When firebase serve is run, it fails with - Error: Certificate object must be an object.
What's been tried
Following Google's documentation here, the instructions include
these steps:
To set up admin credentials for emulated functions (other than Cloud
Firestore and Realtime Database):
Open the Service Accounts pane of the Google Cloud Console.
Make sure that App Engine default service account is selected, and use the options menu at right to select Create key.
When prompted, select JSON
for the key type, and click Create. Set your Google default
credentials to point to the downloaded key:
$ export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"
$ firebase functions:shell
OR
$ firebase serve --only functions
Completing these instructions and running firebase serve... fails. Here are the logs:
Silver-Sliver:functions dchaddportwine$ firebase serve --only functions
=== Serving from '/Users/dchaddportwine/Sites/people-is-cloud-functions/functions'...
i functions: Preparing to emulate functions.
Warning: You're using Node.js v8.11.1 but Google Cloud Functions only supports v6.11.5.
⚠ functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
⚠ functions: Error from emulator. Error occurred while parsing your function triggers.
Error: Certificate object must be an object.
at FirebaseAppError.FirebaseError [as constructor] (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseAppError (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/utils/error.js:119:28)
at new Certificate (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/auth/credential.js:106:19)
at new CertCredential (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/auth/credential.js:189:64)
at Object.cert (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/node_modules/firebase-admin/lib/firebase-namespace.js:220:58)
at Object.<anonymous> (/Users/dchaddportwine/Sites/people-is-cloud-functions/functions/index.js:21:32)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
Question
How do I fix these two errors?
I have firebase-functions installed, and have tried npm i --save firebase-functions
⚠ functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.
Thinking this has to do with GOOGLE_APPLICATION_CREDENTIALS, but I'm not sure how to verify or check what's happening here.
⚠ functions: Error from emulator. Error occurred while parsing your function triggers.
Error: Certificate object must be an object.
UPDATE
Further along in the documentation, it reads:
If you're using custom functions configuration variables, run the
following command in the functions directory of your project before
running firebase serve.
firebase functions:config:get > .runtimeconfig.json
After running this command, I no longer get the certificate error. Woot! However, I am getting new failures and not many hints from the logs.
=== Serving from '/Users/dchaddportwine/Sites/people-is-cloud-functions/functions'...
i functions: Preparing to emulate functions.
i hosting[development-is]: Serving hosting files from: ./
✔ hosting[development-is]: Local server: http://localhost:5000
Warning: You're using Node.js v8.11.1 but Google Cloud Functions only supports v6.11.5.
⚠ functions: Failed to emulate app
⚠ functions: Failed to emulate helloSlack
⚠ functions: Failed to emulate helloIssy
⚠ functions: Failed to emulate interactiveIssy
i functions: No HTTPS functions found. Use firebase functions:shell if you would like to emulate other types of functions.
Code
Here is the firebase cloud functions for helloSlack
exports.helloSlack = functions.https.onRequest((req, res) => {
if (req) {
res.status(200).send(req.body);
} else {
console.log("req Error...");
throw res.status(500);
}
});
Answer
Further along in the documentation, it reads:
If you're using custom functions configuration variables, run the
following command in the functions directory of your project before
running firebase serve.
firebase functions:config:get > .runtimeconfig.json
This project is using custom functions configuration variables. So, after running the config command, I no longer get the certificate error. Woot!
I am trying to follow this tutorial from Firebase Web Codelab, but I am facing a problem at step 5: after running the command firebase serve, I do not get the expected response
Listening at http://localhost:5000, but instead I get:
i functions: Preparing to emulate functions.
i hosting: Serving hosting files from: ./
+ hosting: Local server: http://localhost:5000
which is bad because functions are not being served, although ⚠ functions: Failed to emulate api is not prompted, it remains "preparing to emulate functions" ad infinitum.
More information: CLI Version: 3.18.4
Platform: win32
Node Version: v6.12.3 and I already tried going to ~/.config/configstore/ and deleting the #Google-Cloud folder in order to restart the whole process.
I'm pretty sure you are in the right track, the difference between the expected message (Listening at http://localhost:5000) and the message you get (hosting: Local server: http://localhost:5000) is because you are using the new version of the CLI (v3.18.4) but the codelab chapter is not updated to reflect the new change in the logging message.
You can see the difference in the source-code firebase-tools v2.2.1 where it displays
Listening at http://<HOST>:<PORT>
and in firebase-tools v3.18.4 it displays
hosting: Local server: http://<HOST>:<PORT>
And for the functions emulation, as you are in step5, you don't have yet functions, see functions in the codelab source code.
This is a continuation of troubleshooting this problem and a separate issue related to implementing a suggested solution.
I am attempting to consume Firebase function configuration from a locally-served environment as outlined here but I get an unexpected error.
I create a ".runtimeconfig.json" file within my functions directory containing this:
{
"auth": {
"clientid": MY_CLIENT_ID,
"signoutreturnto": SOME_URL,
"responsetype": SOME_URL,
"redirecturi": SOME_OTHER_URL,
"scope": SOME_OTHER_STRING,
"domain": SOME_DOMAIN
}
}
I execute the following commands from the firebase CLI (within the functions directory):
firebase functions:config:get > .runtimeconfig.json
firebase experimental:functions:shell
I execute my config function via the cli:
config()
I get the following error output in the console:
config()
TypeError: config is not a function
at repl:1:1
at ContextifyScript.Script.runInContext (vm.js:32:29)
at REPLServer.defaultEval (repl.js:341:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)
at emitOne (events.js:96:13)
at REPLServer.emit (events.js:191:7)
at REPLServer.Interface._onLine (readline.js:241:10)
at REPLServer.Interface._line (readline.js:590:8)
my other functions, bigben & firebaseConfig execute as expected from the CLI
If you want to see your functions config at the the CLI emulator prompt, you should run the emulator command from your functions directory:
$ cd functions
$ firebase experimental:functions:shell
Then, at the firebase prompt, you can access your config like this:
firebase> const functions = require('firebase-functions')
firebase> functions.config()
That should print your config. Normally you don't need to do something like this - you would instead just read the config from inside the emulated function using functions.config().
Are you sure you are on the latest version of firebase-tools? You can update by running 'npm install -g firebase-tools'. You should be on v3.17.4, there was a known bug that was resolved in that version.