使用多个项目时,Firebase 部署到错误的主机

Posted

技术标签:

【中文标题】使用多个项目时,Firebase 部署到错误的主机【英文标题】:Firebase deployed to wrong hosting when multiple projects used 【发布时间】:2020-07-16 10:26:07 【问题描述】:

我有两个 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,因为我希望开发项目是默认的...

所以我的.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"
    ]
  


现在,当我运行这些行时,webapp 被部署到 prod 环境,functions 被部署到 dev 环境...

firebase use myapp-dev
firebase deploy

编辑

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

【问题讨论】:

【参考方案1】:

就我而言,我将同一个应用程序部署到 2 个不同的环境(开发和生产)。文档对这种情况不是很清楚,花了一些时间来获得正确的配置。

.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 中目标的托管部分中指定的站点名称

【讨论】:

【参考方案2】:

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

文件.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"
        
      ]
    
  ]

然后使用targetdocumentation进行部署

【讨论】:

以上是关于使用多个项目时,Firebase 部署到错误的主机的主要内容,如果未能解决你的问题,请参考以下文章

将构建文件上传到Firebase主机后,Ionic会抛出错误

Firebase CLI 部署错误:“部署到 Node.js 10 以下的运行时现在在 Firebase CLI 中被禁用。”

部署 firebase 项目时错误显示“找不到模块 firebase-functions”

Typescript 中的 React + Firebase 云功能无法部署

部署函数时出现 Firebase 部署错误

Flutter 现有项目到 WebApp + Firebase (Firestore + Hosting)