Azure pipeline unable to deploy via a bicep file and set values for its parameters

104 Views Asked by At

In my azure-pipelines.yml file I am unable to deploy via a bicep file and set values for its parameters.

Here is the start of my bicep file

targetScope = 'subscription'
param location string = 'uksouth'
param environmentCode string

Here is the task in my azure-pipelines.yml file

  - task: AzureResourceManagerTemplateDeployment@3
    inputs:
      deploymentScope: 'Resource Group'
      azureResourceManagerConnection: 'MyName(SomeGuid)'
      subscriptionId: 'SomeGuid'
      action: 'Create Or Update Resource Group'
      resourceGroupName: '$(resourceGroupName)'
      location: '$(location)'
      templateLocation: 'Linked artifact'
      csmFile: '$(Build.SourcesDirectory)/Infrastructure/Main.bicep'
      deploymentMode: 'Incremental'
      addSpnToEnvironment: true
      useWithoutJSON: true

I have tried overriding parameters in 3 different ways, but get an error for each

  1. No dashes on names
      overrideParameters: 'location $(location) environmentCode $(environmentCode)'
##[error]One of the deployment parameters has an empty key. Please see https://aka.ms/arm-create-parameter-file for details.
  1. Single dash on names
      overrideParameters: '-location $(location) -environmentCode $(environmentCode)'
There were errors in your deployment. Error code: InvalidTemplate.
##[error]Deployment template validation failed: 'The template resource 'resourceGroup' at line '1' and column '1182' is not valid: The language expression property 'location' doesn't exist, available properties are 'name, properties'.. Please see https://aka.ms/arm-functions for usage details.'.
  1. Two dashes on names
      overrideParameters: '--location $(location) --environmentCode $(environmentCode)'
There were errors in your deployment. Error code: InvalidTemplate.
##[error]Deployment template validation failed: 'The template parameters '-location, -environmentCode' in the parameters file are not valid; they are not present in the original template and can therefore not be provided at deployment time. The only supported parameters for this template are 'location, environmentCode'. Please see https://aka.ms/arm-pass-parameter-values for usage details.'.
2

There are 2 best solutions below

1
wenbo On BEST ANSWER

I don't know what kind of actual resources you want to deploy, deploymentScope inputs of AzureResourceManagerTemplateDeployment@3 be profoundly affected by the targetScope in bicep file.

Figuring out the scope of your deployment is critical in this problem

I have coded a sample to show two types of deployment, one in resource group level, the other is subscription level

rg.bicep

// set the target scope for this file
targetScope = 'subscription'

param resourceGroupName string

param location string

resource newRG 'Microsoft.Resources/resourceGroups@2023-07-01' = {
  name: resourceGroupName
  location: location
}

output newRGResourceId string = newRG.id

vnet.bicep

targetScope = 'resourceGroup'

param vnetName string
param location string

resource newVnet 'Microsoft.Network/virtualNetworks@2023-09-01' = {
  name: vnetName
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets: [
      {
        name: 'default'
        properties: {
          addressPrefix: '10.0.2.0/24'
        }
      }
    ]
  }
}

output newVnetResourceId string = newVnet.id

azure-pipelines.yml

variables:
- name: subscriptionId
  value: 'xxxx'
- name: resourceGroupName
  value: 'wb-test-111'
- name: location
  value: 'eastus'
- name: vnetName
  value: 'wb-vnet111'

steps:
- task: AzureResourceManagerTemplateDeployment@3
  displayName: create a new resource group
  inputs:
    deploymentScope: 'Subscription'
    azureResourceManagerConnection: DevOpsSub1Connection-Test
    templateLocation: 'Linked artifact'
    location: '$(location)'
    csmFile: '$(Build.SourcesDirectory)/rg.bicep'
    deploymentMode: 'Incremental'
    overrideParameters: >
      -resourceGroupName $(resourceGroupName)
      -location $(location)


- task: AzureResourceManagerTemplateDeployment@3
  displayName: create a vnet in existing resource group
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: DevOpsSub1Connection-Test
    subscriptionId: $(subscriptionId)
    action: 'Create Or Update Resource Group'
    resourceGroupName: '$(resourceGroupName)'
    location: '$(location)'
    templateLocation: 'Linked artifact'
    csmFile: '$(Build.SourcesDirectory)/vnet.bicep'
    deploymentMode: 'Incremental'
    overrideParameters: >
      -vnetName $(vnetName)
      -location $(location)

my result:

you should check rg deployment in subscription and check vnet deployment in resource group in azure portal

enter image description here

0
Bright Ran-MSFT On

It is recommended using a Bicep parameters file to define these parameters and pass the parameters file to the AzureResourceManagerTemplateDeployment task using the 'csmParametersFile' option. And use the 'overrideParameters' option to override the values of the specified parameters.

  - task: AzureResourceManagerTemplateDeployment@3
    inputs:
      deploymentScope: 'Resource Group'
      azureResourceManagerConnection: 'MyName(SomeGuid)'
      subscriptionId: 'SomeGuid'
      action: 'Create Or Update Resource Group'
      resourceGroupName: '$(resourceGroupName)'
      location: '$(location)'
      templateLocation: 'Linked artifact'
      csmFile: 'path/to/Main.bicep'
      csmParametersFile: 'path/to/parameters/file'
      overrideParameters: '-location $(location) -environmentCode $(environmentCode)'
      deploymentMode: 'Incremental'
      addSpnToEnvironment: true
      useWithoutJSON: true

In addition, you also can try to use the related Azure CLI commands or Azure PowerShell cmdlet to deploy Azure resources with Bicep files. In the pipelines of Azure DevOps, you can use the AzureCLI task to run Azure CLI commands or use the AzurePowerShell task to run the Azure PowerShell cmdlet.

To pass or override the parameters in Bicep files:

  • If using Azure CLI, you can use the option '--parameters' with the 'KEY=VALUE' syntax on the commands. For example.

    az deployment group create \
    --resource-group <resource-group-name> \
    --template-file <path-to-bicep> \
    --parameters location='$(location)' environmentCode='$(environmentCode)'
    
  • If using AzurePowerShell, you can use the '-KEY VALUE' syntax on the cmdlet. For example.

    New-AzResourceGroupDeployment `
    -ResourceGroupName <resource-group-name> `
    -TemplateFile <path-to-bicep> `
    -location "$(location)" `
    -environmentCode "$(environmentCode)"
    

Related documents: