Search code examples
azure-devopsparametersazure-pipelinesazure-pipelines-yamlazure-bicep

Azure Devops Pipeline - deploy subscription level resources using Bicep, Bicep parameters file and override parameter in stage


Here is my situation:

I have a DevOps pipeline that goes Dev > Staging > Prod.

I have my bicep file, I have my bicepparam file.

Inside my bicepparam file, I have a default global parameter:

using 'main.bicep'
param workloadPrefix = 'work'

For my Pipeline I have the following:

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

variables:
  azureServiceConnection: 'connection'
  subscription: ''
  templateFile: './bicep/main.bicep'
  paramFile: './bicep/main.bicepparam'
  pool: 'Azure Pipelines'

stages:
- stage: Dev
  variables:
    location: 'location'
    name: 'Dev deployment'
    workloadPrefix: 'dev'
  jobs:
    - job: Dev
      pool:
        name: '$(pool)'
      steps:
      - task: AzureResourceManagerTemplateDeployment@3
        displayName: 'Deploy Bicep to Dev'
        inputs:
          deploymentScope: 'Subscription'
          azureResourceManagerConnection: '$(azureServiceConnection)'
          subscriptionId: '$(subscription)'
          location: '$(location)'
          csmFile: '$(templateFile)'
          csmParametersFile: '$(paramFile)'
          overrideParameters: '-workloadPrefix $(workloadPrefix)'

Here is my issue:

If I use AzureResourceManagerTemplateDeployment@3, which supports the overrideParameters flag, it doesn't support having the csmParametersFile as a bicepparam file, it requires a JSON file.

If I instead try and use AzureCLI@2:

  steps:
  - task: AzureCLI@2
    displayName: 'Deploy Bicep to Dev'
    inputs:
      azureSubscription: '$(azureServiceConnection)'
      scriptType: 'bash'
      scriptLocation: 'inlineScript'
      inlineScript: | 
        az deployment sub create \
        --name '$(name)' \
        --location '$(location)' \
        --template-file '$(templateFile)' \
        --parameters '$(paramFile)' \
        --overrideParameters workloadPrefix='$(workloadPrefix)' 

It doesn't support the --overrideParameters flag.

If I try to use Powershell, reading the documentation, it looks like the New-AzSubscriptionDeployment doesn't support it either.

Is there a way to do this? That is, use a bicepparam file, with a default parameter, that can get overriden by a particular pipeline stage?


Solution

  • This is known limitation when using the Bicep deploy.

    When you use the bicepparam file in bicep deploy, it doesn't support using the --overrideParameters flag.

    For a workaround, you can convert the bicepparam file to json file in Pipeline with the following command: bicep build-params main.bicepparam .

    It will create a main.json file based on the main.bicepparam file.

    Then you can use the json file for deployment.

    For example:

    trigger: main
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      azureServiceConnection: 'connection'
      subscription: ''
      templateFile: './bicep/main.bicep'
      paramFile: './bicep/main.bicepparam'
      pool: 'Azure Pipelines'
    
    stages:
    - stage: Dev
      variables:
        location: 'location'
        name: 'Dev deployment'
        workloadPrefix: 'dev'
      jobs:
        - job: Dev
          pool:
            name: '$(pool)'
          steps:
          - script: 'bicep build-params ./bicep/main.bicepparam '
           
            
          - task: AzureResourceManagerTemplateDeployment@3
            displayName: 'Deploy Bicep to Dev'
            inputs:
              deploymentScope: 'Subscription'
              azureResourceManagerConnection: '$(azureServiceConnection)'
              subscriptionId: '$(subscription)'
              location: '$(location)'
              csmFile: '$(templateFile)'
              csmParametersFile: 'filepath/main.json'
              overrideParameters: '-workloadPrefix $(workloadPrefix)'
    

    Note: If you are not able to run bicep command, you can add this command: az bicep install to install bicep cli.