使用多个项目时将Firebase部署到错误的托管

时间:2020-04-04 08:39:01

标签: firebase firebase-hosting firebase-cli

我有两个firebase项目:myapp用于生产,myapp-dev用于开发环境。
我首先使用firebase cli通过“ myapp”初始化我的项目,因此所有文件都是以此生成的,包括托管资源myapp(因此我可以将我的应用部署到myapp.web.app)。

然后,我添加了第二个Firebase项目(“ myapp-dev”)。我跑那些

firebase use --add myapp-dev  # I have selected the right myapp-dev firebase project and set `dev` as short name
firebase target:apply hosting myapp-dev myapp  # note here that I also use name "myapp" as resource

我手动更改了.firebasesrc,因为我希望将dev项目设置为默认项目...

所以我的.firebasesrc看起来像这样

{
  "projects": {
    "default": "myapp-dev",
    "prod": "myapp"
  },
  "targets": {
    "myapp": {
      "hosting": {
        "myapp": [
          "myapp"
        ]
      }
    },
    "myapp-dev": {
      "hosting": {
        "myapp": [
          "myapp"
        ]
      }
    }
  }
}

firebase.json

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

现在,当我运行这些行时,Web应用程序已部署到产品环境,functions已部署到开发环境...

firebase use myapp-dev
firebase deploy

编辑

运行firebase target:apply hosting myapp myapp-dev帮助了!

2 个答案:

答案 0 :(得分:0)

我认为您的配置应该是这样的。

文件.firebasesrc

  "targets": {
    "myapp-dev": {
      "hosting": {
        "myapp-dev": [
          "myapp-dev"
        ],
        "myapp": [
          "myapp"
        ]
      }
    }
  }

文件firebase.json:

  "hosting": [
    {
      "target": "myapp-dev",
      "public": "build",
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
      ],
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ]
    },
    {
      "target": "myapp",
      "public": "dist/myapp", /* folder */
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
      ],
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ]
    }
  ]

,然后使用target documentation

进行部署

答案 1 :(得分:0)

就我而言,我有同一个应用程序部署到两个不同的环境(开发和生产)。在这种情况下,文档不是很清楚,需要花一些时间才能获得正确的配置。

.firebaserc

{
  "projects": {
    "production": "myapp-prod",
    "development": "myapp-dev"
  },
  "targets": {
    "myapp-prod": {
      "hosting": {
        "myapp": [
          "site-name-prod"
        ]
      }
    },
    "myapp-dev": {
      "hosting": {
        "myapp": [
          "site-name-dev"
        ]
      }
    }
  }
}

firebase.json

{
  "hosting": [{
    "target": "myapp",
    "public": "dist",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [ {
      "source": "**",
      "destination": "/index.html"
    } ]
  }]
}

要部署到生产运行

firebase use production
firebase deploy

部署到开发中

firebase use development
firebase deploy

在项目中运行firebase deploy时,它将使用firebase.json的托管部分中指定的目标部署到.firebaserc中目标的托管部分中指定的站点名称