如何在 Firebase 托管多项目和多站点上为每个项目使用 "predeploy" 挂钩?

How to use a "predeploy" hook per project on a Firebase Hosting multi-projects and multi-sites?

我想在 Firebase 托管多项目和多站点上为每个项目使用不同的 predeploy 挂钩。

可能吗?


目前:

.firebaserc

{
  "projects": {
    "staging": "project-name-staging",
    "production": "project-name-production"
  },
  "targets": {
    "project-name-staging": {
      "hosting": {
        "admin": [
          "project-name-staging-admin"
        ],
        "public": [
          "project-name-staging-public"
        ]
      }
    },
    "project-name-production": {
      "hosting": {
        "admin": [
          "project-name-production-admin"
        ],
        "public": [
          "project-name-production-public"
        ]
      }
    }
  }
}

firebase.json

{
  "hosting": [
    {
      "predeploy": "yarn workspace admin run build",
      "target": "admin",
      "public": "admin/dist/spa",
      "rewrites": [{
        "source": "**",
        "destination": "/index.html"
      }]
    },
    {
      "predeploy": "yarn workspace public run build",
      "target": "public",
      "public": "public/dist/spa",
      "rewrites": [{
        "source": "**",
        "destination": "/index.html"
      }]
    }
  ]
}

package.json

{
  "private": true,
  "name": "admin (or public)",
  "version": "0.0.1",
  "description": "Project Name Admin (or Public)",
  "productName": "Project Name Admin (or Public)",
  "scripts": {
    "dev": "quasar dev",
    "build": "quasar build"
  },
  "dependencies": {
    "@quasar/extras": "1.10.6",
    "axios": "0.21.1",
    "core-js": "3.13.1",
    "firebase": "8.6.5",
    "quasar": "1.15.19",
    "vue-i18n": "8.24.4"
  },
  "devDependencies": {
    "@quasar/app": "2.2.8",
    "babel-eslint": "10.1.0",
    "dotenv": "10.0.0",
    "eslint": "7.28.0",
    "eslint-config-standard": "16.0.3",
    "eslint-plugin-import": "2.23.4",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-promise": "5.1.0",
    "eslint-plugin-vue": "7.10.0",
    "eslint-webpack-plugin": "2.5.4"
  }
}

理想情况下:

.firebaserc

{
  "projects": {
    "staging": "project-name-staging",
    "production": "project-name-production"
  },
  "targets": {
    "project-name-staging": {
      "hosting": {
        "admin": [
          "project-name-staging-admin"
        ],
        "public": [
          "project-name-staging-public"
        ]
      }
    },
    "project-name-production": {
      "hosting": {
        "admin": [
          "project-name-production-admin"
        ],
        "public": [
          "project-name-production-public"
        ]
      }
    }
  }
}

firebase.json

{
  "hosting": [
    {
      "predeploy": {
        "staging": "yarn workspace admin run staging:build",
        "production": "yarn workspace admin run production:build"
      },
      "target": "admin",
      "public": "admin/dist/spa",
      "rewrites": [{
        "source": "**",
        "destination": "/index.html"
      }]
    },
    {
      "predeploy": {
        "staging": "yarn workspace public run staging:build",
        "production": "yarn workspace public run production:build"
      },
      "target": "public",
      "public": "public/dist/spa",
      "rewrites": [{
        "source": "**",
        "destination": "/index.html"
      }]
    }
  ]
}

package.json

{
  "private": true,
  "name": "admin (or public)",
  "version": "0.0.1",
  "description": "Project Name Admin (or Public)",
  "productName": "Project Name Admin (or Public)",
  "scripts": {
    "dev": "cross-env NODE_ENV=staging quasar dev",
    "staging:build": "cross-env NODE_ENV=staging quasar build",
    "production:build": "cross-env NODE_ENV=production quasar build"
  },
  "dependencies": {
    "@quasar/extras": "1.10.6",
    "axios": "0.21.1",
    "core-js": "3.13.1",
    "firebase": "8.6.5",
    "quasar": "1.15.19",
    "vue-i18n": "8.24.4"
  },
  "devDependencies": {
    "@quasar/app": "2.2.8",
    "babel-eslint": "10.1.0",
    "cross-env": "7.0.3",
    "dotenv": "10.0.0",
    "eslint": "7.28.0",
    "eslint-config-standard": "16.0.3",
    "eslint-plugin-import": "2.23.4",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-promise": "5.1.0",
    "eslint-plugin-vue": "7.10.0",
    "eslint-webpack-plugin": "2.5.4"
  }
}

最后,运行 执行 Firebase 托管部署及其 predeploy 挂钩:

您可以在 package.json 中设置一个运行适当命令的部署脚本 由于无法为每个框架和自定义集成构建 firebase-cli 工具,因此您想在自己的构建脚本末尾注入 deploy 命令

"cross-env NODE_ENV=staging quasar build && firebase deploy --only hosting:admin"

此外,您还内置了“使用”功能,但这更适合管理具有同一站点的多个项目https://firebase.google.com/docs/cli#project_aliases

我还建议阅读此处演示的每个项目场景的多个应用程序:https://firebase.google.com/docs/projects/learn-more#best-practices