我正在尝试构建一些非常简单的东西来管理我们在Azure平台中的日常任务。我们习惯于与 ARM 合作,并且能够处理或多或少复杂的事情。现在我们正在尝试转向 YAML 场景,这将更灵活地接管其他类型的任务。
我已经测试了我的 ARM 模板直接运行它,它工作正常。但是我在 YAML 步骤中迷失了方向,我认为参数应该覆盖未正确引用的参数 json 文件。此外,我发现查找有关连接字符串或获取日期表达式的信息非常复杂。老实说,我认为这会很容易,或者至少比 ARM 得到更好的支持。
我希望你能帮助我更好地理解它。
trigger: none # Disable automatic triggering
pool:
vmImage: 'windows-latest'
parameters:
- name: subscriptionId
displayName: 'Subscription ID'
type: string
default: '********************************
- name: resourceGroupName
displayName: 'Resource Group Name'
type: string
default: 'ARM_LAB'
- name: diskName
displayName: 'Disk Name'
type: string
default: 'azvmlab01v-os'
- name: resourceGroupLocation
displayName: 'Resource Group Location'
type: string
default: 'west europe'
variables:
azureServiceConnectionName: 'ConnectionName'
snapshotName: $[format('{0}-SNAP', pipeline.parameters.diskName)]
diskId: "/subscriptions/${{ parameters.subscriptionId }}/resourceGroups/${{ parameters.resourceGroupName }}/providers/Microsoft.Compute/disks/${{ parameters.diskName }}"
#$[format('/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/disks/{2}', pipeline.parameters.subscriptionId, pipeline.parameters.resourceGroupName, pipeline.parameters.diskName)]
stages:
- stage: Deploy
jobs:
- job: ARM_Deployment
steps:
- task: AzureResourceManagerTemplateDeployment@3
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: $(azureServiceConnectionName)
subscriptionId: ${{ parameters.subscriptionId }}
action: 'Create Or Update Resource Group'
resourceGroupName: ${{ parameters.resourceGroupName }}
location: ${{ parameters.resourceGroupLocation }}
templateLocation: 'Linked artifact'
csmFile: 'ARM/Snapshot from disk/template.json'
csmParametersFile: 'ARM/Snapshot from disk/template.parameters.json'
overrideParameters: '-snapshotName $(snapshotName) -diskId $(diskId)'
这是 YAML,这是管道的输出:
Starting: AzureResourceManagerTemplateDeployment
==============================================================================
Task : ARM template deployment
Description : Deploy an Azure Resource Manager (ARM) template to all the deployment scopes
Version : 3.247.5
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment
==============================================================================
ARM Service Connection deployment scope - ManagementGroup
Checking if the following resource group exists: ARM_LAB.
Resource group exists: true.
Creating deployment parameters.
The detected encoding for file 'D:\a\1\s\ARM\Snapshot from disk\template.json' is 'utf-8'
The detected encoding for file 'D:\a\1\s\ARM\Snapshot from disk\template.parameters.json' is 'utf-8'
There was an error while overriding 'SNAP' parameter because of 'TypeError: Cannot read properties of undefined (reading 'type')', make sure it follows JavaScript Object Notation (JSON)
(node:1380) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Starting template validation.
Deployment name is template-20241108-164136-2b80
There were errors in your deployment. Error code: InvalidTemplate.
##[error]Deployment template validation failed: 'The following parameters were supplied, but do not correspond to any parameters defined in the template: 'SNAP'. The parameters defined in the template are: 'snapshotName, diskId'. Please see https://aka.ms/arm-pass-parameter-values for usage details.'.
##[warning]Validation errors were found in the Azure Resource Manager template. This can potentially cause template deployment to fail. Task failed while creating or updating the template deployment.. Please follow https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-syntax
Starting Deployment.
Deployment name is template-20241108-164136-2b80
There were errors in your deployment. Error code: InvalidTemplate.
##[error]Deployment template validation failed: 'The following parameters were supplied, but do not correspond to any parameters defined in the template: 'SNAP'. The parameters defined in the template are: 'snapshotName, diskId'. Please see https://aka.ms/arm-pass-parameter-values for usage details.'.
##[error]Check out the troubleshooting guide to see if your issue is addressed: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-resource-group-deployment?view=azure-devops#troubleshooting
##[error]Task failed while creating or updating the template deployment.
Finishing: AzureResourceManagerTemplateDeployment
我认为问题不是由模板引起的,而是由yaml引起的。我刚刚测试了一下,其实这个错误的原因是你的变量 concat 不正确。
带有
format
的 parameters
应遵循以下模式:
snapshotName: $[format('{0}-SNAP', '${{parameters.diskName}}' )]
注意:此处必须需要将
${{parameters.diskName}}
括起来的单引号。
下面是一个说明变量的示例。
pool:
vmImage: 'windows-latest'
parameters:
- name: subscriptionId
displayName: 'Subscription ID'
type: string
default: '********************************'
- name: resourceGroupName
displayName: 'Resource Group Name'
type: string
default: 'ARM_LAB'
- name: diskName
displayName: 'Disk Name'
type: string
default: 'azvmlab01v-os'
- name: resourceGroupLocation
displayName: 'Resource Group Location'
type: string
default: 'west europe'
variables:
azureServiceConnectionName: 'ConnectionName'
snapshotName: $[format('{0}-SNAP', '${{parameters.diskName}}' )]
diskId: $[format('/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/disks/{2}', '${{parameters.subscriptionId}}', '${{parameters.resourceGroupName}}', '${{parameters.diskName}}' )]
jobs:
- job: wbjob
steps:
- script: echo 'print snapshotName $(snapshotName)'
- script: echo 'print diskId $(diskId)'
我的测试结果: