如何测试链接的 ARM 模板? [英] How to test linked ARM templates?
问题描述
我的 ARM 模板变得太大,所以我想使用链接模板.我知道模板需要放在 ARM 可以访问的地方.但是我应该能够在将它们上传到目标位置之前以某种方式测试它们.否则,我可能会用无效的模板覆盖以前的工作模板.那我该如何恢复?
My ARM template is getting too big so I would like to use linked templates. I understand that templates need to be somewhere where they are accessible to ARM. But I should be able to test them somehow before I upload them to target location. Otherwise there is a risk that I override previously working templates with invalid ones. How do I revert then?
你是怎么做到的?
推荐答案
所以我想出了这个脚本.每个开发人员都有预先创建的资源组,他将在其中部署资源和预先创建的 Blob 容器来存储当前正在开发的模板.在部署模板之前,我使用 azcopy
将本地文件夹与 Blob 容器同步.不幸的是,有时 Test-AzResourceGroupDeployment
无法在失败的情况下为您提供足够的详细信息,因此我无法根据它的返回值来决定是否执行部署.现在这似乎工作正常.但它已经是第 3 或第 4 个版本,将来可能会发生变化.一种想法是合并 ARM-TTK用于模板测试.
So I came up with this script. Each developer has precreated resource group where he will deploy resources and precreated Blob container to store templates currently in development. Before deploying templates I use azcopy
to synchronize my local folder with Blob container. Unfortunately sometimes Test-AzResourceGroupDeployment
doesn't give you enough details in case of failure so I can't make decision based on it's return value to execute deployment or not. For now this seems to work fine. But it's already 3rd or 4th version and it will probably change in future. One idea is to incorporate ARM-TTK for template testing.
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$resourceGroup = $currentUser.Substring($currentUser.IndexOf('\') + 1) + "-testing"
$containerName = $currentUser.Substring($currentUser.IndexOf('\') + 1) + "-testing"
$storageAccountName = "team_shared_account_here";
$containerUrl = "https://${storageAccountName}.blob.core.windows.net/${containerName}"
Write-Host "Current user: <${currentUser}>" -ForegroundColor Green
Write-Host "Deployment will use templates from <${containerName}> container" -ForegroundColor Green
Write-Host "Resources will be deployed to <${resourceGroup}> resource group" -ForegroundColor Green
Write-Host
Write-Host "Syncing templates..." -ForegroundColor Green
.\azcopy.exe sync '.' $containerUrl --include-pattern "*.json" --delete-destination true
$toDeploy = "app1", "app2"
foreach ($template in $toDeploy) {
$templateUri = "${containerUrl}/${template}.json"
$templateParameterUri = "${containerUrl}/${template}.parameters.DEV.json"
Write-Host "`nDeploying: ${templateUri}" -ForegroundColor Green
Write-Host "Paramaters: ${templateParameterUri}" -ForegroundColor Green
Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroup `
-TemplateUri $templateUri `
-TemplateParameterUri $templateParameterUri `
-Mode Incremental `
-Verbose
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroup `
-TemplateUri $templateUri `
-TemplateParameterUri $templateParameterUri `
-Mode Incremental `
-DeploymentDebugLogLevel All `
-Verbose
}
这篇关于如何测试链接的 ARM 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!