azureazure-devopsyamlazure-resource-manager

ARM deployment job in YAML


I'm trying to build something quite simple to manage our usual tasks in Azure platform. we are used to work with ARM and we manage to handle more or less complex things. Now we are trying to move to a YAML scenario which will be more flexible to take over other kind of tasks.

I've tested my ARM template running It directly and it works fine. However i'm getting lost in the YAML steps, i think parameters should override the parameters json file are not being properly referenced. Also I find extremely complex to find info about concatenating strings or getting date expressions. Honestly I thought this would be easy or at least better supported than ARM.

I hope you can help me to understand it better.

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)'

This is the YAML and heres the output from the pipeline:

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

Solution

  • I do not think the issue caused by the template, but the yaml. I have just test, actually the reason of this error is that your variable concat is incorrect.


    the format with parameters should following below pattern:

    snapshotName: $[format('{0}-SNAP', '${{parameters.diskName}}' )]

    Note: the single quotes arround ${{parameters.diskName}} must be required here.

    Below is a example illustrate the variables.

    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)'
    

    My test result:

    enter image description here

    enter image description here