部署目標

部署目標是由您自行定義的簡稱 ID Firebase 專案中的 Firebase 資源,例如有專屬資訊的 Hosting 網站 使用相同安全性的靜態資產或 Realtime Database 執行個體群組 不過,編寫這類演算法並不容易 因為我們無法寫出所有可能的規則

如果發生以下情況,部署目標會相當實用 多個 Hosting 網站, 多個 Cloud Storage 值區,或 多個 Realtime Database 執行個體。 有了部署目標,Firebase CLI 就能將設定部署至特定 專案中的 Firebase 資源或資源群組,例如:

  • 每個 Hosting 網站的代管設定
  • 專案目錄中個別 Hosting 的靜態資產 網站
  • 多個或多個 Realtime Database 執行個體共用的安全性規則 Cloud Storage 個值區

如要設定部署目標:

  1. 套用TARGET_NAME至指定的 Firebase 或 Firebase 資源群組
  2. 在您的 firebase.json 檔案中, 參照相關聯的 TARGET_NAME 指定每項資源或資源群組的設定時

當您執行 Firebase CLI 指令時 (例如 firebase deploy),Firebase CLI 會將每個 TARGET_NAME 與其 相關聯的 Firebase 資源CLI 會與 Firebase 通訊 並逐一完成每項資源的設定

為 Firebase 資源設定部署目標

使用 Firebase CLI,套用 TARGET_NAME (也就是 ; Firebase 支援下列產品的部署目標:

,瞭解如何調查及移除這項存取權。

部署目標的設定會儲存在您的 .firebaserc 檔案中 因此,您只需為每個位置設定部署目標一次 專案。

設定「Hosting」的部署目標

如要建立部署目標並將 TARGET_NAME 套用至 Hosting 網站,請執行以下指令: 加入以下 CLI 指令:

firebase target:apply TYPE TARGET_NAME RESOURCE_IDENTIFIER

其中的參數如下:

  • TYPE:相關的 Firebase 資源類型

    • 如果是 Firebase Hosting 網站,請使用 hosting
  • TARGET_NAME:你顯示的 Hosting 網站專屬名稱 部署至

  • RESOURCE_IDENTIFIER - Hosting 網站的 SITE_ID 列在 Firebase 專案中

舉例來說,假設您建立了兩個網站 (myapp-blogmyapp-app),您可以套用 每個網站的不重複 TARGET_NAME (分別為 blogapp) 執行 下列指令:

firebase target:apply hosting blog myapp-blog
firebase target:apply hosting app myapp-app

Cloud StorageRealtime Database 設定部署目標

如要建立部署目標,並將 TARGET_NAME 套用至一組 Cloud StorageRealtime Database 資源,請執行下列 CLI 指令:

firebase target:apply TYPE TARGET_NAME RESOURCE-1_IDENTIFIER RESOURCE-2_IDENTIFIER ...

其中的參數如下:

  • TYPE:相關的 Firebase 資源類型

    • 如果是 Cloud Storage 值區,請使用 storage
    • 如果是 Realtime Database 執行個體,請使用 database
  • TARGET_NAME:資源或資源群組的專屬名稱 共用安全性規則

  • RESOURCE_IDENTIFIER:資源的識別碼 列在 Firebase 專案中 (例如 Storage 值區名稱或資料庫) 執行個體 ID)

舉例來說,您可以將 mainTARGET_NAME 套用至三個群組的一組 Cloud Storage 區域性值區 (所有值區共用相同的安全性規則) ,請執行下列指令:

firebase target:apply storage main myproject.appspot.com myproject-eu myproject-ja

請注意,myproject.appspot.com 是預設值區的 ID。 而 myproject-eumyproject-ja 則是在 Firebase 專案

設定 firebase.json 檔案以使用部署目標

為 Firebase 資源設定部署目標後,請逐一參照 已套用TARGET_NAME firebase.json 設定檔:

  1. 為每個 Firebase 資源「TYPE」建立設定物件陣列 (hostingstoragedatabase)。
  2. 在陣列中指定 target (使用 TARGET_NAME) 並定義 您可以為相關聯的 Firebase 資源或資源群組進行設定。

延續上方範例,你的 Firebase 專案有兩個 Hosting 個網站和三個 Cloud Storage 值區 (共用相同的) 安全性規則),您的 firebase.json 檔案會如下所示:

{
  "hosting": [ {
      "target": "blog",  // "blog" is the applied TARGET_NAME for the Hosting site "myapp-blog"
      "public": "blog/dist",  // contents of this folder are deployed to the site "myapp-blog"

      // ...
    },
    {
      "target": "app",  // "app" is the applied TARGET_NAME for the Hosting site "myapp-app"
      "public": "app/dist",  // contents of this folder are deployed to the site "myapp-app"

      // ...

      "rewrites": [...]  // You can define specific Hosting configurations for each site
    }
  ]
}

{
  "storage": [ {
      "target": "main",  // "main" is the applied TARGET_NAME for the group of Cloud Storage buckets
      "rules": "storage.main.rules"  // the file that contains the shared security rules
    }
  ]
}

如果資源有多項設定,您可以建立多項設定 並在 firebase.json 檔案中指定每個目標。所有相關 執行 firebase deploy 時,系統會一併部署資源。

管理部署目標

部署目標的設定會儲存在您的 .firebaserc 檔案中 專案目錄如要管理專案的部署目標,方法是執行 執行下列指令。

指令 說明
firebase target 列出目前專案目錄的部署目標
firebase target:remove \
TYPE RESOURCE_IDENTIFIER
從指派的目標中移除資源
firebase target:clear \
TYPE TARGET_NAME
從指定中移除所有資源或 Hosting 網站 目標

target:removetarget:clear 指令會自動更新部署 目標設定。.firebaserc

先在本機測試再部署

從專案目錄的根目錄執行下列任一指令。

指令 說明
firebase emulators:start 模擬專案中「所有」已設定的資源 目錄
firebase emulators:start \
--only hosting:TARGET_NAME
只模擬Hosting內容的和設定 指定 Hosting 網站
firebase emulators:start \
--only storage:TARGET_NAME
只模擬指定 Cloud Storage 的規則檔案 目標
firebase emulators:start \
--only database:TARGET_NAME
僅模擬指定 Realtime Database 目標的規則檔案

進一步瞭解如何設定及使用 Firebase Local Emulator Suite

部署特定 Firebase 資源

從專案目錄的根目錄執行下列任一指令。

指令 說明
firebase deploy 在專案中建立「所有」可部署資源的版本 目錄
firebase deploy \
--only hosting:TARGET_NAME
僅部署叢集的 Hosting 內容和設定 指定了 Hosting 個網站做為該網站的上線頻道
firebase hosting:channel:deploy CHANNEL_ID \
--only TARGET_NAME
僅部署叢集的 Hosting 內容和設定 將 Hosting 個網站指定為 預覽頻道 網站
firebase deploy \
--only storage:TARGET_NAME
僅部署指定 Cloud Storage 的規則檔案 目標
firebase deploy \
--only database:TARGET_NAME
僅部署指定 Realtime Database 目標的規則檔案