Skip to content

Support Firebase SDK auto-initialization for Cloud Run services - #11208

Merged
falahat merged 282 commits into
mainfrom
bapi_sdk_autoinit
Oct 16, 2026
Merged

falahat merged 282 commits into
mainfrom
bapi_sdk_autoinit

Conversation

@falahat

@falahat falahat commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

Links a Firebase Web App to a Cloud Run service so Firebase SDKs auto-initialize. The app ID is saved in the service's firebase.google.com/app-id annotation.

  • firebase init run asks which web app to link (default: create a new one). A new app is created only after all questions are answered, and init fails if it can't create one. An existing service keeps its app.
  • firebase run:services:update <service> --app <appId> links an app; --clear-app unlinks it.
  • On deploy, the CLI fetches the linked app's config:
    • Build: sets FIREBASE_WEBAPP_CONFIG and FIREBASE_CONFIG.
    • Runtime: sets FIREBASE_CONFIG on the container, and removes it when unlinking.
    • Both always come from the linked app. To override auto-init, users pass their own config in code.
    • If the app can't be fetched, the deploy fails.
  • When a deploy links an app, the CLI checks whether the runtime service account has roles/firebase.sdkAdminServiceAgent. If not, or if it can't check, it prints the gcloud command to grant it. It never changes IAM.

Scenarios Tested

  • Unit tests: npx mocha src/deploy/run/*.spec.ts src/init/features/run.spec.ts
  • Manual:
    • init: picking an existing app links it without creating one; "Create a new web app" creates one only after the questions; quitting partway leaves nothing behind.
    • The client and Admin SDKs initialize from the linked app. A plain deploy keeps the link, --app switches it, --clear-app removes it, and a bad --app fails before deploying.
    • Without the role, Admin SDK Storage and Auth calls fail; the printed command fixes it.

Sample Commands

firebase init run
firebase run:services:update my-service --app
firebase run:services:update my-service --clear-app

@falahat

falahat commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@falahat
falahat added this pull request to stack #11209 September 30, 2026 04:19

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for linking and clearing a Firebase Web App associated with a Cloud Run service to enable SDK auto-initialization. It adds --app and --clear-app options to the update command, automatically links web apps during initialization, and injects the configuration into environment variables. Feedback on these changes includes wrapping getOrCreateWebApp in a try-catch block to prevent crashes from permission errors, skipping redundant redeployments when the app ID is already linked, using hasOwnProperty instead of the in operator to avoid prototype pollution, and replacing numeric length checks in object spreads to prevent evaluating to { ...0 }.

Comment thread src/init/features/run.ts Outdated
Comment thread src/deploy/run/update.ts Outdated
Comment thread src/deploy/run/prepare.ts Outdated
Comment thread src/deploy/run/deploy.ts Outdated
Comment thread src/deploy/run/prepare.ts Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the ability to link and clear a Firebase Web App for Cloud Run services, enabling Firebase SDK auto-initialization. It adds --app and --clear-app options to the run:services:update command, updates firebase init run to automatically associate a Web App, and injects the corresponding FIREBASE_CONFIG and FIREBASE_WEBAPP_CONFIG environment variables during preparation and deployment. The review feedback highlights two important improvements: first, wrapping the getOrCreateWebApp call in firebase init run with a try-catch block to prevent crashes on network or permission failures; second, throwing a FirebaseError if a newly requested Web App lookup fails during deployment, ensuring the service is not left in a partially configured state.

Comment thread src/init/features/run.ts Outdated
Comment thread src/deploy/run/prepare.ts Outdated
@falahat

falahat commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for linking and clearing a Firebase Web App on Cloud Run services to enable SDK auto-initialization, updating the CLI commands, deployment pipeline, and initialization flows accordingly. The feedback recommends moving the resolution of the default service account and project number inside the try-catch block to prevent deployment crashes, correcting the arguments passed to getProjectNumber, and utilizing the getError utility instead of manual type checks to safely handle unknown error types.

Comment thread src/deploy/run/prepare.ts
Comment thread src/deploy/run/prepare.ts Outdated
Comment thread src/deploy/run/prepare.ts Outdated
Comment thread src/deploy/run/args.ts Outdated
Comment thread src/deploy/run/deploy.ts Outdated
Comment thread src/deploy/run/prepare.spec.ts Outdated
Base automatically changed from bapi_build_env to main October 13, 2026 19:50
Comment thread src/deploy/run/deploy.ts Outdated
Comment thread src/deploy/run/deploy.ts Outdated
Comment thread src/deploy/run/deploy.ts
Comment thread src/deploy/run/deploy.ts Outdated
Name the case where a deploy unlinks the app. Keep an emptied env list instead of deleting it: to Cloud Run, [] and no env field mean the same thing. Always send the service's annotations, instead of only when the linked app changed, which removes the undefined return and the conditional update mask.
Comment thread src/deploy/run/deploy.ts Outdated
For an app ID with no app, the web app config lookup returns the project's config without an appId, so a typo in --app deployed and was saved on the service. Check that the app exists first, with the same check App Hosting's --app uses, so the deploy fails before building or saving anything.
@falahat
falahat requested a review from abhis3 October 14, 2026 19:23

@abhis3 abhis3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Happy to merge this as is since it's a prototype and we can clean it up later. Heads up you might need to squash your commits. Left a few AI comments that I thought were reasonable when comparing it to this design doc, feel free to fix/ignore as necessary. I don't think any are blocking the merge here.

Nits:

  • RUN_PERMISSIONS doesn't include firebase.clients.list/get or resourcemanager.projects.getIamPolicy, so CI identities pass the precheck and then fail mid-prepare.

  • A 403 on the IAM read is reported as "Failed to grant…".

  • annotations is in the update mask on every deploy, a full read-modify-write with no etag. Maybe include it only when the link changes.

  • appId and firebaseConfig can diverge; a single autoInit?: {appId, firebaseConfig} would prevent that.

  • The PR description is a bit stale: it says init prompts, it says a failed lookup only warns, and the error text has changed.

Comment thread src/deploy/run/prepare.ts Outdated
Comment thread src/deploy/run/deploy.ts
Comment thread src/init/features/run.ts Outdated
Comment thread src/deploy/run/prepare.ts
…e created

getOrCreateWebApp only warns, and returns nothing, when the project is out of web apps (App Hosting backends work without one). Init then deployed the service without a linked app, silently skipping Firebase SDK auto-initialization.
The CLI always sets FIREBASE_CONFIG on linked services; users override auto-init in code, not with the env var. Once Cloud Run injects it, only the annotation is needed.
The build env annotation could override FIREBASE_CONFIG and FIREBASE_WEBAPP_CONFIG for the build, while the container always got the app's FIREBASE_CONFIG. Now both come from the app: users override auto-init in code, not with these env vars.
@falahat
falahat merged commit d337928 into main Oct 16, 2026
51 checks passed
@falahat
falahat deleted the bapi_sdk_autoinit branch October 16, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants