部署目標

「部署目標」是 Firebase 專案中的 Firebase 資源簡稱 ID (由您自行定義的),例如具有不重複靜態資產的託管網站,或共用相同安全性規則的一組即時資料庫執行個體。

如果您擁有多個託管網站、多個 Cloud Storage 值區或多個即時資料庫執行個體,就適合部署目標。透過部署目標,Firebase CLI 可以將設定部署至專案中的特定 Firebase 資源或一組資源,例如:

  • 每個託管網站的託管設定
  • 每個託管網站專案目錄中的靜態資產
  • 多個即時資料庫執行個體或多個 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 (您自行定義的簡稱 ID) 套用至 Firebase 資源或一組 Firebase 資源。Firebase 支援以下項目的部署目標:

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

設定託管的部署目標

如要建立部署目標,並將 TARGET_NAME 套用至託管網站,請執行下列 CLI 指令:

firebase target:apply TYPE TARGET_NAME RESOURCE_IDENTIFIER

參數位置:

  • TYPE:相關的 Firebase 資源類型

    • 如果是 Firebase 託管網站,請使用 hosting
  • TARGET_NAME:要部署的託管網站的專屬名稱

  • RESOURCE_IDENTIFIER — 您 Firebase 專案中的SITE_ID「託管」網站的清單

舉例來說,如果您已在 Firebase 專案中建立兩個網站 (myapp-blogmyapp-app),則可執行下列指令,將不重複的 TARGET_NAME (blogapp) 分別套用至每個網站:

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

為 Cloud Storage 或即時資料庫設定部署目標

如要建立部署目標,並將 TARGET_NAME 套用至一組 Cloud Storage 或即時資料庫資源,請執行下列 CLI 指令:

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

參數位置:

  • TYPE:相關的 Firebase 資源類型

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

  • RESOURCE_IDENTIFIERFirebase 專案中列出的資源 (例如儲存空間值區名稱或資料庫執行個體 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 資源設定部署目標後,請在 firebase.json 設定檔中參照各個套用的 TARGET_NAME

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

延續上述範例,假設您的 Firebase 專案有兩個託管網站,以及三個共用相同安全性規則的 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
從指定目標中移除所有資源或託管網站

target:removetarget:clear 指令會自動更新專案目錄 .firebaserc 檔案中的部署目標設定。

先在本機測試再部署

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

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

進一步瞭解如何設定及使用 Firebase 本機模擬器套件

部署特定 Firebase 資源

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

指令 說明
firebase deploy 建立專案目錄中所有可部署資源的版本。
firebase deploy \
--only hosting:TARGET_NAME
僅將指定代管網站的「託管」內容和設定部署至網站的直播頻道
firebase hosting:channel:deploy CHANNEL_ID \
--only TARGET_NAME
僅將指定代管網站的「託管」內容和設定部署至網站的預覽管道
firebase deploy \
--only storage:TARGET_NAME
僅部署指定 Cloud Storage 目標的規則檔案
firebase deploy \
--only database:TARGET_NAME
僅部署指定即時資料庫目標的規則檔案