如何将管道变量传递给参数文件以进行蓝图分配

How can I pass pipeline variable to parameters file for blueprint assignment

我正在尝试创建用于部署 Azure 蓝图的 Azure DevOps 管道。参数文件 (JSON) 中有一些字段我想配置。如何将这些值作为管道变量传递并在参数文件中使用它们?

我尝试定义一个管道变量并在参数文件中引用它,例如“$(var-name)”,但它没有用。有办法解决吗?

下面是我的管道定义,我使用 AzureBlueprint 扩展来创建和分配蓝图:

steps:
- task: CreateBlueprint@1
  inputs:
    azureSubscription: $(serviceConnection)
    BlueprintName: $(blueprintName)
    BlueprintPath: '$(blueprintPath)'
    AlternateLocation: false
    PublishBlueprint: true

- task: AssignBlueprint@1
  inputs:
    azureSubscription: $(serviceConnection)
    AssignmentName: '$(blueprintName)-assignment'
    BlueprintName: $(blueprintName)
    ParametersFile: '$(blueprintPath)/assign.json'
    SubscriptionID: $(subscriptionId)
    Wait: true
    Timeout: 500

和我的参数文件:

"parameters":{
         "organization" : {
            "value": "xxxx"
         },
         "active-directory-domain-services_ad-domain-admin-password" : {
            "reference": {
               "keyVault": {
                     "id": "/subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.KeyVault/vaults/xxxx"
               },
               "secretName": "xxxx"
            }
         },
         "jumpbox_jumpbox-local-admin-password" : {
            "reference": {
               "keyVault": {
                     "id": "/subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.KeyVault/vaults/xxxx"
               },
               "secretName": "xxxx"
            }
         },         
         "keyvault_ad-domain-admin-user-password" : {
            "value" : "xxxx"
         },
         "keyvault_deployment-user-object-id" : {
            "value" : "xxxx"
         },
         "keyvault_jumpbox-local-admin-user-password" : {
            "value" : "xxxx"
         }
      }

由于您使用的任务(CreateBlueprintAssignBlueprint)不支持覆盖参数,您有两个选择:

样本:

$paramFile = Get-Content ./azuredeploy.parameters.json | ConvertFrom-Json
$paramFile.parameters.organization.value = "your-org-name" 
$paramFile | ConvertTo-Json | Set-Content ./azuredeploy.parameters.json

请注意,您正在使用的任务在过去 17 个月内未收到更新(此处为 GitHub repository)。

AssignBlueprint@1 doesn't support natively this. However you can modify assign.json using Json substitution

归根结底,Azure Pipeline 变量的名称类似于要放置的 json 文件中叶子的路径。

这是一个例子:

variables:
  Data.DebugMode: disabled
  Data.DefaultConnection.ConnectionString: 'Data Source=(prodDB)\MSDB;AttachDbFilename=prod.mdf;'
  Data.DBAccess.Users.0: Admin-3
  Data.FeatureFlags.Preview.1.NewWelcomeMessage: AllAccounts

# Update appsettings.json via FileTransform task.
- task: FileTransform@1
  displayName: 'File transformation: appsettings.json'
  inputs:
    folderPath: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    targetFiles: '**/appsettings.json'
    fileType: json