How to Create Multiple Vue Apps Under One Project - firebase

I'm new to Vue js and I'm developing an e-commerce project with firebase. I saw in firebase there are multiple hosting features which will help me to host client and admin side in two different domains. I don't know how to create two apps in one project folder like this example in angular. If any knows please share your thoughts

Follow this
Step 0 - Firebase setup
In this tutorial my-app is my Firebase project id
my-app-public Web App is associate to my-app Hosting site (default hosting site takes the project id as name)
my-app-admin Web App is associate to my-app-admin Hosting site
Step 1
Move your public and admin apps in a new directory
my-app/
├── public-site/
└── admin-site/
Step 2
Init from my-app/ with firebase and of course select Hosting feature to set up
firebase init
Do not pay attention to firebase asking for the public name folder
Step 3
Set the build path of your apps to the targets public and admin.
Edit the firebase.json file and add your apps.
{
"hosting": [
{
"target": "public",
"public": "public-site/dist",
"ignore": [
"**/.*",
"**/node_modules/**"
]
},
{
"target": "admin",
"public": "admin-site/dist",
"ignore": [
"**/.*",
"**/node_modules/**"
]
}
],
}
Step 4
Link the targets to yours Firebase Hosting Sites. (Hosting Site != Web App)
Edit the .firebaserc, if it doesn't exist create it at the root of my-app/
{
"projects": {
"default": "my-app", # firebase project id
},
"targets": {
"my-app": { # firebase project id
"hosting": {
"public": [
"my-app" # public hosting site id
],
"admin": [
"my-app-admin" # admin hosting site id
]
}
},
}
}
Now if you type
firebase target
You should get
[ hosting ]
public (my-app)
admin (my-app-admin)
Step 5
Build your both apps
cd public-site/
npm run build
Make sure your apps are build in the dist/ folder
Step 6
Deploy both apps
firebase deploy --only hosting
Or deploy only the public app
firebase deploy --only hosting:public
Your apps should be deployed to
https://my-app.firebaseapp.com/
https://my-app-admin.firebaseapp.com/
BONUS - Build and deploy your apps on merge on master (Github)
First use
firebase init hosting:github
and follow the CLI instructions, it should ask your repo
One it's done check if your repo has the firebase service account key
Go on your github repo > settings > security
And you should see a secret env variable named FIREBASE_SERVICE_ACCOUNT_MY_APP
Let's setup your github workflow in your project, remove the .yml files in .github/workflows generated by the previous command and create your own workflow (file name doesn't matter):
name: Deploy on PROD to Firebase Hosting
'on':
push:
branches:
- master # branch to target
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Install dependencies & build public-site
run: npm ci && npm run build
working-directory: public-site
- name: Install dependencies & build admin-site
run: npm ci && npm run build
working-directory: admin-site
- name: Deploy websites
uses: FirebaseExtended/action-hosting-deploy#v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_MY_APP }}' # edit here
projectId: my-app # edit here
channelId: live
So every push/merge on master will be build each apps and deploy them on Firebase Hosting

Related

"Cloud Functions API has not been used in project" when only using hosting

I initialized my project for hosting, using the firebase cli. I also let it automatically create the CI script for GitHub.
The configs look like this:
{
"projects": {
"default": "project-name"
}
}
{
"hosting": {
"public": "dist",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
name: deploy on push
"on":
push:
branches:
- main
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- run: yarn && yarn build
- uses: FirebaseExtended/action-hosting-deploy#v0
with:
repoToken: "${{ secrets.GITHUB_TOKEN }}"
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT_PROJECT_NAME }}"
channelId: live
projectId: project-name
But once the CI script runs on my actions I get the following error:
[2022-10-06T15:45:17.926Z] <<< [apiv2][body] GET https://cloudfunctions.googleapis.com/v1/projects/project-name/locations/-/functions ***"error":***"code":403,"message":"Cloud Functions API has not been used in project 123123123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.","status":"PERMISSION_DENIED","details":[***"#type":"type.googleapis.com/google.rpc.Help","links":[***"description":"Google developers console API activation","url":"https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123"***]***,***"#type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"SERVICE_DISABLED","domain":"googleapis.com","metadata":***"service":"cloudfunctions.googleapis.com","consumer":"projects/123123123"***]***
[2022-10-06T15:45:17.930Z] [functions] failed to list functions for project-name
[2022-10-06T15:45:17.930Z] [functions] HTTP Error: 403, Cloud Functions API has not been used in project 123123123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
[2022-10-06T15:45:17.931Z] FirebaseError: HTTP Error: 403, Cloud Functions API has not been used in project 123123123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/cloudfunctions.googleapis.com/overview?project=123123123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
at responseToError (/home/runner/.npm/_npx/7750544ccf494d8b/node_modules/firebase-tools/lib/responseToError.js:49:12)
at RetryOperation._fn (/home/runner/.npm/_npx/7750544ccf494d8b/node_modules/firebase-tools/lib/apiv2.js:288:77)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Error: Failed to list functions for project-name
The process '/usr/local/bin/npx' failed with exit code 1
Error: The process '/usr/local/bin/npx' failed with exit code 1
The thing is, I only want to use the hosting capabilities of firebase and have no plans of enabling firebase functions, so why does it require me to do so? I have created projects in the past, which did not have this requirement.
So it seems like it is a problem with the newest firebase-tools version. After downgrading from 11.14.0 to 11.13.0 locally, it worked again (which I recognized thanks to this comment).
So to make it run in CI, I just installed firebase-tools#11.13.0 as a dev-dependency directly into the project and CI ran through fine again.
Per the error, your script is calling two GitHub Actions and one of these is calling GET https://cloudfunctions.googleapis.com/v1/projects/{project}/locations/-/functions to list the Functions in the Project. This method requires the (cloudfunctions) service to be enabled.
Either enable the service in the project so that the call succeeds
Or remove the step in the script that makes the call.

Github Actions - How can I make my env variable(stored in .env file) available in my workflow

I'll try to be as clear as possible. I have also asked about related issues but didn't receive a convincing response.
I'm using React and firebase for hosting.
Also, I'm storing my firebase web API key in my .env file.
I set up firebase hosting using Firebase CLI and chose to automatically deploy on merge or pull request.
After the setup finished a .github folder with .yml file was created in my working directory.
.github
- workflows
-firebase-hosting-merge.yml
-firebase-hosting-pull-request.yml
So now when I deploy my project(without pushing to GitHub) manually to firebase by running firebase deploy everything works fine and my app is up and running.
However when I make changes and push my changes to Github. Github actions are triggered and the automatic deployment to the firebase process starts. The build passes all the checks. However, when I visit the hosted URL there is an error I get in the console saying Your API key is invalid, please check you have copied it correctly.
I tried few workarounds like storing my firebase web API key into the Github secrets and accessing it in my .yml file.
# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools
name: Deploy to Firebase Hosting on merge
'on':
push:
branches:
- master
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- run: npm ci && npm run build --prod
- uses: FirebaseExtended/action-hosting-deploy#v0
with:
repoToken: '${{ secrets.GITHUB_TOKEN }}'
firebaseServiceAccount: '${{ secrets.FIREBASE_SERVICE_ACCOUNT_EVENTS_EASY }}'
channelId: live
projectId: my-project
env:
REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}}
FIREBASE_CLI_PREVIEWS: hostingchannels
But I am still getting the error. I feel that the error is definitely due to the environment variables.
I have stored my firebase web API key in my .env.production file located in the root directory.
Somehow GitHub actions are not using my environment variables defined.
Please let me know how can I manage my env variables so that it can be accessed by my workflow.
The answer is put custom env vars in first level before jobs:
name: Deploy to Firebase Hosting on merge
'on':
push:
branches:
- master
env: # <--- here
REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}} # <--- here
jobs:
build_and_deploy:
...
And add this secrets in Github > Your project > Settings > Secrets
You can use Create Envfile Github Action to create a .env file in your workflow.
To add a key to the envfile, add a key/pair to the with: section. It must begin with envkey_.
steps:
- uses: actions/checkout#v2
- name: Use Node.js
uses: actions/setup-node#v1
- name: Make envfile
uses: SpicyPizza/create-envfile#v1
with:
envkey_REACT_APP_API_KEY: ${{secrets.REACT_APP_API_KEY}}
directory: './'
file_name: '.env'

Configure firebase for sub-app within a folder

I have used firebase hosting to host my app in the root however I would like to serve a separate codebase for my forum under https://myapp.com/forum/
I created two targets: app for my root app in one repository and forum for my other repository. I also created two sites in Firebase.
My question is: is it even possible to have two separate repos and use firebase deploy to have one project under root and other under /forum/
firebase.json (app):
"rewrites": [
{
"source": "!/forum/**",
"destination": "/appIndex.html"
}
]
firebase.json (forum):
"rewrites": [
{
"source": "/forum/**",
"destination": "/forumIndex.html"
}
]
I would like Firebase to show app in the root and forum when I point to mydomain.com/forum/
You can maintain your code in two different repos but once you want to deploy to firebase you'll have to build both and deploy both from the same directory.

Firebase hosting deploy to other site

how to deploy to other firebase hosting sites defined within the same project.
I created multiple firebase hosting "sites".
The command
firebase deploy
however always deploys to the first one.
How can I specify that the static files get deployed to another "site" (and domain).
Thanks
You have to add the other sites as deploy targets. If you have a second site named awesome-site-d3426:
$ firebase target:apply hosting awesome-app awesome-site-d3426
You'll likely have to do the same thing for the primary site.
Then tell Firebase what to deploy to which targets in firebase.json:
{
"hosting": [
{
"target": "awesome-site",
"public": "awesome-site/dist"
},
{
...
}
]
}
You can then deploy all the sites at once(firebase deploy) or a specific site:
$ firebase deploy --only hosting:awesome-site
To deploy to another site created in same firebase project.
Update your firebase.json file as folow
{
"hosting": {
"site":"<site-name>",
"public": ...,
"ignore": [
...
],
"rewrites": [
...
]
}
}

AngularDart 5 and Firebase deployment

I’m trying to deploy my AngularDart 5 web app to Firebase. These are the steps I followed:
I builded my app with the command pub run build_runner build --output build.
I launched the command firebase init and setted web folder as the public folder.
I deployed my web app with the command firebase deploy.
⚠ The problem is that when I open the website I find only a blank page.
❔ What I’m doing wrong?
Thank you!
Yes, I am still having this issue. The content of my firebase.json is:
{
"hosting": {
"public": "web",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
I don’t know if it helps, but my pubspec.yaml file is:
name: angular_app
description: Angular App
version: 0.0.1
environment:
sdk: '>=2.0.0-dev.63.0 <2.0.0'
dependencies:
sass_builder: ^2.0.2
angular: ^5.0.0-alpha+15
angular_forms: ^2.0.0-alpha+7
angular_router: ^2.0.0-alpha+14
dev_dependencies:
angular_test: ^2.0.0-alpha+13
build_runner: ^0.8.9
build_test: ^0.10.2+5
build_web_compilers: ^0.4.0+4
test: ^1.0.0
When I deploy on firebase or when I serve the app with firebase serve, I see only “Loading...”.
Thanks for your help!
In your step 3, you need to use firebase deploy to deploy rather than pub.
In your firebase.json file, the hosting.public entry shown above is just "web" but your build output directory is "build". The entry should probably be "build/web".
Are you setting <base href> in your index.html? If so, to which href value?
You wrote that when you serve you only see “Loading...”. Open then JavaScript console. Do you see any error messages?

Resources