googlearchive / cloud-functions-emulator

A local emulator for deploying, running, and debugging Google Cloud Functions.
https://github.com/GoogleCloudPlatform/cloud-functions-emulator/wiki
Apache License 2.0
827 stars 115 forks source link

Emulator not setting environment variables during deploy #178

Open ahume opened 6 years ago

ahume commented 6 years ago

Environment details

Steps to reproduce

  1. Write code that accesses process.env.GCP_PROJECT outside of main cloud function scope
  2. functions deploy functionName --trigger-http --local-path .

I'm using process.env.GCP_PROJECT to load a specific environment file per project. This works when deploying the function to a project, but not to the emulator.

I'm pretty sure it used to work, will attempt to get old versions and bisect.

ahume commented 6 years ago

This has started working with the same version of the emulator, and unfortunately I don't know what I changed.

einargs commented 6 years ago

Environment

Steps to reproduce

  1. Set up a service account JSON.
  2. Set up the environment variable.
  3. Try to use the default authentication credential system through the googleapis npm package:
    const {google} = require("googleapis");
    google.auth.getApplicationDefault((err, authClient, projectId) => {
    //...
    }

I'm encountering a problem where the environment variable GOOGLE_APPLICATION_CREDENTIALS is not set while running the function emulator. This is a problem, because process.env.GOOGLE_APPLICATION_CREDENTIALS is used to hold the default authentication credential information. I (currently) run the functions emulator using sudo; however, I have confirmed by running a separate node.js file (console.log(process.env.GOOGLE_APPLICATION_CREDENTIALS);) with sudo -E (-E preserves environment) that process.env.GOOGLE_APPLICATION_CREDENTIALS is still set.

I'm not sure if I'm encountering the same issue as @ahume, but I figured it would be better to err on the side of safety and avoid prematurely opening a new issue.

I'm currently using a hack where I manually set process.env.GOOGLE_APPLICATION_CREDENTIALS to the correct value (a path leading to my service-account.json). This is non-optimal, because I would rather not have to remove this line every time I want to deploy a cloud function.

andretti1977 commented 6 years ago

I have the same issue setting custom env vars. The only solution i was able to find is to stop the functions emulator functions stop set the new env var export FOO=bar then restart the emulator functions start At that point my locally executed cloud function is able to see the env var

Just for clarity i'm using

"dependencies": {
    "@google-cloud/firestore": "^0.16.0"
  }

even if version 0.17 is available but does produce an error with semver

mrdulin commented 6 years ago

Same issue. Need clear documentation

jbrinley commented 6 years ago

I've experienced the same issue. Rebooting clears it up.

koteisaev commented 5 years ago

I also noticed that emulator ignores --set-env-vars parameter of deploy command

solieman commented 5 years ago

Yes, unfortunately, emulator ignores --set-env-vars parameter of deploy command. Anyone can help in a workaround for this? I need to read some environment variables.

AndriiTsok commented 5 years ago

Our team experienced the same issue --set-env-file is not working.

twistedpair commented 5 years ago

Just a note: I found functions stop wasn't actually stopping the emulator node process, keeping the environment variable exports from working.

Calling pkill node, then exporting the variable, and restarting the emulator did work.