如何 link Azure ARM 模板中的模板?
How can I link a template in an Azure ARM template?
我有多个 ARM 模板想要 link。但是当我使用 "[uri(deployment().properties.templateLink.uri, 'transform.json')]"
时,我收到一条错误消息,告诉我 deployment() 给出了一个不包含 templateLink 的对象 运行 在本地或通过 Azure DevOps 管道。
然后我尝试将路径发送到我在 Azure DevOps 中构建项目时创建的工件,"[concat(parameters('templateDirectory'), '/transform.json')]"
,然后在调用模板时将其作为参数提供。
但是后来我得到了这个错误
At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
Details:
BadRequest: {
"error": {
"code": "InvalidContentLink",
"message": "The provided content link 'file:///D:/a/r1/a/_Infrastructure/ARM/shared/transform.json' is invalid or not supported. Content link must be an absolute URI not referencing local host or UNC path."
}
} undefined
Task failed while creating or updating the template deployment.
所以我的问题是,当我通过 Azure DevOps 管道部署时,我应该如何处理 linking 模板?
我是否必须在构建步骤中将它复制到存储中,以便我可以在部署步骤中使用 http 或 https 访问它,如果是这样,最好的方法是什么?好像有点复杂。
解决方案更新
所以我寻求的解决方案是将所有模板文件上传到我创建的临时存储,然后将路径添加到引用所有模板的主模板。
任务概述以更好地了解它是如何完成的:
将模板复制到 blob:
部署 ARM 模板:
下面是它在引用其他模板的主模板中的使用方式的片段:
"resources": [
{
"apiVersion": "2015-01-01",
"name": "dashboard-24h",
"type": "Microsoft.Resources/deployments",
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[concat(parameters('templateBasePath'), '/dashboard/24h/azuredeploy-dashboard-deploy.json')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"templateBasePath": {
"value": "[parameters('templateBasePath')]"
},
"appName": {
"value": "[parameters('appName')]"
}
}
}
},
...
]
因此,如果您想使用 deployment().properties.templateLink.uri
,您的模板必须从 url 部署,而不是从本地磁盘部署。
嵌套模板始终必须从 url 部署。因此,如果您想使用上述方法,所有内容都必须上传到可公开访问的某个地方(或者必须通过 URL 完成身份验证,例如 SAS 令牌)。
我通常做的 - 运行 部署前的一个简单的 powershell 脚本,将所有模板上传到一个公共位置,之后我只使用部署功能。
如果您要生成 ARM,请扩展以上内容
要么
使用 AzureResourceManagerTemplateDeployment@3
部署生成的模板,您可以像这样覆盖变量
- task: AzureResourceManagerTemplateDeployment@3
inputs:
deploymentScope: 'Resource Group'
... other values
deploymentMode: 'Validation'
overrideParameters: '-LinkedTemplatesBaseUrl "$(BASE_URL)" -LinkedTemplatesUrlQueryString $(SAS_TOKEN)'
LinkedTemplatesBaseUrl
和 LinkedTemplatesUrlQueryString
必须在 *-parameters.json
文件中定义
仅在从安全存储中获取时使用 LinkedTemplatesUrlQueryString
(这是首选方式)
您可以使用 AzureFileCopy@4
复制您的模板
steps:
- task: AzureFileCopy@4
name: AzureFileCopyTask
inputs:
SourcePath: '$(System.DefaultWorkingDirectory)/build_dir/*.json'
azureSubscription: $(AZURE_SUBSCRIPTION)
Destination: 'AzureBlob'
storage: $(STORAGE_ACCOUNT)
ContainerName: $(STORAGE_CONTAINER)
CleanTargetBeforeCopy: true
BlobPrefix: $(STORAGE_PREFIX)
并使用上述的输出变量here
我有多个 ARM 模板想要 link。但是当我使用 "[uri(deployment().properties.templateLink.uri, 'transform.json')]"
时,我收到一条错误消息,告诉我 deployment() 给出了一个不包含 templateLink 的对象 运行 在本地或通过 Azure DevOps 管道。
然后我尝试将路径发送到我在 Azure DevOps 中构建项目时创建的工件,"[concat(parameters('templateDirectory'), '/transform.json')]"
,然后在调用模板时将其作为参数提供。
但是后来我得到了这个错误
At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-debug for usage details.
Details:
BadRequest: {
"error": {
"code": "InvalidContentLink",
"message": "The provided content link 'file:///D:/a/r1/a/_Infrastructure/ARM/shared/transform.json' is invalid or not supported. Content link must be an absolute URI not referencing local host or UNC path."
}
} undefined
Task failed while creating or updating the template deployment.
所以我的问题是,当我通过 Azure DevOps 管道部署时,我应该如何处理 linking 模板?
我是否必须在构建步骤中将它复制到存储中,以便我可以在部署步骤中使用 http 或 https 访问它,如果是这样,最好的方法是什么?好像有点复杂。
解决方案更新
所以我寻求的解决方案是将所有模板文件上传到我创建的临时存储,然后将路径添加到引用所有模板的主模板。
任务概述以更好地了解它是如何完成的:
将模板复制到 blob:
部署 ARM 模板:
下面是它在引用其他模板的主模板中的使用方式的片段:
"resources": [
{
"apiVersion": "2015-01-01",
"name": "dashboard-24h",
"type": "Microsoft.Resources/deployments",
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[concat(parameters('templateBasePath'), '/dashboard/24h/azuredeploy-dashboard-deploy.json')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"templateBasePath": {
"value": "[parameters('templateBasePath')]"
},
"appName": {
"value": "[parameters('appName')]"
}
}
}
},
...
]
因此,如果您想使用 deployment().properties.templateLink.uri
,您的模板必须从 url 部署,而不是从本地磁盘部署。
嵌套模板始终必须从 url 部署。因此,如果您想使用上述方法,所有内容都必须上传到可公开访问的某个地方(或者必须通过 URL 完成身份验证,例如 SAS 令牌)。
我通常做的 - 运行 部署前的一个简单的 powershell 脚本,将所有模板上传到一个公共位置,之后我只使用部署功能。
如果您要生成 ARM,请扩展以上内容
要么
使用 AzureResourceManagerTemplateDeployment@3
部署生成的模板,您可以像这样覆盖变量
- task: AzureResourceManagerTemplateDeployment@3
inputs:
deploymentScope: 'Resource Group'
... other values
deploymentMode: 'Validation'
overrideParameters: '-LinkedTemplatesBaseUrl "$(BASE_URL)" -LinkedTemplatesUrlQueryString $(SAS_TOKEN)'
LinkedTemplatesBaseUrl
和 LinkedTemplatesUrlQueryString
必须在 *-parameters.json
文件中定义
仅在从安全存储中获取时使用 LinkedTemplatesUrlQueryString
(这是首选方式)
您可以使用 AzureFileCopy@4
复制您的模板
steps:
- task: AzureFileCopy@4
name: AzureFileCopyTask
inputs:
SourcePath: '$(System.DefaultWorkingDirectory)/build_dir/*.json'
azureSubscription: $(AZURE_SUBSCRIPTION)
Destination: 'AzureBlob'
storage: $(STORAGE_ACCOUNT)
ContainerName: $(STORAGE_CONTAINER)
CleanTargetBeforeCopy: true
BlobPrefix: $(STORAGE_PREFIX)
并使用上述的输出变量here