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

Alias environment files not read on pre-deploy checks #7924

Open
eljass opened this issue Nov 8, 2024 · 4 comments
Open

Alias environment files not read on pre-deploy checks #7924

eljass opened this issue Nov 8, 2024 · 4 comments

Comments

@eljass
Copy link

eljass commented Nov 8, 2024

[REQUIRED] Environment info

firebase-tools: 10.9.0

Platform: macOS

[REQUIRED] Test case

See below.

[REQUIRED] Steps to reproduce

Firebase aliases:

// .firebaserc
{
  "projects": {
    "prod": "<PRODUCTION PROJECT>",
    "dev": "<DEV PROJECT>",
    "default": "<DEV PROJECT>"
   },
  ...
}

.env variables:

// ./functions/.env
<EMPTY>

// ./functions/.env.prod
FB={"private-rtdb": "<PRODUCTION PROJECT>-private", "public-rtdb": "<PRODUCTION PROJECT>-public"}

We have helper function that checks if the variable exists:

function getVariable(key: string) {
  const { env } = process
  console.log("Current variables", env)
  // => Prints:
  //      "FIREBASE_CONFIG": "<PROJECT'S_CONFIG>,
  //     "GCLOUD_PROJECT": "<PROJECT I'M DEPLOYING TO>",
  //     "GOOGLE_CLOUD_QUOTA_PROJECT": "<PROJECT I'M DEPLOYING TO>",
  //     "PORT": "8143",
  //     "FUNCTIONS_CONTROL_API": "true",
  //     "HOME": "MY HOME PATH FROM COMPUTER",
  //     "PATH": "<MY PATH VARIABLE FROM COMPUTER>",
  //     "NODE_ENV": "<NODE ENV I SET WITH export NODE_ENV=xxx>",
  //     "CLOUD_RUNTIME_CONFIG": "<OLD RUNTIME CONFIGS SET FOR THE PROJECT IN STRINGIFIED JSON>",
  //     "__CF_USER_TEXT_ENCODING": "..."
  
  const [variableName, field] = key.split('.')

  try {
    const json = JSON.parse(env[variableName])
    if (field) {
      return json?.[field]
    }
    return json
  } catch {
    return env[variableName]
  }
}
  1. Use v1 function for Realtime Database
  2. Specify function instance for the function from the env file:
         import { getVariable } from './utils/variables.ts'
    
         functions
           .region('europe-west1')
           .database.instance(getVariable('FB.private-rtdb'))
           .ref('/test')
           .onWrite(async (snapshot, context) => {...})
  3. Now when deploying the function it fails as it cannot find the "FB" variable

So it seems that the process.env is read from the .env -file only, not from the aliased env like documentation states in here.

I did try with adding the dotenv node module and using dotenv.config() but there was no changes there.

[REQUIRED] Expected behavior

Pre deploy checks should respect named .env files and list the variables.

[REQUIRED] Actual behavior

See above.

Final comment

We did got around this issue with the CLOUD_RUNTIME_CONFIG variable, which did included correct env variables. But I assume it is a deprecated variable that runtimeconfig sets. So if that is removed in the future, we will end up with the same block.

@google-oss-bot
Copy link
Contributor

This issue does not seem to follow the issue template. Make sure you provide all the required information.

@touficbatache
Copy link

touficbatache commented Nov 13, 2024

Also happening on my end, my .env.dev is not being taken into account (for the dev alias). Now I'm having to manually edit the .env file before each deployment on the different aliases... Before, I could see a message in the console confirming the use of the correct .env.* files, now I just see: Now using alias dev (projectname)

@eljass
Copy link
Author

eljass commented Nov 15, 2024

To add on here. It seems the build step for functions is not using any environment variables from the .env files inside ./functions -folder. This is blocking for us as we are getting the trigger instance for function from the env.

Workaround currently is to check the variables also from the env.CLOUD_RUNTIME_CONFIG, and add them there with old runtime config way: firebase functions:config:set <VARIABLE KEY>=<VARIABLE VALUE>.

So in our helper function we use something similar to this:

function getSecretValue(secretName) {
  const { env } = process

  const [variableName, field] = key.split('.')

  // Workaround to check if variable is missing from environment directly but exists in old runtime configs
  const envValue = env?.[variableName] ?? (env?.CLOUD_RUNTIME_CONFIG) && JSON.parse(env?.CLOUD_RUNTIME_CONFIG)[variableName.toLowerCase()])

  try {
    const json = JSON.parse(envValue)
    if (field) {
      return json?.[field]
    }
    return json
  } catch {
    return env[envValue]
  }
}

@colerogers
Copy link
Contributor

Hi @eljass thanks for opening your first issue here!

So looking at your setup, everything actually seems to be WAI. Standard env support with .env files are only available during the function runtime. That's inside the function only and not used as config during build-time.

We developed params as the solution for folks that want to parametrize their configuration and use vars during both build and runtime. But it looks like the instance field on database functions only accepts a string right now. Since that looks like something we can add, I'm going to change this issue to be feature request.

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

5 participants