1

I got an Azure yaml pipeline in which there's an input of a name of an output variable:

- task: QueryAzureDevOpsExtensionVersion@3
  displayName: 'Query Extension Version'
  inputs:
    ...
    outputVariable: 'Task.Extension.Version'
    ...

I noticed that I can access this output variable only in the same stage it was defined so I declared a variable to the entire pipeline so I can use it in multiple stages:

variables:
  - name: TaskExtensionVersion
    value: initialValue 

How can I set the value of Task.Extension.Version to variabls.TaskExtensionVersion?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

1 Answers1

4

UPDATE

I've just released a verion of the Azure DevOps Extension Tasks that can set the flag on the output variable (version 3.1.123), I'll make this the default in v4, since it's a breaking change.

steps:
- task: QueryAzureDevOpsExtensionVersion@3
  displayName: 'Query Extension Version: jessehouwing.jessehouwing-vsts-variable-tasks'
  name: Query
  inputs:
    connectedServiceName: Marketplace
    publisherId: jessehouwing
    extensionId: 'jessehouwing-vsts-variable-tasks'
    versionAction: Patch
    setOutputFlag: true

Version 4 removed the need to set anything and always registers an output variable:

steps:
- task: QueryAzureDevOpsExtensionVersion@4
  displayName: 'Query Extension Version: jessehouwing.jessehouwing-vsts-variable-tasks'
  name: Query
  inputs:
    connectedServiceName: Marketplace
    publisherId: jessehouwing
    extensionId: 'jessehouwing-vsts-variable-tasks'
    versionAction: Patch

You can then reference the variable using Query.Extension.Version in the same job or with: dependencies.JobA.outputs['Query.Extension.Version'] across jobs.


Before

You can try my Variables Tasks:

- task: VariableSetTask@2
  name: SetVersion
  displayName: 'Set variable: TaskExtensionVersion to: ''$(Task.Extension.Version)'''
  inputs:
    variableName: TaskExtensionVersion
    Value: '$(Task.Extension.Version)'
    IsOutput: true

Or use a Powershell script:

- powershell: |
   $value = $env:version
   Write-Host "##vso[task.setvariable variable=TaskExtensionVersion;isoutput=true]$value"
  displayName: 'PowerShell Script'
  name: SetVersion
  env:
    version: $(Task.Extension.Version)

I don't think the extension tasks currently register the variable as an output variable across stages.

Then make sure you reference the variable using the correct syntax:

$(dependencies.JobA.outputs['SetVersion.TaskExtensionVersion'])

Be sure to add the name: parameter to your Job and your Task so the output variable can be referenced. The naming of tasks and jobs and declaring dependencies is required to make this work. See the docs on this topic:

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • bummer that I can't use the task because its not installed on our azure organization and I can't install there freely. About the script, if I use it, do I still need to leave the variable declaration at the start of the pipeline, or does the task.setvariable creates it and it expects that it won't be defined before? – CodeMonkey Sep 05 '22 at 13:10
  • Something is not working.. I used the script and right after I tried using it and print its value after the script with a bash script echo "version is $(TaskExtensionVersion)". I then got a message in the pipeline saying TaskExtensionVersion: command not found – CodeMonkey Sep 05 '22 at 14:24
  • You need to reference it with the `$(SetVersion.TaskExternsionVersion)` notation in the same job and with the `$(dependencies.JobName.outputs['(SetVersion.TaskExternsionVersion])` notation when referencing across jobs. See the docs I referenced above. – jessehouwing Sep 05 '22 at 14:34
  • When passing a variable to a script task, use the `env:` section to pass them in explicitly like I've done in the powershell task above. – jessehouwing Sep 05 '22 at 14:37
  • 1
    Thank you so much... finally everything worked – CodeMonkey Sep 07 '22 at 10:15
  • BTW, this pipeline from the example I showed in MS docs completely ignores the vss extension file and the task.json file. When I created a version manually I changed these versions in the files but now with the pipeline it creates a new version to the markeyplace but the files remain with the same version written inside. Luckily it seems like it doesn't matter for the tasks' operation – CodeMonkey Sep 07 '22 at 12:41
  • The files in disk and in your repo are untouched, but if you extract the vsix file (just a zip archive) the files in there should be updated. – jessehouwing Sep 07 '22 at 16:03
  • maybe you'd know what I hope would be the last question I got regarding the pipeline I'm making? This is a more general pipeline question: https://stackoverflow.com/questions/73635958/set-azure-parameters-value-based-on-conditions – CodeMonkey Sep 07 '22 at 17:18
  • 1
    Does the update of your answer with the flag saves the script step? Technically you added a task.setvariable command inside QueryAzureDevOpsExtensionVersion? – CodeMonkey Sep 10 '22 at 13:27
  • Hi again. Got another issue after trying to update the pipeline tasks to version 4... can you please take a look? https://stackoverflow.com/questions/74689104/get-version-from-queryazuredevopsextensionversion – CodeMonkey Dec 05 '22 at 13:38