How to exclude ServiceAccountKey.json when I deploy cloud functions? - firebase

I downloaded ServiceAccountKey.json to make local cloud functions env.
I think ServiceAccountKey.json should not be public and it should not be in GCP.
So when I deploy cloud functions, how do I exclude it?
Or is that no problem to upload ServiceAccountKey.json using firebase deploy --only functions?
Please teach me how to do. Thank you.

I would move the ServiceAccountKey.json file out of the deployment folder. This way, it will never be deployed and you also don't have to add it to the git ignore file. You could also add it to the ignored files in your firebase.json file like so:
{
"functions": {
"source": "functions",
"ignore": [
"ServiceAccountKey.json"
]
}
}

If you use Firebase Function emulator, you wouldn't need to manage service account locally yourself by using Application Default Credentials. You can initialize Admin SDK like this:
import { initializeApp } from "firebase-admin/app"
const admin = initializeApp(); // <-- no params

Related

How can i deploy correctly mi website in firebase? [duplicate]

I have complete all process
Firebase Login
Firebase init
Firebase deploy
now finally i got a link https://testing-37553.firebaseapp.com/
but my website not showing so what should i have to do.
What you are seeing is the default index.html page that is created by the CLI when you create a new project.
You should adapt this index.html page as you whish (i.e. by modifying the HTML/CSS/JS code of this page, in your local project directory) and re-deploy the project with
firebase deploy
or
firebase deploy --only hosting
Go to your firebase.json file and make sure that the 'public' key under "hosting" is set to "build" like below:
"hosting": {
"public": "build"
}
It was likely set to "public" by default when you ran firebase init. In which case it would look like this:
"hosting": {
"public": "public"
}
The problem with the default is that React places all your static assets in the 'build' directory when you run npm run build, so that is where you want to point firebase to.
I faced the similar issue, my application was on react built using create react app. I was trying to deploy via firebase but getting the default page of firebase then i figured out that I havent build my application so firebase was not able to find my application html file and hence it was trying to deploy the default one.
I solved it by running yarn build, then when I do firebase init selected "hosting" option . After this step while selecting folder to deploy rather public I selected "build" and this solved my problem and my application got hosted. Hope this helps!!
For Angular 8.
If the name of your project is "ManyuDictionary".
1)Run firebase login.
2)Run firebase init and choose:
a) hosting
b) What do you want to use as your public directory? dist/ManyuDictionary
c) Configure as a single-page app (rewrite all urls to /index.html)? Yes.
Firebase will now write configuration info to firebase.json and project
information to .firebaserc.
3)Run ng build --prod.This will replace the default index.html in dist/ManyuDictionary.
4)Run firebase deploy.
And here is your website [https://manyudictionary.firebaseapp.com].
For Angular 7+, here is the straight forward solution that works for me as of August-2019
Just delete the index.html file at the root of your /dist folder.
Then, copy all the content inside the directory(named like your project main project folder's name) found in your /dist folder
Lastly, rerun firebase deploy
Enjoyed!
Solution that worked for me as of 9/17/2020
1- Finish the firebase init steps.
2- Add this to your firebase.json file:
//"public": should be, dist + project name on your computer
//"site": should be the app name which you created on your firebase cloud
{
"hosting": {
"public": "dist/your-project-name",
"site": "firebase-project-name",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
3- ng build --prod
4- firebase deploy
Firebase asks wehn you type firebase init hosting.
File dist/your-project/index.html already exists. Overwrite?
Press
NO
And you are good to go!
If you follow firebase Hosting steps, but not solve your problem.Then you can follow this step for solving your problem:
Deploy your project in netlify.
Take the live project link from netlify.
Go to firebase > your project > Hosting > Dashboard.
Click the Add custom domain Button.
Paste the netlify project live link in input box.
Click the Continue button.
That's ok.

Unable to display website using Firebase Hosting

I am trying to host my dynamic Node.js website using Firebase Hosting but I am having difficulty based on my file structure. Here is what is looks like:
In my code, I am not using an index.html file but when I ran firebase init it automatically created one for me along with a 404.html file. When I deploy my code, only index.html file seems to execute. But when I try deploying after deleting the index.html file, I can see the 404.html file.
This is the firebase.json file. that I am using:
{
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
None of the files in the 'public/html' folder seem to be executing. I tried changing the public field to public/html in the firebase.json file but then I can only see Cannot GET / when deployed. How do I run the files in the 'public/html' folder when deploying along with the js files in the 'src/js' folder?
I have an error.html file in my code which is located under 'public/html/main'. If I want to use it besides the 404.html file, will I have to place in directly inside the public folder?
Additionally, I also have a lot of backend files. In order to use those, do I need to use some other service offered by firebase or that can be accomplished by using Firebase Hosting itself?
With Firebase Hosting, if you want to see web content when you load the "/" path, you will need a file called "index.html" in your public directory. It will not look in subdirectories. By default, with the configuration you're showing, all the paths are served relative to public.
It's hard to tell what you're expecting to work differently. If you want to refer to foo.html nested under html, then your path will need to be "/html/foo.html". If you want to change the hosting configuration to do something different, I suggest looking over the documentation.

FIREBASE Hosting can't use the correct site

I have the following two hostings set up in my Firebase hosting. I don't know where the first one with the bad name came from, but anyway it's there and it's always coming up when I use firebase init from the command line as the only hosting I can use. Does anyone know how i can use Firebase init to connect my website to the second hosting address, that's the lbdsapp address? Also preferably I'd like to delete the lbds-aeb3d if possible and make the other one the default.F
Any help would be really appreciated. Thanks.
The first site lbds-aeb3d is the main site created from your project ID, When you create the project you probably entered lbds which is too short for a project ID, so some random string is automatically added. So you can not delete it since it's associated with your project.
Since you have added another site, that makes your project a multisites hosting project and you will have to configure .firebaserc and firebase.json accordingly.
You can start by giving your secondary site a name to refer to when you want set it as a deploy target. You can do this by the following command
firebase target:apply hosting <target-name> <resource-name>
In your case, if you call it myapp it would be
firebase target:apply hosting myapp lbdsapp
Then firebase will generate .firebaserc that include myapp as a target
Next, you'll have to define hosting configuration for your site in firebase.json
You will have to add the target name in the hosting config. Note that if you have more sites, you will have to configure hosting as array of config objects
{
"hosting": {
"target": "myapp",
"public": ...,
// ...
}
}
When you deploy your site, you will have to specify the target
firebase deploy --only hosting:myapp

Content not updating with firebase serve --only hosting

I'm trying to test changes to my site. But when I make changes and restart firebase serve --only hosting the changes aren't showing up.
I'm using Vuejs if that matters...
i had this problem but it turns out chrome cached my old version and is putting it every time
just disable the cache from chrome from devtools
https://www.technipages.com/google-chrome-how-to-completely-disable-cache
With Chrome, you can have the cache disable when DevTools are open by selecting the "Disable cache" option in the Network tab.
I had this issue as well. The easiest way of fixing this is to clear cache and cookies, and reload.
If I use npm run dev I see the content changes immediately with hot reloading. Not sure how npm run dev is different from firebase serve --hosting only or why you need firebase serve at all when developing hosting.
You need to run npm run build before firebase serve --hosting only too. When using firebase serve
The issues seems to be that the emulator hosting doesn't do hot reloading.
I just do this in the console
npm run build && firebase deploy
(To build into the dist folder AND deploy to firebase hosting)
my firebase.json file looks like this
{
"hosting": {
"public": "dist",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
When all this is done, I log into firebase hosting on the browser, and I delete the previous uploads. Though it will take time for Firebase to update its CDN (probably after 10 minutes), with the new code.

How to deploy express to Firebase?

Currently i'm learning express and Firebase. I can deploy an angular or react page (front-end) to Firebase.
And now, i'm trying to create the back-end using express, then i realize, i cant just simply deploying it as another project to Firebase since Firebase hosting will looking for index.html as entry point (*please correct me if i'm wrong). I've tried Firebase functions but still can't.
Can i set a javascript (expressjs) file to Firebase as entry point?
thanks.
You can deploy an Express app to Cloud Functions for Firebase as an HTTP trigger. There is a sample that illustrates this. The basic form of implementation looks like this in your Cloud Functions index.js:
const functions = require('firebase-functions')
const express = require('express')
const app = express()
// configure app here, add routes, whatever
exports.api = functions.https.onRequest(app)
Now you can access your routes under the path https://your-assigned-hostname/api/whatever
EDIT
You also need to configure your firebase.json file if you want to redirect every request to the Express app.
Add this line inside the hosting object: "rewrites" : [{"source" : "**", "function" : "api"}]

Resources