0
votes

I'm developper of a private custom extension usable in Azure Devops.

I have a problematic around the output of task.json in our extension and wonder if you can help to clarify / resolve the problematic.

1-What works.

I have created a release with 2 step : First step, execute my extension Second Step, retrieve output variable (sorry for the naming of the task, I'm in testing mode) enter image description here

Our task.json execute a script named vm.ps1

enter image description here

In this vm.ps1 script, I have a piece of code to set the value of the variable $VirtualMachineId:

enter image description here

This value is then retrieve with the second step of the release, azure powershell inline script :

enter image description here

The result is diplayed in the log : enter image description here

My value is retrieved, the goal is accomplish.

2-What doesn’t work : Implement in the task.json

Now, if I want to fully implement this variable in the product, I need to add this one in the output section of our task.json :

enter image description here

So, the UI of our interface will be display in the output section like the example below ( do not be disturb to not see VirtualMachineId in the below image, it is an illustration image): enter image description here

I run another time my release, as I can see in the log of the extension execution, the variable is set with appropriate value.

enter image description here

Then the Azure inline script execute and the value is empty : enter image description here

To resume: If I declare the variable in the output section of the task json, I can't retrieve the value or the value is empty. Does anybody here have an idea of what I do wrong ?

Thanks to have take time to read me.

2

2 Answers

0
votes

To retrieve the output variable's value of a task. You need to set a reference name for the following task to refer to the output variable.

For example: Download secure file task has an output variable (secureFilePath) . In order the following task to refer to its output variable. The reference name needs to be defined(eg. myFile).

enter image description here

Then in the following tasks. You can refer to the output variable(secureFilePath) by $(myFile.secureFilePath)

enter image description here

Hope above helps!

0
votes

Here is example how to pass variables between tasks (in adhoc scripting ):

  1. Task one (set new variable):
steps:
- powershell: 'Write-Host "##vso[task.setvariable variable=testvar;]testvalue"'
  displayName: 'set_variable'
  1. Task two (get variable from the scope):
steps:
- powershell: 'Write-Host $(testvar)'
  displayName: 'print_variable'

This way your variable is accessible in all tasks under same job execution.

Output variables are meant to be a way for passing the value between task. If you need to pass variable between jobs, just use dependencies with output variables.

Here I made a sample stage that:

  • contains 2 jobs A & B
  • creates output variable named testvar
  • passes output variable between stages with use of dependencies
  • displays variable
trigger:
- master

jobs:
- job: A
  steps:
  - powershell: 'Write-Host "##vso[task.setvariable variable=testvar;isOutput=true;]testvalue"'
    displayName: 'set_variable'  
    name: "Setter"
- job: B
  dependsOn: A
  variables:
    # map the output variable from A into this job
    varFromA: $[ dependencies.A.outputs['Setter.testvar'] ]
  steps:
  - script: echo $(varFromA)

If it doesn't solve problem.. It means I do not understand your use case. Could you elaborate more?

Here you can see some useful documentation.

GitHub Notes Microsoft Notes