[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIREBASE_CONFIG and GCLOUD_PROJECT env vars missing (Node12) #859

Closed
dgaedcke opened this issue Feb 7, 2021 · 4 comments
Closed

FIREBASE_CONFIG and GCLOUD_PROJECT env vars missing (Node12) #859

dgaedcke opened this issue Feb 7, 2021 · 4 comments
Assignees

Comments

@dgaedcke
Copy link
dgaedcke commented Feb 7, 2021

Related issues

I've found many links online about this being an issue with Node10 (suggesting reversion to Node8);
But nothing thats solved my issue
It did get better (less severe warning) when I added the top-line commented in the code below

[REQUIRED] Version info

Node v 12.20.1
firebase-admin@9.4.2
│ ├─┬ @firebase/database@0.8.3
│ │ ├── @firebase/auth-interop-types@0.1.5
│ │ ├─┬ @firebase/component@0.1.21
│ ├─┬ @firebase/database-types@0.6.1
│ │ └── @firebase/app-types@0.6.1
│ ├─┬ @google-cloud/firestore@4.9.1
└─┬ firebase-functions@3.13.1

I'm on an M1 mac and when I run:
firebase --version
I get: Abort trap: 6

[REQUIRED] Test case

due to the abort-trap above, I'm using gcloud to deploy as follows:

gcloud functions deploy newCommFeedRecord
--trigger-event providers/google.firebase.database/eventTypes/ref.create
--trigger-resource projects/_/instances/tsapi-prod-default-rtdb/refs/commNews/{date}/{window}/{record}
--runtime nodejs12 --project tsapi-prod

[REQUIRED] Steps to reproduce

Deploy this one function:

"use strict";

// manually adding next line gives me a more benign warning
// but I cannot find and docs saying this should be necessary
// process.env.GCLOUD_PROJECT = "tsapi-prod"
// process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.newCommFeedRecord = functions.database.ref('commNews/{date}/{window}/{record}')
  .onCreate((snap, context) => {
    var date = context.params.date;
    var window = context.params.window;
    var record = context.params.record;
    var mostRecentWindow = {}
    var leastRecentWindow = {}
    var commNewsInternal = {}
    var updatePaths = false

    console.log("new comm feed record written at " + date + " " + window + " " + record)

    // read most recent window values
    // grab global data mapping from firebase
    snap.ref.root.child('_commNewsInternal').once('value')
      .then((snapshot) => { 
        console.log("_commNewsInternal is: " + JSON.stringify(snapshot.val()));
        console.log("windows is: " + JSON.stringify(snapshot.val().windows));

        // copy internal data
        commNewsInternal = {
            total: snapshot.val().total,
            limit: snapshot.val().limit,
            windows: snapshot.val().windows,
        }

        if (commNewsInternal.total === undefined || commNewsInternal.limit === undefined) {
          console.log("need total and limit!");
          return false
        }

        // check for empty window list in firebase
        if (commNewsInternal.windows === undefined) {
          console.log("need to init windows!"); 

          // create new window entry
          var newWindow = {
            date: date, 
            window: window, 
            count: 0
          }
          commNewsInternal.windows = [newWindow]

          console.log("setting window list to: " + JSON.stringify(commNewsInternal.windows));
        } 

        // get most recent window 
        mostRecentWindow = commNewsInternal.windows[commNewsInternal.windows.length - 1]
        leastRecentWindow = commNewsInternal.windows[0]

        // (1) compare most recent window values
        if (date != mostRecentWindow.date 
          || window != mostRecentWindow.window) {
            console.log("need create new window/date for new comm feed record!"); 

          // create and append new window entry
          var newWindow = {
            date: date, 
            window: window, 
            count: 0,
          }
          commNewsInternal.windows.push(newWindow)

          // newest window is now the most recent window
          mostRecentWindow = newWindow

          // paths need to be updated
          updatePaths = true
        } 
 
        // (2) update the total count and mostRecentWindow count
        commNewsInternal.total += 1
        mostRecentWindow.count += 1

        if (mostRecentWindow == leastRecentWindow) {
          // NOTE: there is no need to update the path list
          console.log("only one window!"); 
        } else if (commNewsInternal.total - leastRecentWindow.count > commNewsInternal.limit) {
          // need to remove least recent window
          console.log("diff " + commNewsInternal.total - leastRecentWindow.count + " greater than limit!");

          // adjust total
          commNewsInternal.total -= leastRecentWindow.count

          // remove least recent window
          commNewsInternal.windows.shift()
          console.log("windows is now " + JSON.stringify(commNewsInternal.windows))

          // paths need to be updated
          updatePaths = true
        }

        console.log("updating comm news internal with " + JSON.stringify(commNewsInternal)); 
        return snap.ref.root.child('_commNewsInternal').update(commNewsInternal); 
      }).then(() => { 
        // (3) update active paths if needed   
        if (!updatePaths) {
          return true;
        } 

        console.log("need to update paths!");
        var paths = []
        for (var i = 0; i < commNewsInternal.windows.length; i++) {
            var path = "commNews/" + commNewsInternal.windows[i].date + "/" + commNewsInternal.windows[i].window
            paths.push(path)
        }

        console.log("updating commNews-activePaths with " + JSON.stringify(paths));
        return snap.ref.root.child('commNews-activePaths').set(paths);
      })
      .catch((error) => {
        console.error("failed to get windows data: " + error.message);
      });
    return true;
});

[REQUIRED] Expected behavior

function should deploy and run without errors
referencing this example:
https://github.com/firebase/functions-samples/blob/master/quickstarts/uppercase/functions/index.js

[REQUIRED] Actual behavior

warning: FIREBASE_CONFIG and GCLOUD_PROJECT env vars missing (Node12)
Firebase init will fail

Were you able to successfully deploy your functions?

yes, it deploys with the above listed errors

@taeold
Copy link
Contributor
taeold commented Feb 11, 2021

Hi @dgaedcke.

As you've seen from the logs, the Firebase AdminSDK requires FIREBASE_CONFIG and GCLOUD_PROJECT env vars in the runtime environment.

When you deploy a Cloud Functions with the firebase cli tool (i.e.firebase deploy), the tool will automatically inject these environment variables in the deployed function. gcloud cli doesn't which is why you are seeing the errors.

Finding the correct values for these env var depends on your Google Cloud project, but if you already have some functions deployed via firebase tool, you can look at the environment variables associated with those deployment and copy them over to the problematic function.

However, it looks like the underlying issue is that the firebase CLI tool fails to run on your M1 macbook. Have to considered filing a bug in https://github.com/firebase/firebase-tools for that?

@taeold taeold added the Needs: Author Feedback Issues awaiting author feedback label Feb 11, 2021
@taeold taeold self-assigned this Feb 11, 2021
@dgaedcke
Copy link
Author
dgaedcke commented Feb 11, 2021

@taeold Thanks Daniel ... I've reinstalled Firebase with:
curl -sL firebase.tools | upgrade=true bash
and firebase --version
still yields: Abort trap: 6

Filed that bug report ... thanks for the clarification!!
firebase/firebase-tools#3129

@google-oss-bot google-oss-bot added Needs: Attention and removed Needs: Author Feedback Issues awaiting author feedback labels Feb 11, 2021
@taeold
Copy link
Contributor
taeold commented Feb 12, 2021

Great. Let me know if you need help finding the values for FIREBASE_CONFIG and GCLOUD_PROJECT.

Closing as the original issue is now resolved.

@dgaedcke
Copy link
Author
dgaedcke commented Feb 12, 2021

I know my project ID but not sure about FIREBASE_CONFIG ... but even if I knew both values, I'm not sure where or how to deploy it given that firebase CLI is not working .... I'll reach out again here if I don't hear from the CLI team about my M1 issue ... thanks!!

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

No branches or pull requests

3 participants