廣告位招商中 歡迎洽談

使用 Salesforce CLI 與 Metadata API 跨環境部署 Agentforce:架構、元資料依賴與 CI/CD 實踐

松下祥子 · 365Finds Editorial
已收錄 88,650 篇 Wiki 文章

讓你的 AI 用上 365 Wiki

複製下方的提示詞,貼給你常用的 AI,然後直接提問。

下載完整版 Skill

先用提示詞體驗;經常使用時,安裝完整版 365Wiki Skill,以後無需重複複製。沒有常用 AI?Gemini、Microsoft Copilot 等網頁版也可以。

365Wiki Skill

將自主運行的 AI Agent 從 Sandbox 推送到生產環境,絕對不能當成一般的 Apex 程式碼或自訂物件處理。在許多團隊的 DevOps 實務中,若繼續沿用傳統的一攬子元資料推送策略,Agentforce 的部署幾乎必然會被晦澀的依賴報錯、未綁定的 Action 以及未啟動的執行時期狀態中斷。365Finds 的核心判斷非常明確:對 Agentforce 採取單次全量打包部署在設計邏輯上就是不可靠的。要建立高可用的自動化交付管線,工程團隊必須拆解 Agent 的元資料依賴階層,並強制將管線的 Salesforce CLI(sf)基準升級至 Metadata API 64.0 及以上版本。

搞定 Agentforce 的自動化交付,關鍵在於釐清底層的元資料類型、執行嚴格的四階段依賴發布順序,並補齊 Metadata API 本身無法自動配置的環境級前置條件。


元資料架構:Planner、Topic 與 Action 的底層關係

在早期 Einstein Copilot 階段(API 60.0 至 63.0),Agent 的規劃核心採用單體結構的 GenAiPlanner。從 API 64.0 開始,Salesforce 將其全面重構為模組化 Bundle 體系。在版本控制(Git)中管理 Agentforce 時,DevOps 團隊必須識別並追蹤以下核心元資料類型:

元資料類型存放目錄與副檔名在 Agentforce 架構中的職責
GenAiPlannerBundlegenAiPlannerBundles/<name>.genAiPlannerBundle取代舊版 GenAiPlanner;存放 Agent 推理規劃策略、系統指令與 Topic 綁定關係
GenAiPlugingenAiPlugins/<name>.genAiPlugin定義 Agent Topic(主題);組織自然語言指令、意圖識別與具體 Action 的歸屬
GenAiFunctiongenAiFunctions/<name>.genAiFunction定義 Agent Action(操作);宣告輸入輸出參數,對應至底層 Apex、Flow 或 Prompt
GenAiPromptTemplategenAiPromptTemplates/<name>.genAiPromptTemplateEinstein 1 Studio 提示詞範本;負責內容生成與上下文 Grounding
Bot 與 BotVersionbots/<name>/<name>.bot、botVersions/...承載 Agent 規劃引擎的實際對話執行時期容器

在日常開發中,許多開發者習慣直接在 Agentforce Builder 圖形介面中內嵌設定 Topic 和 Action。如果這些資產未發布為全域可重複使用資產,檢索導出的元資料很可能會產生孤立引用或草稿殘留。365Finds 建議在進入 CI/CD 流程前,務必將每一個 Agent Action 明確固化為全域資產庫中的獨立元資料元件。


目標環境的前置準備(Pre-Flight)

Salesforce Metadata API 只能同步元資料定義與業務邏輯,無法自動啟用組織級功能開關或自動指派權限上下文。若目標 Sandbox 未做好底層設定,GenAiPlannerBundle 的部署在驗證階段就會直接失敗。

在執行 CLI 部署命令之前,管線必須確保目標環境具備以下基礎條件:

  1. 平台功能開關:在 Setup 中,「Turn on Einstein」、「Einstein Bots」 以及 「Agentforce for Salesforce」 必須已處於開啟狀態;在 Scratch Org 流程中,必須在 project-scratch-def.json 的 features 中預先宣告。
  2. Data Cloud 狀態對齊:若 Agent 的 Action 或 Prompt 依賴 Data Cloud 的 DMO 實體或向量檢索索引(Search Index),目標環境必須完成 Data Cloud 的關聯與預置;從生產環境複製或重新整理的 Sandbox 需要確認索引關係沒有遺失。
  3. Agent 執行身分權限:Agentforce 在執行時期依託專屬技術帳號(Einstein Agent User)調用業務邏輯。該使用者帳號必須被賦予包含對應底層 Apex 類別執行權、Autolaunched Flow 執行權及相關物件/欄位級權限(OLS/FLS)的 Permission Set。

四階段依賴部署策略

Salesforce CLI 的元資料解析引擎無法自動理順 Agent 資產中錯綜複雜的父子依賴。如果將 Action、Topic 與 Planner 塞進同一次部署,當 GenAiPlugin 試圖綁定一個尚未編譯完成的 GenAiFunction 時,系統便會拋出異常。

365Finds 推薦在 CI/CD 發布管線(如 GitHub Actions、GitLab CI 或 Azure DevOps)中實施嚴格的四階段時序部署:

flowchart LR
    A[第一階段:業務邏輯與底層 Schema] --> B[第二階段:Action 與 Prompt 範本]
    B --> C[第三階段:Topic 意圖分組]
    C --> D[第四階段:Planner Bundle 與 Bot 容器]

第一階段:底層業務邏輯與 Schema

優先發布 Agent 所需呼叫的基礎平台能力:

第二階段:Action 與 Prompt 範本

部署具體的執行契約與提示詞範本:

第三階段:Topic 意圖設定

部署用於組織指令並分派 Action 的主題分類:

第四階段:規劃引擎與執行時期容器

部署將整個 Agent 組織成完整系統的頂層架構:


清單檔案設定(package.xml)

若採用清單檔案模式執行檢索與部署,請務必將 API 版本明確指定為 64.0 或更高,以確保完整支援 GenAiPlannerBundle:

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>ApexClass</name>
    </types>
    <types>
        <members>*</members>
        <name>Flow</name>
    </types>
    <types>
        <members>*</members>
        <name>GenAiPromptTemplate</name>
    </types>
    <types>
        <members>*</members>
        <name>GenAiFunction</name>
    </types>
    <types>
        <members>*</members>
        <name>GenAiPlugin</name>
    </types>
    <types>
        <members>*</members>
        <name>GenAiPlannerBundle</name>
    </types>
    <types>
        <members>*</members>
        <name>Bot</name>
    </types>
    <types>
        <members>*</members>
        <name>BotVersion</name>
    </types>
    <version>64.0</version>
</Package>

在專案根目錄的 sfdx-project.json 中,固定 sourceApiVersion:

{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true
    }
  ],
  "name": "agentforce-devops",
  "namespace": "",
  "sfdxLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "64.0"
}

CI/CD 管線核心 CLI 命令

1. 匯出與檢索

從開發 Sandbox 中按清單匯出 Agentforce 資產,避免擷取到不相關的本機雜質:

sf project retrieve start --manifest manifest/package.xml --target-org sandbox-dev

2. PR 預檢驗證(Dry Run)

在程式碼合併至主幹前,針對整合 Sandbox 或臨時 Scratch Org 執行驗證模式,不寫入任何元資料變更:

sf project deploy validate \
  --manifest manifest/package.xml \
  --target-org uat-org \
  --test-level RunLocalTests \
  --wait 30

3. 分階段定向發布

在執行正式環境發布時,如果不依賴單一全量 manifest,可以透過 --source-dir 嚴格控制發布順序:

# 1. 發布底層業務邏輯
sf project deploy start --source-dir force-app/main/default/classes force-app/main/default/flows --target-org prod-org --wait 20

# 2. 發布 Action 與 Prompt 範本
sf project deploy start --source-dir force-app/main/default/genAiFunctions force-app/main/default/genAiPromptTemplates --target-org prod-org --wait 15

# 3. 發布 Topic
sf project deploy start --source-dir force-app/main/default/genAiPlugins --target-org prod-org --wait 15

# 4. 發布 Planner Bundle 與 Bot 容器
sf project deploy start --source-dir force-app/main/default/genAiPlannerBundles force-app/main/default/bots --target-org prod-org --wait 20

部署後啟動機制與關鍵避坑點

透過 Salesforce CLI 成功部署 Agentforce 元資料並不等同於 Agent 已對外提供服務。Metadata API 匯入的 BotVersion 預設處於停用(Inactive)或草稿狀態,這是平台為防止未經測試的 Prompt 直接面對生產流量所設定的防護機制。

要在管線中實現自動啟動,工程團隊可以在部署後透過 sf apex run 觸發一段輕量腳本,將對應 BotVersion 的狀態欄位設為 Active,或者在發布流程中保留一道在 Agentforce Builder 內進行人工驗證並簽核啟動的防線。

在多人協同開發情境中,多位工程師若同時修改 .genAiPlugin 中的自然語言指令,極易在 Git 中引發衝突。由於 Topic 檔案以結構化 XML 混合儲存提示詞和動作關聯,一般的逐行程式碼合併極易破壞 XML 結構。團隊必須將 Topic 視為最小獨立模組,避免在同一迭代週期內讓多名開發者平行改動同一個 Topic 分支。

在 Metadata API 的驗證規則中,若引用的 Invocable Action 回應逾時超過 30 秒,或者未能通過目標環境物件的 Schema 驗證,GenAiPlannerBundle 的部署將會被直接拒絕。

參考資料