1

I've been doing a React+Firebase project without any CI/CD, and the manual effort has become unpleasant, so I'm looking to automate deployment to 2 environments, but not sure how to it with Firebase.

I have 2 environments - test and prod, each pointing to a separate Firebase project and database. Currently depending on where I deploy, I just alternate between config and configTest:

const configTest = {
    apiKey: "XXX",
    authDomain: "domaintest.firebaseapp.com",
    databaseURL: "https://domaintest.firebaseio.com",
    projectId: "domain-test",
    storageBucket: "domaintest.appspot.com",
    messagingSenderId: "000"
};

const config = {
    apiKey: "YYY",
    authDomain: "domain.firebaseapp.com",
    databaseURL: "https://domain.firebaseio.com",
    projectId: "domain",
    storageBucket: "domain.appspot.com",
    messagingSenderId: "111"
};

firebase.initializeApp(config);
//firebase.initializeApp(configTest);

I am assuming there should be a better way, but I couldn't find any instruction on the topic.

Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33

1 Answers1

2

Usually you have the config object filled with environment variables accessed via process.env global variable.

const config = {
  apiKey: process.env.API_KEY,
  authDomain: process.env.AUTH_DOMAIN,
  ...
};

You can use modules like env-cmd or dotenv to load environment variables from .env file.

Gabriel Bleu
  • 9,703
  • 2
  • 30
  • 43
  • Thanks, with your initial hint I found the instruction at Firebase: https://firebase.google.com/docs/functions/config-env Maybe you want to incorporate this in your answer? – Dmitri Borohhov Aug 20 '19 at 14:14