Power Platform Solution 正式環境導出:Environment Variable 的正確處理方式

| PowerPlatform | 1 Reads

在 Power Platform 做 DEV → PROD 部署時,如果 Solution 內包含會隨環境改變的設定,例如:

  • 外部系統 URL

  • Power BI Report URL

  • Tenant ID

  • API Endpoint

  • Storage 路徑

不應該把 DEV 環境的實際值寫死在 App 或一起帶進正式環境。

正確做法是使用 Environment Variable


1. Environment Variable 命名

建議:

項目 設定方式
Display Name 日文等容易理解的名稱
Schema Name Publisher Prefix + 英文 / 羅馬字
Data Type 依用途選擇,URL 通常為 Text
Default Value 不設定
Current Value DEV 環境的實際值

例如:

Display Name:
帳票 URL

Schema Name:
abc_ReportUrl_Sample

Default Value:
未設定

Current Value:
DEV 環境的 URL

其中 abc_ 為 Solution Publisher Prefix。


2. 開發環境使用 Current Value

DEV 開發期間,可以正常設定:

Current Value = DEV 專用值

Canvas App、Power Automate 等元件從 Environment Variable 取得設定,而不是直接寫死:

https://dev.example.com/...

這樣 App 本體就不依賴特定環境。


3. Export 前移除 Current Value

準備向 PROD 發布時,對環境變量的 Current Value 執行:

Remove from this solution

注意:

這不是刪除 DEV 環境中的 Current Value。

只是讓這個值:

不要被包含在即將導出的 Solution 中。

結果:

DEV 環境
Current Value = DEV 值
        ↓
仍然保留


導出的 Solution
Current Value
        ↓
不包含

4. 正式交付導出 Managed Solution

推薦的基本原則:

DEV
↓
Unmanaged Solution 開發
↓
完成測試
↓
移除 Environment Variable Current Value
↓
Export
↓
Managed Solution
↓
Import PROD

也就是:

開發用 Unmanaged,正式交付用 Managed。


5. PROD 第一次 Import 時設定正式值

因為:

Default Value = 未設定
Current Value = 未包含在 Solution

所以第一次 Import 到 PROD 時,需要為該環境填入真正的正式值。

例如:

DEV
Current Value
= https://dev.example.com/...

PROD
Current Value
= https://prod.example.com/...

相同的 Solution,在不同 Environment 使用不同的 Current Value。


6. 後續升級不再覆蓋 PROD 的 Current Value

假設 PROD 已經設定:

Current Value = PROD 專用值

後續發布:

Managed Solution V2
Managed Solution V3
Managed Solution V4

只要每次導出的 Solution 都不包含 Current Value,更新 Solution 時:

PROD 已經設定好的 Current Value 會繼續保留。

因此不需要每次升級都重新設定環境變量。


最終規則

整套流程其實只需要記住下面幾點:

項目 規則
開發環境 Unmanaged Solution
正式交付 Managed Solution
Display Name 可以使用日文
Schema Name Prefix + 英文 / 羅馬字
Default Value 不設定
DEV Current Value 正常設定
Export 前 Remove from this solution
PROD Current Value 第一次部署時設定
後續版本升級 不帶 Current Value,保留 PROD 原值

核心思想只有一句:

Solution 負責程式與設定的「定義」,各 Environment 負責自己的「實際值」。

這樣才能做到 DEV、TEST、PROD 共用同一套 Solution,而不需要因為 URL、Tenant ID 等環境差異重新修改 App。

This article was last edited at