Search code examples
azureadoazure-cliazure-bicep

How to pass parameters from an Azure devops pipeline into a Bicep template


First off, Bicep is new to me. I'm currently creating a template for creating a storage account and have a set name I want to follow which has the environment name within it. I want to add a parameter / variable into the bicep file so that it will take the value from the pipeline when it is run as the environment name is set at runtime and then placed into the Bicep file so that it then creates a storage account name for that said environment.

Can anyone advise how to do this?

Thanks,

Darren

So my current bicep template has this:

// Creates a storage account, private endpoints and DNS zones
@description('Azure region of the deployment')
param location string = 'UK South'

@description('Name of the storage account')
param storageName string = 'random${env}01'

@allowed([
  'Standard_LRS'
  'Standard_ZRS'
  'Standard_GRS'
  'Standard_GZRS'
  'Standard_RAGRS'
  'Standard_RAGZRS'
  'Premium_LRS'
  'Premium_ZRS'
])

@description('Storage SKU')
param storageSkuName string = 'Standard_LRS'

resource storage 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageName
  location: location
  tags: {
    Environment: 'Dev'
  }
  sku: {
    name: storageSkuName
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
    allowBlobPublicAccess: true
    allowCrossTenantReplication: true
    allowSharedKeyAccess: true
    encryption: {
      keySource: 'Microsoft.Storage'
      requireInfrastructureEncryption: false
      services: {
        blob: {
          enabled: true
          keyType: 'Account'
        }
        file: {
          enabled: true
          keyType: 'Account'
        }
        queue: {
          enabled: true
          keyType: 'Service'
        }
        table: {
          enabled: true
          keyType: 'Service'
        }
      }
    }
    isHnsEnabled: false
    isNfsV3Enabled: false
    keyPolicy: {
      keyExpirationPeriodInDays: 7
    }
    largeFileSharesState: 'Disabled'
    minimumTlsVersion: 'TLS1_2'
    networkAcls: {
      bypass: 'AzureServices'
      defaultAction: 'Allow'
    }
    supportsHttpsTrafficOnly: true
  }
}

output storageId string = storage.id

Solution

  • First, in your pipeline you need to add a parameter with your environment:

    parameters:
      - name: environment
        displayName: "Your environment"
        type: string
    

    Then you need to add this parameter to your bicep:

    targetScope = 'resourceGroup'
    
    // input parameters
    param environment string
    
    @description('Name of the storage account')
    param storageName string = 'random${environment}01'
    

    Then, you need an Azure CLI task to start your bicep deployment and pass the parameter to the bicep:

    - task: AzureCLI@2
      displayName: Bicep deployment
      inputs:
        azureSubscription: $(azureServiceConnection)
        scriptType: bash
        scriptLocation: inlineScript
        inlineScript: |
          set -e
    
          echo '##[Section]Deploy sa'
    
          az deployment group create \
            --resource-group $(rg_to_deploy_to) \
            --name "sa-deployment" \
            --template-file ./storageaccount.bicep \
            --parameters environment="${{ parameters.environment }}"
    

    Let me know if this clears this up for you.