azure-devopstfsazure-devops-extensions

How to use setVariable in a Azure DevOps Task


I have developed an Azure DevOps (AzDO) task, it's working great. https://github.com/LanceMcCarthy/akeyless-extension-azdo

However, there's one last showstopper problem, my task's output variables are not available in subsequent tasks.

Description

As a task author, you can predefine some output variables as part of your task manifest. You are also allowed to set what's a called an "ad-hoc" output variable form your code (or script if its a powershell task)

Here's the method:

SDK.setVariable(outputVarName, varValue, true, true);

Here is a real place where I use it, but here's what the method parameters are supposed to do:

API ref

According to the SDK's documentation, using this method should provide me with some outputs:

enter image description here

Although the doc example uses the script approach, that's the same as using the SDK's setVariable() method.

Problem

Okay, now onto the problem. I am expecting that if I run this code in my extension:

SDK.setVariable('MY_COOL_OUTPUT', 'hello world', true, true);

and the task's name was:

enter image description here

I would expect the following to be available in the subsequent tasks using the output variable task-name.output-var-name syntax (as explained in this doc section).

$myValue = $(MY_TASK_NAME.MY_COOL_OUTPUT);

However, I get the following error:

MY_TASK_NAME.MY_COOL_OUTPUT : The term 'MY_TASK_NAME.MY_COOL_OUTPUT' is not recognized as the name of 
a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.

I know for a fact that using the name, as entered in the task settings, works because I do it in a previous script-only steps:

enter image description here

Further Investigation

I have tried a few different things, but no success. It's as if I have the task name wrong, but I've tried many different spelling and they never match.

Have you developed an extension before that uses ad-hoc outputs? If yes, how do the subsequent tasks reference that variable?

Thank you!


Solution

  • I have checked the Extension task demo.

    The cause of the issue is that you set the output variable in your code, but you did not set the output variable field for the task.

    Currently the output variable field of your extension is not set.

    For example:

    enter image description here

    This means that the extension task has no output variable.

    To solve this issue, you need to add the outputVariables field in the task.json file to connect the variable(MY_COOL_OUTPUT) set in the extension code.

    Here is an example:

    {
    .....
    
      "outputVariables": [
        {
            "name": "MY_COOL_OUTPUT",
            "description": "Task Output Variable"
        }
      ],
        "execution": {
            "Node16": {
                "target": "src/index.js"
            }
        }
    }
    

    In this case, when you install the extension, you will see the output variable in the task.

    For example:

    enter image description here

    On the other hand, when you set the name of the variable, you need to define the Name field of the task instead of the Display name.

    For example:

    - task: akeylessExtensionAzdo@0
      displayName: 'Akeyless Secrets'
      name: test
      inputs:
        accessid: xxx
    

    In classic pipeline, you can define the Reference name in the Output Variables field.

    If you don't set the name of the task, it will use task name by default.

    For more details, you can refer to this Github sample: task.json