查询 Azure 服务连接以获取资源名称

Querying an azure service connection for resource names

我在 ADO 中有一个服务连接来连接到 azure 以更新资源,但我需要一些资源的名称(例如,存储帐户名称,例如:“st-base-storage-dev”

目前,我正在从门户中获取此名称并在阶段的顶部创建一个变量,但想知道是否有一种方法可以从 azure 中获取值,而无需在未来阶段进行此类手动干预?

您可以添加一个Azure CLI task in your pipeline to run the following command to get the specific resources properties and set the value of the name to a variable. Thus you can use the variable in subsequent tasks. See az resource list

az resource list --resource-group ResourceGroup --resource-type Microsoft.Storage/storageAccounts

以下 YAML 供您参考:

pool:
  vmImage: Windows-latest

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: 'YourServiceConnection'
    scriptType: 'ps'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $resources = (az resource list --resource-group ResourceGroup  --resource-type Microsoft.Storage/storageAccounts) | ConvertFrom-Json
      $staccount = $resources.name
      Write-Host "staccount:" $staccount
      Write-Host "##vso[task.setvariable variable=storageaccount]$staccount"
  displayName: Get Resource name and set variable

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "storageaccount" : $(storageaccount)'
  displayName: Print the variable