Search code examples
firebasegoogle-cloud-functions

Firebase functions deploy single function with a different ignore configuration


We have several 2nd gen functions sharing the same source code. Only one of them needs to be deployed with a different ignore configuration in firebase.json.

It's about 2 MB inside a bin folder that we want to ignore in all the other functions, except for that one.

Our current firebase.json looks like this:

"functions": [{
  ... ...,
  "ignore": [
    "node_modules",
    "firebase-debug.log",
    ... ...,
    "bin"   // <==== REMOVE THIS LINE FOR A SPECIFIC FUNCTION ONLY
  ]
}]

Any ideas on how we could achieve that?

What we've tested:

We thought of configuring a different functions[].codebase: 'code2', for example, (so we could run firebase deploy --only functions:code2:myBinFunction), but actually both functions[].source point to the same folder, so deployment complains with Error: functions.source must be unique but './functions' was used more than once.

We don't want to split source code to a different folder because they all share many things and to keep maintenance simple. Also, I'm sure anyone in the team will forget and deploy using the wrong codebase and this would need a way to fail if deployed without the bin folder.

Maybe there's a totally different approach to this? Thanks in advance

EDIT: Oh well, configuring functions[].source: 'functions' in one block, and functions[].source: './functions' on the other block did the trick to allow deployment. But I still have the problem that I need deployment to fail if the function is deployed from the wrong codebase (i.e., deployed without the bin folder it needs)


Solution

  • I ended up configuring a predeploy hook to:

    • Reject deployment of full function list. Only individual deployment is allowed.
    • Reject deployment if there's a mix of bin-folder-required functions and regular functions.
    • Reject deployment of any function not being deployed from its corresponding codebase.

    I created 2 codebases pointing to the same folder:

    The firebase.json looks like this:

    {
      "functions": [
        {
          "source": "functions",
          "codebase": "cb1",
          "predeploy": "node scripts/firebase-predeploy.js",
          "ignore": [
            "node_modules",
            ".git",
            ... ...,
            "bin"
          ]
        },
        {
          "source": "./functions",
          "codebase": "cb2",
          "predeploy": "node scripts/firebase-predeploy.js",
          "ignore": [
            "node_modules",
            ".git",
            ... ...
          ]
        }
      ],
    

    so the functions that need the bin folder will be deployed using --only functions:cb2:myBinFunction.

    And the script firebase-predeploy.js considers:

    process.env.PROJECT_DIR // where firebse.json is located
    process.env.npm_lifecycle_script // package.json script being run
    

    We always deploy from package.json scripts, so this approach works for us.

    If someone wants the script as a reference, just ask. More info on firebase deploy hooks here.