firebasefirebase-hostingfirebase-cli

Pass environment variables to Firebase predeploy scripts fails silently


I am trying to run a npm build during my hosting pre-deploy script.

If I was going to run the build from the command line, I can successfully run this:

set NUXT_ENV_GCLOUD_PROJECT=whatever&&npm run build

And that works just fine. But when I try the same in the following firebase.json file it simply skips the build:

{
  "hosting": {
    "predeploy": ["set NUXT_ENV_GCLOUD_PROJECT=whatever&& npm run build"],
  }
}
> firebase deploy --only hosting

=== Deploying to 'xxx'...

i  deploying hosting
Running command: set NUXT_ENV_GCLOUD_PROJECT=whatever&& npm run build
+  hosting: Finished running predeploy script.
i  hosting[dev-phojo-app]: beginning deploy...
i  hosting[dev-phojo-app]: found 41 files in ./dist

It says that it finished running the predeploy scripts, but it in fact never did. If I remove the setting of the environment variable, then it works as expected (but, of course, the build fails because the environment variable is not there..)

What is going on here?


Solution

  • Instead of using a shell expression in firebase.json, I suggest putting those expressions into a separate script file, and invoke that script from the predeploy hook. So, something like this:

    predeploy.sh:

    #!/bin/bash
    export NUXT_ENV_GCLOUD_PROJECT=whatever
    npm run build
    

    firebase.json:

      "hosting": {
        "predeploy": [ "./predeploy.sh" ],
      }
    

    Make sure that predeploy.sh is executable: chmod a+x predeploy.sh

    I suspect that the Firebase CLI just wants to "exec" the command you give, and that it can't contain shell expressions, such as &&.