0
votes

From reading the Azure DevOps documentation here, it looks like it's possible to define variables in scripts, but only for Bash and Powershell tasks. If I want to use the Azure Powershell task, it does not seem to work. Here is my yaml for what I am trying to do.

- 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)

The VersionName and SourceId variables don't get output in either task. Is it even possible to set variables like this with the Azure Powershell task? I am using the Azure Powershell task since I need to obtain information from a resource in one Azure subscription and then use it to deploy something in another Azure subscription.

2
Not get your latest information, I tested Krzysztof Madej's answer, on my side, it works well. If the following answer is helpful, you could Accept it as an Answer , so it could help other community members who get the same issues and we could archive this thread, thanks.Hugh Lin
@JohnRamos can you consider upvoting my answer if it was useful to you?Krzysztof Madej

2 Answers

1
votes

Yes, this is possible. You have a mistake in a way how you get variables. First let's name your fist task and then use just $(setvarStep.VersionName) instead of $($env:VersionName)" you should get what you need

- 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'
0
votes

Here agree with Krzysztof Madej. To use the output variable of the task, we need to use the depend name.

Use outputs in the same job:

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

In addition, use outputs in a different job:

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

For detailed guide, please refer to this official documnet.