firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.02k stars 941 forks source link

Error deploying Next.js site using firebase hosting #5692

Closed sudhxnva closed 1 year ago

sudhxnva commented 1 year ago

Environment info

firebase-tools: 11.27.0

Platform: macOS

Test case

Deploying a boilerplate Next.js application using Firebase Hosting

Steps to reproduce

Enable the web frameworks preview:

firebase experiments:enable webframeworks

Generate a boilerplate Next.js app with the CLI:

firebase init hosting

Try to deploy it:

firebase deploy --only hosting

Expected behavior

App gets deployed on firebase hosting

Actual behavior

CLI recognizes that it needs to build a Cloud Function since it contains dynamic routes, but it fails at the time of creating this Cloud function:

i  deploying functions, hosting
i  functions: preparing codebase firebase-frameworks-<app-id> for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
⚠  functions: package.json indicates an outdated version of firebase-functions. Please upgrade using npm install --save firebase-functions@latest in your functions directory.
⚠  functions: Please note that there will be breaking changes when you upgrade.
i  functions: Loaded environment variables from .env.
i  functions: preparing .firebase/<app-id>/functions directory for uploading...
i  functions: packaged <root-dir>/.firebase/<app-id>/functions (1.66 MB) for uploading
i  functions: ensuring required API run.googleapis.com is enabled...
i  functions: ensuring required API eventarc.googleapis.com is enabled...
i  functions: ensuring required API pubsub.googleapis.com is enabled...
i  functions: ensuring required API storage.googleapis.com is enabled...
✔  functions: required API eventarc.googleapis.com is enabled
✔  functions: required API storage.googleapis.com is enabled
✔  functions: required API run.googleapis.com is enabled
✔  functions: required API pubsub.googleapis.com is enabled
i  functions: generating the service identity for pubsub.googleapis.com...
i  functions: generating the service identity for eventarc.googleapis.com...
✔  functions: .firebase/<app-id>/functions folder uploaded successfully
i  hosting[<app-id>]: beginning deploy...
i  hosting[<app-id>]: found 14 files in .firebase/<app-id>/hosting
✔  hosting[<app-id>]: file upload complete
i  functions: creating Node.js 16 function firebase-frameworks-serkl-app:ssrserklapp(us-central1)...
⚠  functions: HTTP Error: 400, Could not create Cloud Run service ssrserklapp. spec.template.spec.containers.resources.limits.cpu: Invalid value specified for cpu. For the specified value, maxScale may not exceed 10.
Consider running your workload in a region with greater capacity, decreasing your requested cpu-per-instance, or requesting an increase in quota for this region if you are seeing sustained usage near this limit, see https://cloud.google.com/run/quotas. Your project may gain access to further scaling by adding billing information to your account.

My guess from going through the logs is that the CLI is requesting for 80 max instances, but when I try to manually create a Cloud Function in the GCP console, it only allows me to set max instances 10. I already have a few Cloud Functions deployed via Firebase, not sure if that reduces the number of instances I can create.

Debug Logs

[info] ✔  hosting[<app-id>]: file upload complete 
[debug] [2023-04-14T13:08:09.521Z] [hosting] deploy completed after 3537ms
[info] i  functions: creating Node.js 16 function firebase-frameworks-<app-id>:srrapplication(us-central1)... 
[debug] [2023-04-14T13:08:09.525Z] >>> [apiv2][query] POST https://cloudfunctions.googleapis.com/v2/projects/<app-id>/locations/us-central1/functions functionId=srrapplication
[debug] [2023-04-14T13:08:09.525Z] >>> [apiv2][body] POST https://cloudfunctions.googleapis.com/v2/projects/<app-id>/locations/us-central1/functions {"name":"projects/<app-id>/locations/us-central1/functions/srrapplication","buildConfig":{"runtime":"nodejs16","entryPoint":"srrapplication","source":{"storageSource":{"bucket":"gcf-v2-uploads-redacted-us-central1","object":"redacted.zip"}},"environmentVariables":{"GOOGLE_NODE_RUN_SCRIPTS":""}},"serviceConfig":{"environmentVariables":{"__FIREBASE_FRAMEWORKS_ENTRY__":"next.js","FIREBASE_CONFIG":"{\"projectId\":\"<app-id>\",\"storageBucket\":\"<app-id>.appspot.com\",\"locationId\":\"us-central\"}","GCLOUD_PROJECT":"<app-id>","EVENTARC_CLOUD_EVENT_SOURCE":"projects/<app-id>/locations/us-central1/services/srrapplication"},"availableMemory":"256Mi","maxInstanceRequestConcurrency":80,"availableCpu":"1"},"labels":{"deployment-tool":"cli-firebase","firebase-functions-codebase":"firebase-frameworks-<app-id>","firebase-functions-hash":"redacted"}}
[debug] [2023-04-14T13:08:11.258Z] <<< [apiv2][status] POST https://cloudfunctions.googleapis.com/v2/projects/<app-id>/locations/us-central1/functions 400
[debug] [2023-04-14T13:08:11.258Z] <<< [apiv2][body] POST https://cloudfunctions.googleapis.com/v2/projects/<app-id>/locations/us-central1/functions {"error":{"code":400,"message":"Could not create Cloud Run service srrapplication. spec.template.spec.containers.resources.limits.cpu: Invalid value specified for cpu. For the specified value, maxScale may not exceed 10.\nConsider running your workload in a region with greater capacity, decreasing your requested cpu-per-instance, or requesting an increase in quota for this region if you are seeing sustained usage near this limit, see https://cloud.google.com/run/quotas. Your project may gain access to further scaling by adding billing information to your account.","status":"INVALID_ARGUMENT"}}
[warn] ⚠  functions: HTTP Error: 400, Could not create Cloud Run service srrapplication. spec.template.spec.containers.resources.limits.cpu: Invalid value specified for cpu. For the specified value, maxScale may not exceed 10.
Consider running your workload in a region with greater capacity, decreasing your requested cpu-per-instance, or requesting an increase in quota for this region if you are seeing sustained usage near this limit, see https://cloud.google.com/run/quotas. Your project may gain access to further scaling by adding billing information to your account. 
[warn] ⚠  functions: failed to create function projects/<app-id>/locations/us-central1/functions/srrapplication 
[error] Failed to create function projects/<app-id>/locations/us-central1/functions/srrapplication
[debug] [2023-04-14T13:08:11.260Z] Total Function Deployment time: 1736
[debug] [2023-04-14T13:08:11.260Z] 1 Functions Deployed
[debug] [2023-04-14T13:08:11.260Z] 1 Functions Errored
[debug] [2023-04-14T13:08:11.261Z] 0 Function Deployments Aborted
[debug] [2023-04-14T13:08:11.261Z] Average Function Deployment time: 1735
[info] 
[info] Functions deploy had errors with the following functions:
    firebase-frameworks-<app-id>:srrapplication(us-central1)
[debug] [2023-04-14T13:08:11.262Z] Not printing URL for HTTPS function. Typically this means it did not match a filter or we failed deployment
[info] i  functions: cleaning up build files... 
[debug] [2023-04-14T13:08:11.265Z] >>> [apiv2][query] DELETE https://artifactregistry.googleapis.com/v1beta2/projects/<app-id>/locations/us-central1/repositories/gcf-artifacts/packages/srrapplication [none]
[debug] [2023-04-14T13:08:11.266Z] >>> [apiv2][query] GET https://us.gcr.io/v2/<app-id>/gcf/us-central1/tags/list [none]
[debug] [2023-04-14T13:08:11.736Z] <<< [apiv2][status] GET https://us.gcr.io/v2/<app-id>/gcf/us-central1/tags/list 200
[debug] [2023-04-14T13:08:11.736Z] <<< [apiv2][body] GET https://us.gcr.io/v2/<app-id>/gcf/us-central1/tags/list {"child":[],"manifest":{},"name":"<app-id>/gcf/us-central1","tags":[]}
[debug] [2023-04-14T13:08:11.737Z] Could not find image for function projects/<app-id>/locations/us-central1/functions/srrapplication
[debug] [2023-04-14T13:08:13.186Z] <<< [apiv2][status] DELETE https://artifactregistry.googleapis.com/v1beta2/projects/<app-id>/locations/us-central1/repositories/gcf-artifacts/packages/srrapplication 404
[debug] [2023-04-14T13:08:13.186Z] <<< [apiv2][body] DELETE https://artifactregistry.googleapis.com/v1beta2/projects/<app-id>/locations/us-central1/repositories/gcf-artifacts/packages/srrapplication {"error":{"code":404,"message":"Package \"projects/<app-id>/locations/us-central1/repositories/gcf-artifacts/packages/srrapplication\" was not found.","status":"NOT_FOUND"}}
[debug] [2023-04-14T13:08:13.187Z] Functions deploy failed.
[debug] [2023-04-14T13:08:13.188Z] {
  "endpoint": {
    "id": "srrapplication",
    "project": "<app-id>",
    "region": "us-central1",
    "entryPoint": "srrapplication",
    "platform": "gcfv2",
    "runtime": "nodejs16",
    "httpsTrigger": {},
    "labels": {
      "deployment-tool": "cli-firebase"
    },
    "environmentVariables": {
      "__FIREBASE_FRAMEWORKS_ENTRY__": "next.js",
      "FIREBASE_CONFIG": "{\"projectId\":\"<app-id>\",\"storageBucket\":\"<app-id>.appspot.com\",\"locationId\":\"us-central\"}",
      "GCLOUD_PROJECT": "<app-id>",
      "EVENTARC_CLOUD_EVENT_SOURCE": "projects/<app-id>/locations/us-central1/services/srrapplication"
    },
    "codebase": "firebase-frameworks-<app-id>",
    "cpu": 1,
    "concurrency": 80,
    "targetedByOnly": true,
    "hash": "redacted"
  },
  "op": "create",
  "original": {
    "name": "FirebaseError",
    "children": [],
    "context": {
      "function": "projects/<app-id>/locations/us-central1/functions/srrapplication"
    },
    "exit": 1,
    "message": "Failed to create function projects/<app-id>/locations/us-central1/functions/srrapplication",
    "original": {
      "name": "FirebaseError",
      "children": [],
      "context": {
        "body": {
          "error": {
            "code": 400,
            "message": "Could not create Cloud Run service srrapplication. spec.template.spec.containers.resources.limits.cpu: Invalid value specified for cpu. For the specified value, maxScale may not exceed 10.\nConsider running your workload in a region with greater capacity, decreasing your requested cpu-per-instance, or requesting an increase in quota for this region if you are seeing sustained usage near this limit, see https://cloud.google.com/run/quotas. Your project may gain access to further scaling by adding billing information to your account.",
            "status": "INVALID_ARGUMENT"
          }
        },
        "response": {
          "statusCode": 400
        }
      },
      "exit": 1,
      "message": "HTTP Error: 400, Could not create Cloud Run service srrapplication. spec.template.spec.containers.resources.limits.cpu: Invalid value specified for cpu. For the specified value, maxScale may not exceed 10.\nConsider running your workload in a region with greater capacity, decreasing your requested cpu-per-instance, or requesting an increase in quota for this region if you are seeing sustained usage near this limit, see https://cloud.google.com/run/quotas. Your project may gain access to further scaling by adding billing information to your account.",
      "status": 400
    },
    "status": 400
  }
}
[debug] [2023-04-14T13:08:13.189Z] Error: Failed to create function srrapplication in region us-central1
    at /Users/<user>/.nvm/versions/node/v16.15.0/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:48:11
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Fabricator.createV2Function (/Users/<user>/.nvm/versions/node/v16.15.0/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:271:32)
    at async Fabricator.createEndpoint (/Users/<user>/.nvm/versions/node/v16.15.0/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:132:13)
    at async handle (/Users/<user>/.nvm/versions/node/v16.15.0/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:85:17)
[error] 
[error] Error: There was an error deploying functions
google-oss-bot commented 1 year ago

This issue does not have all the information required by the template. Looks like you forgot to fill out some sections. Please update the issue with more information.

sudhxnva commented 1 year ago

@google-oss-bot The issue is up to date with all the fields filled out

coderDove commented 1 year ago

@sudhxnva experienced same issue - looks like there is a disconnect between correct Cloud Functions config and what Firebase tools actually try to apply. Hosting deploy tries to set as a max instance count to extremely big numbers - in my case according to logs it was 3000 that is definitely failing. You can manually set the max instance count in frameworksBackend config in firebase.json. In fact you can set any config value that can cause deployment problem in frameworksBackend and it will be used during deployment.

Working firebase.json, checked with Firebase tools v11.30.0

{
  "hosting": {
    "source": ".",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "frameworksBackend": {
      "region": "us-east1",
      "maxInstances": 10
    }
  }
}
sudhxnva commented 1 year ago

@coderDove Thank you! I will try this solution :)

JocheRDevelopment commented 3 months ago

I am using Firebase App Hosting, which is different from Firebase Hosting as it can deploy from your GitHub repo each time you push a new commit. I noticed in the logs that it was assigning more than 10 maxInstances:

INFO 2024-08-03T17:09:38.003483886Z Step #3: runConfig:
INFO 2024-08-03T17:09:38.003485352Z Step #3: cpu: 1
INFO 2024-08-03T17:09:38.003486074Z Step #3: memoryMiB: 512
INFO 2024-08-03T17:09:38.003486968Z Step #3: concurrency: 80
INFO 2024-08-03T17:09:38.003487545Z Step #3: maxInstances: 100
INFO 2024-08-03T17:09:38.003488121Z Step #3: minInstances: 0

To address this, I added the following to my apphosting.yaml:

runConfig: cpu: 1 memoryMiB: 512 concurrency: 80 maxInstances: 2 minInstances: 0