Share via


快速入門:將 Bicep 模組發佈至私人模組登錄

了解如何將 Bicep 模組發佈至私人模組登錄,以及如何從您的 Bicep 檔案呼叫模組。 私人模組登錄可讓您在組織內共用 Bicep 模組。 若要深入了解,請參閱建立 Bicep 模組的私人登錄 (部分機器翻譯)。 若要參與公用模組登錄,請參閱《貢獻指南》。

必要條件

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

若要使用模組登錄,您必須有 Bicep CLI0.4.1008 版或更新版本。 若要搭配 Azure CLI 使用,您也必須有 Azure CLI 2.31.0 版或更新版本;若要搭配 Azure PowerShell 使用,您也必須有 Azure PowerShell 7.0.0 版或更新版本。

Bicep 登錄裝載於 Azure Container Registry (ACR)。 若要建立登錄,請參閱快速入門:使用 Bicep 檔案建立容器登錄 (部分機器翻譯)。

若要設定您的環境以進行 Bicep 開發,請參閱安裝 Bicep 工具 (部分機器翻譯)。 完成那些步驟之後,您將會有 Visual Studio Code \(英文\) 和 Bicep 延伸模組 \(英文\),或 Visual StudioBicep 延伸模組 \(英文\)。

建立 Bicep 模組

模組是從另一個 Bicep 檔案部署的 Bicep 檔案。 任何 Bicep 檔案都可當作模組使用。 您可以在此快速入門中使用下列 Bicep 檔案。 它會建立儲存體帳戶:

@minLength(3)
@maxLength(11)
param storagePrefix string

@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Standard_ZRS'
  'Premium_LRS'
  'Premium_ZRS'
  'Standard_GZRS'
  'Standard_RAGZRS'
])
param storageSKU string = 'Standard_LRS'
param location string

var uniqueStorageName = '${storagePrefix}${uniqueString(resourceGroup().id)}'

resource stg 'Microsoft.Storage/storageAccounts@2021-06-01' = {
  name: uniqueStorageName
  location: location
  sku: {
    name: storageSKU
  }
  kind: 'StorageV2'
  properties: {
    supportsHttpsTrafficOnly: true
  }
}

output storageEndpoint object = stg.properties.primaryEndpoints

將 Bicep 檔案儲存為 storage.bicep

發佈模組

如果您沒有 Azure Container Registry (ACR),請參閱必要條件 (部分機器翻譯) 以建立一個。 ACR 的登入伺服器名稱為必要資訊。 登入伺服器名稱的格式為:<registry-name>.azurecr.io。 若要取得登入伺服器名稱:

az acr show --resource-group <resource-group-name> --name <registry-name> --query loginServer

使用下列語法,將 Bicep 檔案當作模組發佈至私人模組登錄。

az bicep publish --file storage.bicep --target br:exampleregistry.azurecr.io/bicep/modules/storage:v1 --documentationUri https://www.contoso.com/exampleregistry.html

在上述範例中,./storage.bicep 是要發佈的 Bicep 檔案。 視需要更新檔案路徑。 模組路徑具有下列語法:

br:<registry-name>.azurecr.io/<file-path>:<tag>
  • br 是 Bicep 登錄的結構描述名稱。
  • file path 在 Azure Container Registry 中稱為 repositoryfile path 可以包含以 / 字元分隔的區段。 如果登錄中不存在此檔案路徑,即會加以建立。
  • tag 可用於指定模組的版本。

若要確認已發佈的模組,您可以列出 ACR 存放庫:

az acr repository list --name <registry-name> --output table

呼叫模組

若要呼叫模組,請在 Visual Studio Code 中建立新的 Bicep 檔案。 在新的 Bicep 檔案中,輸入下面這一行。

module stgModule 'br:<registry-name>.azurecr.io/bicep/modules/storage:v1'

<registry-name> 取代為您的 ACR 登錄名稱。 系統需要一點時間來將模組還原至本機快取。 還原模組之後,模組路徑下方的紅色波浪線就會消失。 在該行結尾處加入 = 和一個空格,然後選取 [required-properties],如下列螢幕擷取畫面所示。 模組結構會自動填入。

Visual Studio Code Bicep extension required properties

下列範例是完成的 Bicep 檔案。

@minLength(3)
@maxLength(11)
param namePrefix string
param location string = resourceGroup().location

module stgModule 'br:ace1207.azurecr.io/bicep/modules/storage:v1' = {
  name: 'stgStorage'
  params: {
    location: location
    storagePrefix: namePrefix
  }
}

將 Bicep 檔案儲存在本機,然後使用 Azure CLI 或 Azure PowerShell 來部署 Bicep 檔案:

resourceGroupName = "{provide-a-resource-group-name}"
templateFile="{provide-the-path-to-the-bicep-file}"

az group create --name $resourceGroupName --location eastus

az deployment group create --resource-group $resourceGroupName --template-file $templateFile

在 Azure 入口網站中,確認已成功建立儲存體帳戶。

清除資源

如果不再需要 Azure 資源,請使用 Azure CLI 或 Azure PowerShell 模組來刪除快速入門資源群組。

resourceGroupName = "{provide-the-resource-group-name}"

az group delete --name $resourceGroupName

下一步