How to use Firebase hosting preview channel to test cloud functions? - firebase

I have a back-end server built using firebase cloud functions and I'm trying to test it and have a shareable test link without having to deploy to the live channel.
I found this https://firebase.google.com/docs/hosting/test-preview-deploy#preview-channels but when I follow the instructions, it only deploys the static public folder. So any changes I make to the back-end have no effect. I can use the back-end using the preview URL but it's always the same version as the live build.
Am I missing something? Or is preview channels for static content only?

That's correct, preview channels currently only support static content + Firebase Hosting configuration. One way to work around this (admittedly not ideal), is to deploy the old and new versions of your Cloud Function as separate functions (e.g. app and appV2) and change the preview channel to point to the different function.

Related

Can Firebase Hosting redirect to a Cloud Run tag?

We use cloud run to generate preview URLs as well as production URLs. We use github actions to generate a tagged cloud run deployment of our python backend based on the PR (e.g. pr-12---abcxyz.web.app) and generate a front-end cloud run deployment for our Vue app. Our load balancer on GCP allows us to ensure that requests going to our LB are directed to our production Cloud Run deploys (e.g. tagged with prod)
We want to make use of firebase hosting for its edge deployment capabilities, but it looks like we cannot have firebase redirect to a specific tag, only the entire cloud run instance.
If it is not possible to redirect to a cloud run tag, how would you recommend we set up preview URLs with firebase + python backend? We want to make sure that the preview URL uses the front-end and back-end code from our monorepo so that we never have questions about what is running where.

how to automatically deploy to firebase on commit to main branch?

The Main part
I am trying to create a comfortable workflow for myself. I just want to know is there any way to automate deploys from GitHub Repo to Firebase hosting when I commit changes in the repo's main branch at my desktop and push origin to GitHub?
...possibly using "GitHub actions"...
Context and what I've tried
As on 15-Jan-2022 there are two ways I found of deploying website files to firebase hosting.
The de facto firebase cli way, where you use firebase deploy command to push files to firebase hosting.
The automated way using github actions, where the site is deployed automatically every time a pull request is created.
The first one is tedious, the second one is not my cup of cake. Especially because the workflow I want to setup is not too much git oriented, I don't use "pull requests" as far as I understand. I just commit changes directly in the main branch.
(That is so because, I am not a programmer, I just trying to create a blogging site using the Firebase hosting + GitHub repo)
I also referred https://medium.com/firebase-developers/the-comprehensive-guide-to-github-actions-and-firebase-hosting-818502d86c31
Use Deploy to live & preview channels via GitHub pull requests. It will work if you use pull requests to review changes or if you push commits directly to the primary branch.
I would recommend that you use pull requests though. It's a great flow to double check your changes and with the Firebase action it will deploy a temporary preview of the the changes so you can validate everything looks correct.

Firebase RemoteConfig - different configs for different channels

Context:
I'm building a recipe website with Angular frontend and Node.js express backend.
I'm using Heroku for the backend and trying to use Firebase for the frontend. The backend is not ready for use, and is unstable. The frontend is almost ready for v1. For now, all I have is the ability to retrieve a list of recipes from the GET /api/recipes endpoint, and display them (and search through the loaded recipes). Eventually, I plan to add the ability to add/edit/delete/search through the recipes, as well as other endpoints. For now, it works equally well with a static .json file or a dynamic GET endpoint.
In the process of adding this to the backend, the backend will naturally go down and may become temporarily unusable at times, so I'm of course going to have two deployments of the API, and two deployments of the frontend. Prod and Dev, Live and Preview. This should be easy with Heroku, but Firebase is giving me some problems.
I want to have different configs for different deployments. A way for the front-end to ask Firebase "Where's the API I'm supposed to use". This shouldn't go in the repository, code once deploy many, yada: https://12factor.net/codebase . But I haven't figured out how to do this with Firebase.
Goal:
Firebase Remote Config should use a condition such as App Channel to send a different api_url parameter based on what channel the deployment is. This should not be stored in the repository. It should not give away the other deployments etc.
Whatever config I use should be build-once deploy-many. I do not wish to pass a config parameter such as configs:{"recipes.web.app":{apiUrl:"..."}, "recipes-preview-1239j20.web.app":{apiUrl:"..."}}. The frontend must not be aware of other deployments. It shouldn't even really have to be aware of where it's being hosted.
Current Progress:
I successfully deployed different versions to the live channel and the preview channel. I successfully made a RemoteConfig config using GUI (https://console.firebase.google.com/project/<project_name>/config). I did see how to add conditions. I successfully accessed RemoteConfig inside my custom service in Angular. I did find out that init.json on the preview and live channels are identical. I did successfully limit it to "If it's the recipe app", but it's useless because everything's the recipe app. There is a way to make multiple sites, but that's not really useful either, because I'd have to set up the entire app again.
I did not manage to figure out how to deploy remoteconfig.template.json from command line. I did not manage to figure out what conditions are useful. I did not manage to get the "Version" condition to be enabled in the condition selector. I did not manage to find any condition about "channels". I did not manage to find anything with "remote config" and "channels" even on the same page! I did not manage to find anything in firebase docs about "deployments".
Alternative and Unrelated Sidenote:
I'm extremely frustrated with Firebase, and if I can't figure this out, I'm going to abandon it in favor of a Heroku thing I just found. An "unsupported" buildpack for nginx (https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nginx). "Unsupported" as in it's unofficial. I'm familiar with nginx, and using that will make everything about this personal project (except the server location and build/deploy process) match up with what I'm doing at work. And also both halves of my personal project (static web and api) will be on the same host, which might make things nice.

Firebase Remote Config - copy to another project

I have two projects for dev and prod. I want to be able to run a script to copy dev config to prod.
Firebase Remote Config has an API for programatically updating Remote Config. But as far as I can tell, you need to init admin with a project-specific service account. It seems like I would need two admin instances, but I'm not sure that's possible?
I'm wondering if someone has done this before and has an example script. Thanks!
See docs:
https://firebase.google.com/docs/remote-config/automate-rc
There is no Firebase Admin SDK for Flutter, so you'll have to implement this on a different platform that is supported. For a list of these platforms and instructions on setting it up, see the documentation on adding Firebase to a server.
For these platforms that the Firebase Admin SDK targets, you can create multiple instances of the FirebaseApp class, and initialize each of them with different credentials and project configuration. For examples of how to do this, see the documentation on initializing multiple apps.

How to use same function for Dev and Production - Firebase

I am new to firebase and cloud functions, please try to help.
I have an app that uses cloud function to call external APIs. As my app is now live and consuming those APIs through cloud functions. I have to develop new features in my app using Dev environment without disturbing Live functions.
There are two different URLs of external APIs for Dev and Production.
For example:
Dev = demoapi.example.com/someapi
Live = api.example.com/someapi
How can i only upload cloud functions to dev environment without disturbing LIVE app.
Do i have to write copy of each function in my index file? Or is there any other way? becuase there are around 20 functions.
Both Dev and Production DBs/Projects are different in firebase. But i can only pay for one blaze plan (as cloud function requires blaze plan to make external requests) so i want to use one project for functions.
Making copy of each function is not the right choice.

Resources