firebase / firebase/firebase-tools

Alias environment files not read on pre-deploy checks

Open
#7,924 4 comments 0 reactions 1 assignee Assigned to @taeold View on GitHub
api: functions type: feature request
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info

**firebase-tools:** 10.9.0

**Platform:** macOS
### [REQUIRED] Test case

See below.

### [REQUIRED] Steps to reproduce

Firebase aliases:
```js
// .firebaserc
{
"projects": {
"prod": "",
"dev": "",
"default": ""
},
...
}
```

.env variables:
```txt
// ./functions/.env

// ./functions/.env.prod
FB={"private-rtdb": "-private", "public-rtdb": "-public"}
```

We have helper function that checks if the variable exists:
```ts
function getVariable(key: string) {
const { env } = process
console.log("Current variables", env)
// => Prints:
// "FIREBASE_CONFIG": ",
// "GCLOUD_PROJECT": "",
// "GOOGLE_CLOUD_QUOTA_PROJECT": "",
// "PORT": "8143",
// "FUNCTIONS_CONTROL_API": "true",
// "HOME": "MY HOME PATH FROM COMPUTER",
// "PATH": "",
// "NODE_ENV": "",
// "CLOUD_RUNTIME_CONFIG": "",
// "__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:
```ts
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](https://firebase.google.com/docs/functions/config-env?gen=1st#deploying_multiple_sets_of_environment_variables_2).

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.