設定您的 Bicep 環境

Bicep 支援名為 bicepconfig.json 的選擇性設定檔。 在此檔案中,您可以新增自訂 Bicep 開發體驗的值。 此檔案會與預設設定檔合併。 如需詳細資訊,請參閱了解合併流程。 若要自訂設定,請在 Bicep 檔案的相同目錄或父目錄中建立設定檔。 如果有多個包含 bicepconfig.json 檔案的父目錄,Bicep 會使用最接近之父目錄裡的設定。 如需詳細資訊,請參閱了解檔案解析流程

若要設定 Bicep 延伸模組設定,請參閱 VS Code 和 Bicep 延伸模組

在 Visual Studio Code 中建立設定檔

您可以使用任何文字編輯器來建立設定檔。

若要在 Visual Studio Code 中建立 bicepconfig.json 檔案,請開啟 [命令選擇區] ([CTRL/CMD]+[SHIFT]+[P]),然後選取 [Bicep:建立 Bicep 設定檔]。 如需詳細資訊,請參閱建立 Bicep 設定檔

Screenshot of how to create Bicep configuration file in VS Code.

Visual Studio Code 的 Bicep 延伸模組支援 bicepconfig.json 檔案的 IntelliSense。 使用 IntelliSense 探索可用的屬性和值。

Screenshot of the intellisense support in configuring bicepconfig.json.

了解合併流程

bicepconfig.json 檔案會經歷與預設設定檔由下而上的遞迴式合併流程。 在合併流程期間,Bicep 會檢查這兩個設定中的每個路徑。 如果預設設定中沒有某個路徑,則會在最終結果中新增該路徑及其相關聯的值。 反之,如果某個路徑存在於預設設定中但有不同的值,則來自 bicepconfig.json 的值會在合併的結果裡取得優先。

請考慮預設設定定義如下的情節:

{
  "cloud": {
    ...
    "credentialPrecedence": [
      "AzureCLI",
      "AzurePowerShell"
    ]
  },
  "moduleAliases": {
    "ts": {},
    "br": {
      "public": {
        "registry": "mcr.microsoft.com",
        "modulePath": "bicep"
      }
    }
  },
  ...
}

bicepconfig.json 定義如下:

{
  "cloud": {
    "credentialPrecedence": [
      "AzurePowerShell",
      "AzureCLI"
    ]
  },
  "moduleAliases": {
    "br": {
      "ContosoRegistry": {
        "registry": "contosoregistry.azurecr.io"
      },
      "CoreModules": {
        "registry": "contosoregistry.azurecr.io",
        "modulePath": "bicep/modules/core"
      }
    }
  }
}

產生的合併設定會是:

{
  "cloud": {
    ...
    "credentialPrecedence": [
      "AzurePowerShell",
      "AzureCLI"
    ]
  },
  "moduleAliases": {
    "ts": {},
    "br": {
      "public": {
        "registry": "mcr.microsoft.com",
        "modulePath": "bicep"
      },
      "ContosoRegistry": {
        "registry": "contosoregistry.azurecr.io"
      },
      "CoreModules": {
        "registry": "contosoregistry.azurecr.io",
        "modulePath": "bicep/modules/core"
      }
    }
  },
  ...
}

在上述範例中,會取代 cloud.credentialPrecedence 的值,而 cloud.moduleAliases.ContosoRegistrycloud.moduleAliases.CoreModules 的值會附加在合併的設定中。

了解檔案解析流程

bicepconfig.json 檔案可以放在 Bicep 檔案的相同目錄或父目錄中。 如果有多個包含 bicepconfig.json 檔案的父目錄,Bicep 會使用最接近之父目錄裡的設定檔。 例如,在某個資料夾結構中,每個資料夾都有一個 bicepconfig.json 檔案:

A diagram showing resolving `bicepconfig.json` found in multiple parent folders.

如果您在 child 資料夾中編譯 main.bicep,則會使用 child 資料夾中的 bicepconfig.json 檔案。 系統會忽略 parent 資料夾和 root 資料夾中的設定檔。 如果 child 資料夾不包含設定檔,Bicep 會搜尋 parent 資料夾中的設定,然後搜尋 root 資料夾。 如果在任何資料夾都找不到設定檔,Bicep 會預設為使用預設值

在 Bicep 檔案叫用多個模組的情境中,每個模組都會經歷使用最近 bicepconfig.json 進行編譯的過程。 然後,主要 Bicep 檔案會以其對應的 bicepconfig.json 進行編譯。 在下列情節中,modA.bicep 會使用位於 A 資料夾中的 bicepconfig.json 來編譯、modB.bicep 使用 B 資料夾中的 bicepconfig.json 編譯,最後 main.bicep 使用 root 資料夾中的 bicepconfig.json 編譯。

A diagram showing resolving `bicepconfig.json` found in multiple parent folders with the module scenario.

如果沒有 AB 資料夾中的 bicepconfig.json 檔案,則全部三個 Bicep 檔案都會使用 root 資料夾中找到的 bicepconfig.json 來編譯。 如果 bicepconfig.json 不存在於任何資料夾,編譯流程預設會使用預設值

設定 Bicep 模組

使用模組時,您可以新增模組路徑的別名。 這些別名會簡化 Bicep 檔案,因為您不需要重複執行複雜的路徑。 您也可以設定雲端設定檔和認證優先順序,以從 Bicep CLI 和 Visual Studio Code 向 Azure 進行驗證。 認證可用來將模組發佈至登錄,以及在使用插入資源函式時,將外部模組還原至本機快取。 如需詳細資訊,請參閱將模組設定新增至 Bicep 設定

設定 Linter 規則

Bicep Linter 會檢查 Bicep 檔案是否有語法錯誤和最佳做法違規。 您可以修改 bicepconfig.json 來覆寫 Bicep 檔案驗證的預設設定。 如需詳細資訊,請參閱將 Linter 設定新增至 Bicep 設定

啟用實驗性功能

您可以將下列區段新增至 bicepconfig.json 檔,以啟用實驗性功能。

以下是啟用 'compileTimeImports' 和 'userDefinedFunctions' 功能的範例。

{
  "experimentalFeaturesEnabled": {
    "compileTimeImports": true,
    "userDefinedFunctions": true
  }
}

如需目前實驗性功能集的資訊,請參閱實驗性功能

下一步