Skip to content

Environment is empty on deploy #1244

Closed
Closed

Description

@LaCocoRoco

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

  1. google-oss-bot commented on Sep 27, 2022

    @google-oss-bot
    Collaborator

    I found a few problems with this issue:

    • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
    • This issue does not seem to follow the issue template. Make sure you provide all the required information.
  2. changed the title [-]Environment variable is epmpty on deploy[/-] [+]Environment is empty on deploy[/+] on Sep 27, 2022
  3. inlined commented on Oct 3, 2022

    @inlined
    Member

    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:

    1. Params are a much better way to declare a dependency on environments and providing env when figuring out what env you need is obviously a contradiction
    2. Due to ways we're consolidating the roadmaps of Cloud Functions for Firebase and Firebase Extensions, we need to be able to launch and analyze a codebase agnostic of all projects where it might be installed
  4. inlined commented on Oct 3, 2022

    @inlined
    Member

    (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-tools commandline tooling)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions