7
votes

So i was migrating my app from node express to firebase-functions!

In my node-express app I have .env file which contains all the data, FOr starters let's consider this as my .env file

GOOGLE_CLIENT_ID = 4046108-bssbfjohpj94l0dhpu69vpgs1ne0.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET = lTQHpj3yY57oQpO

And then in my passport strategy, I have something like this

passport.use(new GoogleStrategy({
    clientID: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL:  "/auth/google/callback",
    userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
    accessType: 'offline',
    passReqToCallback: true
  },

Now,

Question: 1 - Firebase-functions probably don't support .env file, so can we set env variable without manually adding it using set flag? let's say I have lot variable in my environment

Question - 2: Can I access the variable I set by doing something like this

firebase functions:config:set slack.url=https://hooks.slack.com/services/XXX

using

 process.env.slack.url

or we have to do (necessary)

functions.config().slack.url

Question:3 From Firebase Docs, it is written

There are environment variables that are automatically populated in the functions runtime and in locally emulated functions, including:

process.env.GCLOUD_PROJECT: Provides the Firebase project ID

process.env.FIREBASE_CONFIG: Provides the following Firebase project config info:

What do they mean when they mean? and if the answer to the question two is false then how are they using process.env.FIREBASE_CONFIG:

1
Is this a real google-client-id/secret? If so, consider removing itjo_va
@jo_va Secret/Client-ID won't work for others. I randomly removed Alphabets/numbers from between. Btw- Can you answer the question?anny123
You're confusing process environment variables, and the special environment configuration provided by the Firebase tools. They're completely different things.Doug Stevenson
@DougStevenson Can you help me in comprehending the difference between both? I wasn't able to understand the difference between both from firebase docs.anny123

1 Answers

2
votes

Answer for question 1:

Please note that, at this time, there doesn't appear to be a supported way to deploy true environment variables along with your function(s) using the firebase CLI tool. You can instead provide it as function.config() information, which is their recommended alternative to environment variables.

If you really really want to avoid function config, "pure" google cloud functions support setting environment variables. This page is a walkthrough of how to use a .env.yaml file and ultimately access those values from process.env in your code base. I know your file wasn't *.yaml, but this is a good solution (with some minor refactoring to YAML format).

The short version:

gcloud functions deploy FUNCTION_NAME --env-vars-file .env.yaml FLAGS

Answer for question 2

There is a difference between Firebase function config key/value pairs (i.e. function.config()) and process.env access. They are similar in purpose, but persist data in different places (i.e. in Firebase's services vs actual environment variables). Thus, the syntax for accessing each one is different.

Answer for question 3

Firebase has some process.env variables available to you by convention. They're simply documenting that for your convenience, so you can assume those are there and available as true environment variables (and not function.config() values).

Again, as of right now, the CLI tool doesn't seem to let you set true environment variables. So you'll have to go with function config, or do some other hack similar to this, which takes your function config key/values and sets them as environment variables at runtime.

const functions = require('firebase-functions');
const config = functions.config();
// Porting envs from firebase config
for (const key in config.envs){
  process.env[key.toUpperCase()] = config.envs[key];
}