GoogleCloudPlatform / cloud-builders-community

Community-contributed images for Google Cloud Build
https://cloud.google.com/cloud-build/
Apache License 2.0
1.26k stars 858 forks source link

Firebase image does not receive custom env variables (process.env.my_var is undefined) #553

Closed anargu closed 2 years ago

anargu commented 2 years ago

I managed to tweak the gcr.io/cloud-builders-community/firebase build image so in theory it should handle custom ENVIRONMENT VARIABLES, so it can be retrieved by the code I implemented to deploy to firebase functions.

When I set the env variable I receive undefined when I receive in my code the process.env.my_var but the script that runs the firebase cli command gets the env var without problems.

In this bash script firebase.sh it does receive the value of process.env.NEXT_PUBLIC_CONFIG:

#!/bin/bash

if [ -n "$NEXT_PUBLIC_CONFIG" ]; then
    echo "NEXT_PUBLIC_CONFIG received"
    echo "$NEXT_PUBLIC_CONFIG"
fi

# run the original firebase
if [ $FIREBASE_TOKEN ]; then
  NEXT_PUBLIC_CONFIG=$NEXT_PUBLIC_CONFIG firebase "$@" --token $FIREBASE_TOKEN
else
  firebase "$@"
fi

But, in this block code it does not receive the process.env.NEXT_PUBLIC_CONFIG, it is undefined. This is the first part of the firebase function code I implemented

if (process.env.NEXT_PUBLIC_CONFIG == undefined) {
  throw new Error(`process.env.NEXT_PUBLIC_CONFIG is undefined. Value: ${process.env.NEXT_PUBLIC_CONFIG}. Process.env${JSON.stringify(process.env)}`);
}

const config = JSON.parse(process.env.NEXT_PUBLIC_CONFIG);
//...

Part of my cloudbuild.yml is:

steps:
  - id: 'npm install'
    name: 'gcr.io/cloud-builders/npm'
    args: ['install']

  - id: "deploy firebase"
    name: 'gcr.io/$PROJECT_ID/firebase'
    env:
      - 'NEXT_PUBLIC_CONFIG=$_CONFIG'
      - 'FIREBASE_TOKEN=$_FIREBASE_TOKEN'
    args:
      [
        'deploy',
        '--project',
        '$_FIREBASE_PROJECT_ID',
        '--only',
        'functions',
      ]

I am not sure why this behavior happens, Am I missing something?. Or does firebase cli command does not allow custom env vars?

Affected builder image

gcr.io/cloud-builders-community/firebase (modified, as I explained above)

Expected Behavior

Should read env variable from the code to be submitted to firebase functions

Actual Behavior

process.env.my_var is undefined

Steps to Reproduce the Problem

  1. Tweak Firebase build image
  2. use firebase build image to run "firebase deploy --only functions"
  3. Pass JSON data through env variable
  4. Deploy build (with Cloud Build)

Additional Info

I don't use a json file in my repository because it is public so this sensible data would be exposed. I didn't use yet the firebase :config set as this is not the pattern I follow with the rest of instances/backend components I already configured.

kozer commented 2 years ago

Any news on that?

anargu commented 2 years ago

Hi @kozer , I couldn't get a fix on that. What I had to do is modify my solution approach so I don't have to worry about passing env variables. But something useful I got from this issue is that when you use the firebase image and deploy on gcp then the credentials are already passed in to the build, so you don't have to pass the FIREBASE_TOKEN variable. I will close this as there is no new activity here.