Arm模板(Bicep):合并应用程序设置时的循环依赖关系(列表函数)



我正试图通过二头肌文件更新应用程序服务的AppSettings。

在我的二头肌模板中做这件事时:

var currentAppSettings = list('Microsoft.Web/sites/appServiceName/config/appsettings', '2018-11-01')
var newAppSettings = {
test: 'testValue'
}
var mergedAppSettings = union(currentAppSettings, newAppSettings)
resource appServiceConfig 'Microsoft.Web/sites/config@2018-11-01' = {
name: 'appServiceName/appSettings'
properties: mergedAppSettings
}

我在部署二头肌文件时遇到了一个循环依赖错误:

"Deployment template validation failed: 'Circular dependency detected on resource: '/subscriptions/293d7347-b26f-4413-9986-d61717aaff26/resourceGroups/WSAPlayground/providers/Microsoft.Web/sites/playground-fitxp-backend-euw-wa/config/appSettings'. Please see https://aka.ms/arm-template/#resources for usage details.'."

有没有一种方法可以做到这一点而不会出现依赖性错误?

尝试使用模块。Bicep模块本质上是嵌套部署。在顶级文件(即main(中,提取当前值并将它们作为object类型的参数传递给子模块(appsettings(,然后执行merge和update。线索是在不同于读取当前值的模块中部署更新。

如果部署同时创建应用程序服务/功能应用程序并设置应用程序设置,那么使用模块似乎没有帮助。这会导致循环依赖性错误。

相反,我最终使用的解决方案是将当前应用程序设置的提取移到二头肌模板之外,并将其作为参数传入。类似于这个bash脚本:

existingAppSettings="{}"
functionAppAlreadyDeployed=$(az appservice plan list --query "[?name=='ic-portfolio-service-preprod-app-plan']" | jq length)
if [functionAppAlreadyDeployed -eq 1]
then
existingAppSettings=$(az functionapp config appsettings list --name ic-${{parameters.serviceName}}-${{parameters.environment}} --resource-group ${{parameters.serviceName}}-${{parameters.environment}} | jq -r 'map( { (.name): .value } ) | add')
fi
az deployment group create 
--name "deploymentNameGoesHere" 
--resource-group "resourceGroupNameGoesHere" 
--template-file "$(Pipeline.Workspace)/templatepathgoeshere/main.bicep" 
--parameters "buildNumber=$(Build.BuildNumber)" 
"existingAppSettings=$existingAppSettings" 
--mode Complete

注意-我使用az appservice plan list是因为appservice不支持exists

然后,您可以将其作为对象传递到二头肌模板中:

@secure()
param existingAppSettings object

并使用如下:

var appSettings = { 
New app settings go here
}
resource functionAppAppsettings 'Microsoft.Web/sites/config@2018-11-01' = {
name: '${functionAppName}/appsettings'
properties: union(existingAppSettings, appSettings)
}