firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Hosting deploy fails with multiple sites in the project

Open arsen opened this issue 8 years ago • 18 comments

firebase --version : 4.2.1 os: Mac OS High Sierra (10.13.5)

After I have added a second site to the same project the deploy is failing. In the debug it is giving the following message when I run a deploy command: cmd: firebase deploy --only hosting:dealer

[info] Project Console: https://console.firebase.google.com/project/[project]-dev/overview [debug] [2018-08-31T18:22:18.699Z] TypeError: Cannot read property 'deploys' of undefined at /Users/[user]/.npm-global/lib/node_modules/firebase-tools/lib/deploy/index.js:105:32 at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:228:7) [error] [error] Error: An unexpected error has occurred.

And this is my .firebaserc file:

{
  "projects": {
    "default": "[name]-dev",
    "prod": "[name]-prod"
  },
  "targets": {
    "[name]-dev": {
      "hosting": {
        "dealer": [
          "[name]-dealer-dev"
        ],
        "consumer": [
          "[name]-dev"
        ]
      }
    }
  }
}

arsen avatar Aug 31 '18 18:08 arsen

Same here, on Ubuntu 18.04

jaonoctus avatar Sep 05 '18 03:09 jaonoctus

I also got this error but i fixed it by changing my firebase.json config to use an array with a hosting configuration for each site.

old firebase.json

{
  "hosting": {
    "public": "dist",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

updated firebase.json

{
  "hosting": [
    {
      "target": "production",
      "public": "dist",
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ]
    },
    {
      "target": "staging",
      "public": "dist",
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ]
    }
  ]
}

My .firebaserc

{
  "projects": {
    "default": "[project-id]"
  },
  "targets": {
    "[project-id]": {
      "hosting": {
        "production": [
          "[project-id]"
        ],
        "staging": [
          "[project-id]-staging"
        ]
      }
    }
  }
}

danielx avatar Sep 12 '18 09:09 danielx

Same problem and put an array didn't work.

gammafilho avatar Sep 15 '18 23:09 gammafilho

Can this be done in Spark Plan? Or Need an Upgrade!

vnoitkumar avatar Sep 20 '18 05:09 vnoitkumar

I was facing the same problem, I try @danielx suggestion and it works. So, this error occurred because the firebase.json file didn't get update when I run the firebase target:apply hosting target-name resource-name command.

sikloidz avatar Sep 28 '18 04:09 sikloidz

@vnoitkumar you must be on the Blaze plan to create multiple Hosting sites. See the note here: https://firebase.google.com/docs/hosting/multisites

bkendall avatar Sep 28 '18 17:09 bkendall

@sikloidz firebase target:apply doesn't change firebase.json - it only effects .firebaserc.

bkendall avatar Sep 28 '18 17:09 bkendall

@arsen Thanks for bringing this to our attention. It seems as though that happens when a hosting deploy target name that doesn't exist in firebase.json. I've brought this to the attention of the appropriate people. 😄

When using multiple Hosting sites, it's important to follow all the directions in the documentation in order for deployments to work. The cliff notes version going from one to two sites (where the project name is my-web-app:

  • create a new site in the Firebase Console under Hosting (e.g. new-site)
  • run firebase target:apply [some-target-name] my-web-app to give a name to to the original site
  • run firebase target:apply [new-target-name] new-site in the terminal in your project
  • update firebase.json to make the hosting object an array, being sure to specify in each element the target value
    • be sure to add "target": "[some-target-name]", to the original object so that the default site can continue to be deployed
    • a sample configuration may look like this (w/o comments, 'cause JSON):
{
  "hosting": [
    {
      "target": "some-target-name",
      "public": "dist", // this was the original deployed folder, now with `target` above
    },
    {
      "target": "new-target-name", // this is a new `target` and object
      "public": "new-dist",
    }
  ]
}
  • now firebase deploy --only hosting should deploy both some-target-name and new-target-name, and firebase deploy --only hosting:some-target-name will only deploy some-target-name.

Hope this helps!

bkendall avatar Sep 28 '18 17:09 bkendall

This happened to my when I got confused when I added multiple resources check your .firebaserc and in the array of the target name only the activated site-names must be present.

anaganisk avatar Oct 09 '18 07:10 anaganisk

I came here because I got the same deploy error when adding a second site to firebase hosting. I was able to resolve it using @danielx comment by updating the .firebaserc file.

Original: "targets": { ["project-name"]: { "hosting": { ["target:apply-name"]: [ ["project-name"], ["deployment-folder"] ], ["target-apply-name2"]: [ ["project-name2"], ["deployment-folder2"] ] } } }

Updated: "targets": { ["project-name"]: { "hosting": { ["target:apply-name"]: [ ["project-name"] ], ["target-apply-name2"]: [ ["project-name2"] ] } } }

fkajouii avatar Oct 28 '18 00:10 fkajouii

@bkendall For my use case, I want the exact same hosting settings for two deploy targets: stage and live. I just want to push different build to these two targets, so do I really need to duplicate the settings in firebase.json? I would prefer if it defaulted to using the non-array settings for both. Thanks!

emilsall avatar Nov 05 '18 23:11 emilsall

@emilsall As I recall, yes, the settings have to be duplicated.

bkendall avatar Nov 08 '18 17:11 bkendall

@bkendall Alright, thanks for the reply!

emilsall avatar Nov 08 '18 18:11 emilsall

I was able to resolve this by updating the main firebase.json configration. The issue was in the hosting reference syntax, which needs to be updated using an array formate instead of the brackets formate.

Initial file – generated by firebase:

{
  "database": {
    "rules": "database.rules.json"
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },
  "storage": {
    "rules": "storage.rules"
  }
}

Updated file, with revising hosting section:

{
  "database": {
    "rules": "database.rules.json"
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  },
  "storage": {
    "rules": "storage.rules"
  },
  "hosting": [
    {
        "public": "public"
    },
    {
      "target": "staging",
      "public": "public"
    },
    {
      "target": "production",
      "public": "public"
    }
  ]
}

I'm only using 2 enviornments in this case. I did not have to make any updates to the .firebaserc file.

zinxor avatar Nov 11 '18 02:11 zinxor

Nice update. I'm going to chat with the folks responsible about this code path and behavior to see if we want to change our initial templates (specifically for hosting) to be arrays rather than objects. That way, when someone wants to do multi-site or use multiple targets, that user doesn't have to try to parse out object vs. array in firebase.json or in the documentation.

For the time being, I'm going to close this. If there's any more feedback, please feel free to leave it here!

bkendall avatar Nov 12 '18 16:11 bkendall

@vnoitkumar you must be on the Blaze plan to create multiple Hosting sites. See the note here: https://firebase.google.com/docs/hosting/multisites

??? it doesnt say anything Infact pricing says Mulitple sites can be deployed to Spark plan too!

YohanWadia avatar Nov 22 '19 15:11 YohanWadia

To deploy also to the default context the following firebase.json worked for me using "site" with the project id:

{
  "hosting": [
    {
      "site": "my-firebase-id",
      "public": "public",
       [...]
    },
    {
      "target": "first-target-name",
      "public": "first-target-folder",
            [...]
    },
    {
      "target": "second-target-name",
      "public": "second-target-folder",
      [...]
    },
  ]
}

The firebase target:apply command was only neccessay for the target-sites, not for the main site.

herberlin avatar Jan 04 '20 17:01 herberlin

This is what worked for me and was the easiest solution was to run the command below and select the same options I did when I first started the project.

firebase init

DisgruntledTech avatar Sep 30 '23 18:09 DisgruntledTech