Skip to content

Make environment variables on function instances be accessible during trigger parsing - #3422

Merged
taeold merged 6 commits into
masterfrom
dl-cf3-env-vars-injection
Jun 2, 2021
Merged

taeold merged 6 commits into
masterfrom
dl-cf3-env-vars-injection

Conversation

@taeold

@taeold taeold commented May 27, 2021 •

Copy link
Copy Markdown
Contributor

We move the logic for setting up environment variables for function instances from post-trigger parsing to pre-trigger parsing. This makes environment variables accessible during trigger parsing, e.g.

const myFunc = functions.runWith({minInstances: process.env.TIER == "PROD" ? 20 : 0})...

(Of course, there isn't a way for users to specify environment variables for now. That may change soon.)

This change simply refactors existing logic - nothing new is introduced and function deploys should work exactly as it is done today.

Warning There is technically a breaking change where we no longer pass caller's process.env when forking a process to parse function triggers in user's source code. We think this is removing what was previously an undefined behavior but recognize we may be breaking someone's workflow. We will soon propose a support for setting project-level environment variables more easily.

…ger parsing.

We move the logic for setting up environment variables for function instances from post-trigger parsing to pre-trigger parsing. This makes environment variables accessible during trigger parsing, e.g.

const myFunc = functions.runWith({minInstances: process.env.TIER == "PROD" ? 20 : 0})...

(Of course, there isn't a way for users to specify environment variables for now. That may change soon.)
@google-cla google-cla Bot added the cla: yes Manual indication that this has passed CLA. label May 27, 2021
): Promise<TriggerAnnotation[]> {
return new Promise((resolve, reject) => {
const env = _.cloneDeep(process.env);
const env = { ..._.cloneDeep(process.env), ...envs } as NodeJS.ProcessEnv;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a breaking change, but I actually think we shouldn't ever have been carrying forward process.env. It was always weird undefined behavior before and I'd be OK dropping it for now. IMO, we should be populating exactly the env they'll be getting in production.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, as a nit, you don't need _.cloneDeep anymore since you're already cloning with the splat operator and this is just a simple map so there's no recursive cloning to do.

entryPoint: annotation.entryPoint,
runtime: runtime,
trigger: trigger,
environmentVariables: envs,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. this gives me a bit of pause. If we're requiring project-level env, should env be a backend attribute and not a FunctionSpec attribute?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo that's an excellent point! Thanks - will address.

@taeold
taeold force-pushed the dl-cf3-env-vars-injection branch from 2a439b3 to 57da423 Compare June 1, 2021 22:23
@taeold
taeold requested a review from joehan June 1, 2021 22:23
@taeold
taeold marked this pull request as ready for review June 1, 2021 22:32
@taeold
taeold merged commit e1713ac into master Jun 2, 2021
@taeold
taeold deleted the dl-cf3-env-vars-injection branch June 2, 2021 18:30
devpeerapong pushed a commit to devpeerapong/firebase-tools that referenced this pull request Dec 14, 2021
… trigger parsing (firebase#3422)

We move the logic for setting up environment variables for function instances from post-trigger parsing to pre-trigger parsing. This makes environment variables accessible during trigger parsing, e.g.

```
const myFunc = functions.runWith({minInstances: process.env.TIER == "PROD" ? 20 : 0})...
```

(Of course, there isn't a way for users to specify environment variables for now. That may change soon.)

This change simply refactors existing logic - nothing new is introduced and function deploys should work exactly as it is done today.

*Warning* There is technically a breaking change where we no longer pass caller's `process.env` when forking a process to parse function triggers in user's source code. We think this is removing what was previously an undefined behavior but recognize we may be breaking someone's workflow. We will soon propose a support for setting project-level environment variables more easily.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Manual indication that this has passed CLA.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants