Firebase Admin SDK - not using Netlify functions

Hi, everyone.

I haven’t deployed my site yet, since I don’t know how to do that when i apparently can’t require my Firebase credidentials.json file at build time in Netlify.

I’ve got a Gatsby site that uses a plugin called ‘gatsby-source-firestore’, and this requires the Firebase admin SDK. In my gatsby-config.js, the plugin is configured like this:

 resolve: 'gatsby-source-firestore',
  options: {
    credential: require("./src/firebase/firebase-creds.json"),
    types: [
      {
        type: 'Product',
        ...more data
        }),
      },
    ],
  },
}, 

Since this is a json file, and not environment variables, I’m not sure how to require this information in Netlify. Is it possible to deploy the site with the json-file directly to Netlify, without continuous deployment via github?

All help appreciated :pray:

Sure, just include it in your deploy. Gatsby deploys tons of json files by default :slight_smile: Seems like your code tries to read it from “./src/firebase/firebase-creds.json” so I guess make sure the file is there during deploy! Do you use it during site build, or during runtime?

If you are having trouble getting it to deploy, you’d be well-served by letting us know which deploy you didn’t see it deployed in (link us to its logs page; each deploy has a separate logs page from the deploys listing in our UI even if there are no build logs there), so our staff can confirm or deny that the file is in the deploy at the path you are expecting.

Hey!

Thanks for the reply. Well, It’s used during build I guess, since the plugin is used to create pages (createPages). However, I got an error during deploy, where it said it couldn’t locate the json, just like you are saying. It would be awesome if you guys could have a look at it :sunglasses:

https://app.netlify.com/sites/quirky-joliot-4749a2/deploys/5f554d3ecbf55b4c303a2484

Oh, also, while I’m at it - I’m using github for continuous deployment with Netlify, so there’s that… I guess I somehow have to make use of the Netlify environment variables then, after all?

Hey @arnelamo,
For security reasons, I don’t think you’d want to deploy your credentials. What you can try is setting a Netlify environment variable to GOOGLE_APPLICATION_CREDENTIALS, copy/pasting the whole json file into there, and then referencing it in your Gatsby config as process.env.GOOGLE_APPLICATION_CREDENTIALS. You may have to JSON.parse() it, like JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS)

Want to let us know if that works for you?

I’ve tried, and failed - unfortunately.

It works perfectly locally in development mode, but it seems Netlify has a problem with one of the variables: “private_key” in the firebase config. Of that reason, I got some tips to try to JSON.parse(JSON.stringify(config)), but that didn’t change anything.

Hey @arnelamo,
Have you tried just passing process.env.GOOGLE_APPLICATION_CREDENTIALS with no JSON parsing or anything? It looks like that plugin is expecting a JSON string (which is what your env variable is):

If not, it looks like the plugin will also accept an appConfig object instead of those credentials. Take a look at #4 here, may be worth a shot:

         appConfig: {
           apiKey: "api-key",
           authDomain: "project-id.firebaseapp.com",
           databaseURL: "https://project-id.firebaseio.com",
           projectId: "project-id",
           storageBucket: "project-id.appspot.com",
           messagingSenderId: "sender-id",
           appID: "app-id",
         },

Hi again,@jen.

Passing the config object to credentials works fine in dev mode, but on Netlify it causes the same error as in my last reply - it does not even initialize the app.

Now I’ve tried to put them into a env variable, like so:


Ps. I added the .replace(/\\n/g, '\n') as of some reason the \n becomes \\n when you stringify() it, so before I did that the app didn’t even initialize in dev mode.

Then, i set it like so:
credential: JSON.parse(process.env['GOOGLE_APPLICATION_CREDENTIALS']),

Again, this works like a charm in dev mode, it initializes and creates queries and pages. In Netflify however, it’s still struggling with the private_key it seems.

And, worth to mention: I removed the quotes from the private_key env variable (at Netlify), as someone also said that should help, and the above error message is produced without quotes. However, if I try to deploy with the quotes, I get a different error message which reads:

Error: 14 UNAVAILABLE: Getting metadata from plugin failed with error: error:0 909006C:PEM routines:get_name:no start line

I’m actually beginning to consider giving up, as time passes on. It seems they (Google) have made this a whole lot easier if you use their hosting service, but I really wanted to use Netlify instead of multiple reasons. :weary:

Anyways, thanks a lot for the help. I really appreciate it.

Dang! I really feel like that should work. Sharing this issue in case it sheds any light: https://github.com/googleapis/google-cloud-node/issues/2658#issuecomment-334750249 If you wanted to share the repo (without the credentials!), I’d be happy to keep working on it. Though totally understand if you’re ready to move on at this point :slight_smile: