如何将参数从 Azure DevOps 管道传递到 Bicep 模板

问题描述 投票:0回答:1

首先,二头肌对我来说是新的。我当前正在创建一个用于创建存储帐户的模板,并有一个我想要遵循的集合名称,其中包含环境名称。我想在 bicep 文件中添加一个参数/变量,以便在运行时从管道中获取值,因为环境名称是在运行时设置的,然后放入 Bicep 文件中,以便它创建一个存储帐户名称对于那个环境。

任何人都可以建议如何做到这一点吗?

谢谢,

达伦

所以我当前的二头肌模板有这个:

// 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
azure ado azure-cli azure-bicep
1个回答
1
投票

首先,在您的管道中,您需要添加一个与您的环境相关的参数:

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

然后你需要将此参数添加到你的二头肌:

targetScope = 'resourceGroup'

// input parameters
param environment string

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

然后,您需要一个 Azure CLI 任务来启动二头肌部署并将参数传递给二头肌:

- 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 }}"

如果这能为您解决问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.