Repository navigation
Description
Related issues
Probably related: Feature Request: Params #1084
Version info
node: v16.15.0
firebase-functions: 3.18.0
firebase-tools: 11.9.0
firebase-admin: 11.0.1
Test case & Steps to reproduce
Test Nr.1
# .env
DATA='data'// functions/receive.ts
import { https } from 'firebase-functions';
console.log(process.env.DATA);
export const httpsOnRequest = https.onRequest(async (req, res) => {
console.log(process.env.DATA);
res.status(200);
});Here everything is fine. I got the correct console output from all scopes.
Test Nr.2
# .env
GOOGLE_APPLICATION_CREDENTIALS=firebase.admin.json// functions/receive.ts
import { applicationDefault, initializeApp } from 'firebase-admin/app';
import { https } from 'firebase-functions';
initializeApp({
credential: applicationDefault(),
databaseURL: 'database_url',
});
export const httpsOnRequest = https.onRequest(async (req, res) => {
res.status(200);
});Everything is still fine. Credential is loaded from firebase.admin.json and the admin SDK can be used.
Test Nr.3
# .env
CREDENTIALS_ADMIN_PROJECT_ID='project_id'
CREDENTIALS_ADMIN_CLIENT_EMAIL'client_email'
CREDENTIALS_ADMIN_PRIVATE_KEY='private_key'
CONFIG_FIREBASE_DATABASE_URL='datbase_url'import { initializeApp } from 'firebase-admin';
import { cert } from 'firebase-admin/app';
import { https } from 'firebase-functions';
initializeApp({
credential: cert({
projectId: process.env.CREDENTIALS_ADMIN_PROJECT_ID,
clientEmail: process.env.CREDENTIALS_ADMIN_CLIENT_EMAIL,
privateKey: process.env.CREDENTIALS_ADMIN_PRIVATE_KEY,
}),
databaseURL: process.env.CONFIG_FIREBASE_DATABASE_URL,
});
export const httpsOnRequest = https.onRequest(async (req, res) => {
res.status(200);
});Here everything goes down the drain. On deploy it fails with Error: Failed to load function definition from source: Failed to generate manifest from function source: Error: Service account object must contain a string "project_id" property.. For some inexplicable reason the environment variables are all empty.
Summarized: Firebase does collect the environment path from GOOGLE_APPLICATION_CREDENTIALS but does not use custom environment variables.
Test Nr.4
# .env
CREDENTIALS_ADMIN_PROJECT_ID='project_id'
CREDENTIALS_ADMIN_CLIENT_EMAIL'client_email'
CREDENTIALS_ADMIN_PRIVATE_KEY='private_key'
CONFIG_FIREBASE_DATABASE_URL='datbase_url'import { initializeApp } from 'firebase-admin';
import { App, cert } from 'firebase-admin/app';
import { https } from 'firebase-functions';
class Application {
private app?: App;
get get() {
if (!this.app) {
this.app = initializeApp({
credential: cert({
projectId: process.env.CREDENTIALS_ADMIN_PROJECT_ID,
clientEmail: process.env.CREDENTIALS_ADMIN_CLIENT_EMAIL,
privateKey: process.env.CREDENTIALS_ADMIN_PRIVATE_KEY,
}),
databaseURL: process.env.CONFIG_FIREBASE_DATABASE_URL,
});
}
return this.app;
}
}
const application = new Application();
export const httpsOnRequest = https.onRequest(async (req, res) => {
application.get;
res.status(200);
});This works and is the current solution, because the environment only gets loaded on the first request.
Expected behavior
Function should load environment variable at deploy time and deploy without failure.
Actual behavior
Environment variables are not loaded and deploy fails.
Were you able to successfully deploy your functions?
Yes. Not using environment variables or load on first request.
Activity
google-oss-bot commentedon Sep 27, 2022
I found a few problems with this issue:
[-]Environment variable is epmpty on deploy[/-][+]Environment is empty on deploy[/+]inlined commentedon Oct 3, 2022
As detailed in #1084 this is expected behavior. The Params feature is very near completion and will allow you to configure your code even with process.env being empty during "discovery" (when we load your code during a deploy).
There's a couple of reasons for this:
inlined commentedon Oct 3, 2022
(Per your observations, some legacy built-in environment variables are present like GCLOUD_PROJECT, and FIREBASE_CONFIG. These exist because the SDK will blow up without them until Params are finished. Those environment variables will also be removed in a major release of the
firebase-toolscommandline tooling)