0
votes

I have two firebase projects (default and staging). Both are initialised under the same aliases in the CLI.

I can deploy everything successfully to 'default' and all but functions successfully to 'staging'. I get a 403 error when deploying to staging.

I initialise my firebase admin using the following code, that uses my Service Account key.json file, so I can run my functions locally.

// Import from Firebase
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'

// Admin Init that supports localhost testing
const serviceAccount = require('../keys/default-key.json')
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
})

If I manually change the reference to 'staging-key.json' which uses the Service Account key.json from my staging project, then the deploy to staging works fine too.

My question: Can I automatically change the referenced key.json file depending on the project I'm deploying too?

OR - is there a better way I should be managing this situation?

Ideally I just want to use 'firebase use' in CLI to switch between projects and deploy, without having to alter the code.

1

1 Answers

0
votes

You will want to find a way to make use of the environment configuration provided by the Firebase CLI. This lets you add some configuration to your deployment. As stated in the docs:

Often you'll need additional configuration for your functions, such as third-party API keys or tuneable settings. The Firebase SDK for Cloud Functions offers built-in environment configuration to make it easy to store and retrieve this type of data for your project.

You can certainly put your service account credentials in the environment, so you don't have to figure out which file to deploy. To make it easier, consider writing a deployment script for each of your deployment targets (default and staging) to determine which values they get.