node.jsfirebasegoogle-cloud-functionsfirebase-tools

Firebase Function - conditionally deploy function based on logic using paramaterized config


We used to use firebase functions config with 1st gen functions like this code snippet below. Code like this used to work to only deploy that function when it was the production environment:

const functions = require('firebase-functions')


exports.onlyUseThisInProduction = functions.config().someValue === 'production' &&
 functions.pubsub
    .schedule('* * * * *')
    // ...


But now we are using parameterized configuration as described here and 2nd gen functions: https://firebase.google.com/docs/functions/config-env?gen=2nd.

Now the code is more like this:

const { defineString } = require('firebase-functions/params')
const { onSchedule } = require('firebase-functions/v2/scheduler')
const someValue = defineString('SOME_VALUE').value()

exports.onlyUseThisInProduction = someValue === 'production' && onSchedule({ schedule: '* * * *' ... })

The function onlyUseThisInProduction doesn't ever get deployed with the code above. Our other functions are working properly, but the functions that are meant to be conditionally deployed don't ever get deployed. I know we could do things like deploy only specific functions through the CLI, but I'd to control which can be deployed through the code so if we just run firebase deploy it doesn't deploy some functions.


Solution

  • It's not possible to use defineString and other parameters for this because parameters are only available at the time the function is running, not at the time it's being deployed.

    You could try to find some other way to inject a value at deploy time, but I suspect that's not going to possible. I think you should use the Firebase CLI to determine which functions to deploy, as that's the only directly supported solution.

    See also: How to deploy some functions to Cloud Functions for Firebase without affecting some other functions?