是否可以在 Azure DevOps 中的 Azure Powershell 任务中设置变量并将它们传递给另一个任务?

Is it possible to set variables in the Azure Powershell task in Azure DevOps and pass them to another task?

通过阅读 Azure DevOps 文档 here,似乎可以在脚本中定义变量,但仅限于 Bash 和 Powershell 任务。如果我想使用 Azure Powershell 任务,它似乎不起作用。这是我想要做的事情的 yaml。

- stage: promote_image
 displayName: Promote VM Image to Prod
 jobs:
 - job: VMPush
   steps:
   - task: AzurePowerShell@5
     inputs:
       ScriptType: 'InlineScript'
       Inline: |
         $srcimageversion = get-azgalleryimageversion -ResourceGroupName $srcRG `
         -GalleryName $srcgallery `
         -GalleryImageDefinitionName $srcimagename | select -last 1
         Write-Host "##vso[task.setvariable variable=VersionName;isOutput=true]$srcimageversion.Name"
         Write-Host "##vso[task.setvariable variable=SourceId;isOutput=true]$srcimageversion.Id.ToString()"
         Write-Host "Version name is $($env:VersionName)"
         Write-Host "VM source is $($env:SourceId)"
       azurePowerShellVersion: 'LatestVersion'
       azureSubscription: $(nonProd_Subscription)
   - task: AzurePowerShell@5
     inputs:
       ScriptType: 'InlineScript'
       Inline: |
         Write-Host "Version name is $($env:VersionName)"
         Write-Host "VM source is $($env:SourceId)"
       azurePowerShellVersion: 'LatestVersion'
       azureSubscription: $(prod_Subscription)

VersionName 和 SourceId 变量在这两个任务中都没有输出。甚至可以使用 Azure Powershell 任务设置这样的变量吗?我正在使用 Azure Powershell 任务,因为我需要从一个 Azure 订阅中的资源获取信息,然后使用它在另一个 Azure 订阅中部署某些内容。

是的,这是可能的。您在获取变量的方式上存在错误。首先让我们命名你的第一个任务,然后只使用 $(setvarStep.VersionName) 而不是 $($env:VersionName)" 你应该得到你需要的东西

- task: AzurePowerShell@5
  name: setvarStep
  inputs:
    ScriptType: 'InlineScript'
    Inline: |
      $value1 = "Value1"
      $value2 = "Value2"
      Write-Host "##vso[task.setvariable variable=VersionName;isOutput=true]$value1"
      Write-Host "##vso[task.setvariable variable=SourceId;isOutput=true]$value2"value"
    azurePowerShellVersion: 'LatestVersion'
    azureSubscription: 'rg-the-code-manual'
- task: AzurePowerShell@5
  inputs:
    ScriptType: 'InlineScript'
    Inline: |
      Write-Host "Version name is $(setvarStep.VersionName)"
      Write-Host "VM source is $(setvarStep.SourceId)"
    azurePowerShellVersion: 'LatestVersion'
    azureSubscription: 'rg-the-code-manual'

这里同意 Krzysztof Madej。要使用任务的输出变量,我们需要使用depend name.

在同一作业中使用输出:

steps:
- task: MyTask@1  # this step generates the output variable
  name: ProduceVar  # because we're going to depend on it, we need to name the step
- script: echo $(ProduceVar.MyVar) # this step uses the output variable

此外,在不同的作业中使用输出:

jobs:
- job: A
  steps:
  - task: MyTask@1  # this step generates the output variable
    name: ProduceVar  # because we're going to depend on it, we need to name the step
- job: B
  dependsOn: A
  variables:
    # map the output variable from A into this job
    varFromA: $[ dependencies.A.outputs['ProduceVar.MyVar'] ]
  steps:
  - script: echo $(varFromA) # this step uses the mapped-in variable

详细攻略请参考本官方documnet.