練習 - 將模組發佈至您的登錄
您已建立私人登錄供您的玩具公司使用。 在本練習中,您將會:
- 為網站資源建立模組。
- 為 CDN 中的資源建立另一個模組。
- 將模組發佈至您的登錄。
- 列出登錄中的模組。
此練習使用適用於 Visual Studio Code 的 Bicep 延伸模組。 請務必在 Visual Studio Code 中安裝此延伸模組。
為網站建立模組
您先前已建立模組來部署網站。 在這裡,您會儲存模組檔案以便能夠加以發佈。
打開 Visual Studio Code。
建立名為 website.bicep 的新檔案。
將下列程式碼貼到 website.bicep 檔案:
@description('The Azure region into which the resources should be deployed.') param location string @description('The name of the App Service app.') param appServiceAppName string @description('The name of the App Service plan.') param appServicePlanName string @description('The name of the App Service plan SKU.') param appServicePlanSkuName string resource appServicePlan 'Microsoft.Web/serverfarms@2023-12-01' = { name: appServicePlanName location: location sku: { name: appServicePlanSkuName } } resource appServiceApp 'Microsoft.Web/sites@2023-12-01' = { name: appServiceAppName location: location properties: { serverFarmId: appServicePlan.id httpsOnly: true } } @description('The default host name of the App Service app.') output appServiceAppHostName string = appServiceApp.properties.defaultHostName
儲存檔案。
您可以選取 [檔案] > [另存新檔],或在 Windows 中選取 Ctrl+S (macOS 為 ⌘+S)。 請務必記住您儲存檔案的位置。 例如,您可能需要建立範本資料夾來儲存檔案。
為 CDN 建立模組
和先前的步驟類似,您會儲存預先建立的模組檔案,以便能夠很快地發佈。
建立名為 cdn.bicep 的新檔案。
將下列程式碼貼到 cdn.bicep 檔案:
@description('The host name (address) of the origin server.') param originHostName string @description('The name of the CDN profile.') param profileName string = 'cdn-${uniqueString(resourceGroup().id)}' @description('The name of the CDN endpoint') param endpointName string = 'endpoint-${uniqueString(resourceGroup().id)}' @description('Indicates whether the CDN endpoint requires HTTPS connections.') param httpsOnly bool var originName = 'my-origin' resource cdnProfile 'Microsoft.Cdn/profiles@2024-02-01' = { name: profileName location: 'global' sku: { name: 'Standard_Microsoft' } } resource endpoint 'Microsoft.Cdn/profiles/endpoints@2024-02-01' = { parent: cdnProfile name: endpointName location: 'global' properties: { originHostHeader: originHostName isHttpAllowed: !httpsOnly isHttpsAllowed: true queryStringCachingBehavior: 'IgnoreQueryString' contentTypesToCompress: [ 'text/plain' 'text/html' 'text/css' 'application/x-javascript' 'text/javascript' ] isCompressionEnabled: true origins: [ { name: originName properties: { hostName: originHostName } } ] } } @description('The host name of the CDN endpoint.') output endpointHostName string = endpoint.properties.hostName
儲存檔案。
將模組發佈至您的登錄
在 Visual Studio Code 終端機中,執行下列命令。 以您的私人登錄名稱取代
YOUR_CONTAINER_REGISTRY_NAME
。az bicep publish \ --file website.bicep \ --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/website:v1' az bicep publish \ --file cdn.bicep \ --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/cdn:v1'
請注意,您不需要登入。 Bicep 會使用 Azure CLI 中的登入資訊,向登錄驗證您的身分。
執行下列命令以列出登錄中的成品:
az acr repository list \ --name YOUR_CONTAINER_REGISTRY_NAME
輸出會顯示模組的名稱:
[ "cdn", "website" ]
在 Visual Studio Code 終端機中,執行下列命令。 以您的私人登錄名稱取代
YOUR_CONTAINER_REGISTRY_NAME
。bicep publish website.bicep ` --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/website:v1' bicep publish cdn.bicep ` --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/cdn:v1'
請注意,您不需要登入。 Bicep 會使用 Azure PowerShell 中的登入資訊,向登錄驗證您的身分。
執行下列命令以列出登錄中的成品:
Get-AzContainerRegistryRepository -RegistryName YOUR_CONTAINER_REGISTRY_NAME
輸出會顯示模組的名稱:
cdn website
您也可以使用 Azure 入口網站來列出登錄中的模組。 從資源群組的 [概觀] 索引標籤中選取 YOUR_CONTAINER_REGISTRY_NAME
,然後選取 [存放庫]。 您稍後會在本課程模組中連線到 Azure 入口網站。