Run firebase cli on Cloud Run - firebase

What I want to acheieve
So using Cloud Run, I wanted to achieve as below
Firebase Cloud Function(FCF) invoked
FCF makes a call to the other API service and fetch a certain value {bar:baz}
FCF makes a call to Cloud Run endpoint with the value as params
Cloud Run that was built and deployed with Docker Image of Firebase CLI gets triggered
in Cloud Run, it executes the commands firebase functions:config:set foo.bar=baz
Successfully updates FCF environment variables as foo.bar=baz
Issues
so previously asked here, Google Cloud Run - Run Shell Script in firebase project and with the solutions that were provided by #guillaume blaquiere, I finally got to the point where I run commands like firebase --version or firebase projects:list and they worked fine as the information is printed out on the browser.
However, when I run the commands like firebase use project test123 or firebase functions:config:set foo.bar=baz, Cloud Run throws the error exec error: exit status 1
Source Code
# Dockerfile
FROM node
RUN npm i -g firebase-tools
ADD my_script.sh /
COPY --from=msoap/shell2http /app/shell2http /shell2http
RUN chmod +x my_script.sh
ENTRYPOINT ["/shell2http","-export-all-vars"]
CMD ["/update","/my_script.sh"]
#my_script.sh
#!/bin/sh
firebase --token $TOKEN use foobarbaz-123
firebase functions:config:set foo.bar=baz
when build image with gcb
gcloud builds submit --tag gcr.io/foobarbaz-123/testbash
when deploy
gcloud run deploy --image gcr.io/foobarbaz-123/testbash --platform managed
or deploy with token that I get from firebase login:ci
gcloud run deploy --image gcr.io/foobarbaz-123/testbash --platform managed --set-env-vars=TOKEN=foobarbaz12345
where do I need to fix? Any advices or suggestions will be appreciated

Related

Where can I find firebase cloud function deploy logs? Path starts with /www-data-home/.npm/_logs/

I am getting an error message
Build failed: npm ERR! Cannot read property 'firebase-admin' of undefined\n\nnpm ERR!
A complete log of this run can be found in:\nnpm ERR!
/www-data-home/.npm/_logs/2022-11-23T04_36_44_234Z-debug.log; Error ID: beaf8772"
Where is this www-data-home location?
I was having the same error, updated package.json to use engine: 16, deployed again and then it worked (believe it forced updating all npm that could have been corrupted remotely).
As mentioned in this documentation on Viewing logs
Logs for Cloud Functions are viewable either in the Google Cloud
Console, Cloud Logging UI, or via the firebase command-line tool.
Using the Firebase CLI To view logs with the firebase tool, use the
functions:log command: firebase functions:log
To view logs for a specific function, provide the function name as an
argument:
firebase functions:log --only <FUNCTION_NAME> For the full range
of log viewing options, view the help for functions:log:
firebase help functions:log
You can view logs for Cloud Functions in the Cloud Logging UI using
filter as those resource types
resource.type="cloud_function"
Refer this npm docs on npm logging.
The npm CLI has various mechanisms for showing different levels of
information back to end-users for certain commands, configurations &
environments.
All logs are written to a debug log, with the path to that file
printed if the execution of a command fails.You can find the
npm-debug.log file in your .npm directory. To find your .npm
directory, use npm config get cache.
You can also check this stackoverflow thread

Could not save data to emulator - "Did not find any running emulators for project "

when I running fireabse. i am running the following command :
"serve": "firebase use dev && firebase emulators:start --import=./emulator-data"
i have added data but I can not now save the data to the emulator
and I have got :
Error: Did not find any running emulators for project test-dev.
the command that i run is :
firebase emulators:export ./emulator-data
how can I fixed it ?
i verified the project is connected on gcloud and everything was working as it should
In order to troubleshoot this error you can follow the following steps as mentioned in Github_issue
ran sudo firebase emulators:export ./exported and got a new error
file not found!!
control c the running firebase process then ran it
again and got the same file not found error!!
command quit both
terminal windows
deleted the log files
ran the firebase emulator
again with "sudo firebase emulators:start --import ./data
--export-on-exit" (works)
opened another terminal window and "sudo firebase emulators:export ./exported" and it worked!!
Sometimes this issue can occur due to installations of firebase-tools path and npm and node paths are different. You may have a look at the link.
For additional information on emulators, you can follow the firebase documentation.

Firebase and Gitlab continuous integration pipeline failing

I am trying to hook up a simple static website to firebase hosting from Gitlab using their continuous integration tab. After downloading and installing firebase command line tools I am here:
stages:
- deploy
deploy-prod:
stage: deploy
only:
- master
script:
- firebase use <project-name> --token $FIREBASE_TOKEN
- firebase deploy --only hosting -m "Pipe $CI_PIPELINE_ID Build $CI_BUILD_ID" --token $FIREBASE_TOKEN
After that in firebase I deploy the site. However, gitlab then reports that the job is not deployed because
Error: firebase use must be run from a Firebase project directory.
Run firebase init to start a project directory in the current folder.
I dont understand the error, as I have in fact run firebase in the project director. Or is the error referring to the image thats in the yml file?
Either way, could someone please help me out? I've found a number of blog posts using CI in gitlab with firebase but I am stuck.
I got same issue and solved it by check folder structure
in my case:
/root/xx_project
firebase.json is in xx_project
so I need to cd xx_project

Firebase deploy function --non-interactive

I am integrating CI/CD for deploying my firebase functions.
firebase use PROJECTID --token FIREBASE_TOKEN
firebase deploy --token FIREBASE_TOKEN --non-interactive
Now whenever a function is deleted from index.js, it throws the following exception.
Error: The following functions are found in your project but do not
exist in your local source code: httpSeeding(us-central1) Aborting
because deletion cannot proceed in non-interactive mode. To fix,
manually delete the functions by running: firebase functions:delete
httpSeeding --region us-central1
Is there a way in the non-interactive mode where a deleted functions are removed from the console without running firebase functions:delete httpSeeding??
Run deploy with -f option, it should delete extra functions in non-interactive mode
firebase deploy --token FIREBASE_TOKEN -f
From the docs available by running firebase help deploy, we see following option:
-f, --force delete Cloud Functions missing from the current working directory without confirmation
Just run the same deploy command from your shell. It will automatically delete functions that are missing from your source.
Also you can delete functions directly from the Cloud console.

How can I automate "firebase deploy"?

Is there any way I can automate my "firebase deploy" pushes to Firebase Hosting using a script? I can't seem to figure out how to do this.
Thanks.
Here's a couple different approaches:
Firebase's own David East on how to do it with Travis and GitHub
https://www.youtube.com/watch?v=QLVzozWDYAs
I took an alternate approach, since my automation tool is written in java.
firebase CLI installed in the bash shell, previous to launching my java binary.
[deploy.sh] shell script runs "firebase deploy" command.
java binary has internal code to run the [deploy.sh] script.
Bottom line - no matter what your automation binary is written in, as long as it can shell out and run your [deploy.sh] script in the same shell that firebase is installed in, you should be good to go.
Open your terminal and run this command to generate a new CI token. this will help a lot if you are deploying your project on GitHub
npm install -g firebase-tools
firebase deploy --non-interactive --token $FIREBASE_TOKEN
to complete the automation process
firebase login:ci
personal suggestion: click here to see steps of using all commands

Resources