다음을 통해 공유


VSTS/TFS: Understanding Task Groups and its various use cases with setvariable logging command.

What is a Task Group?

A Task Group (formerly called Meta Task)  lets you encapsulate a sequence of tasks already defined in a build definition or a release definition into a single reusable task that can be added to a build definition or release definition just like any other task. You can choose to extract the parameters from the encapsulated tasks as configuration variables, and abstract the rest of the task information. If you haven't explored them yet, see the docs here to first understand them.

How do Task Groups get executed on the agent?

This is an interesting area where understanding the product internals helps. A Task Groups is only an aid during authoring of Build/Release definitions. It gets expanded(flattened) into the tasks which it encapsulates before the entire workflow of tasks is sent to the agent for execution. This means that agent has no knowledge of a Task Group is and it only gets a list of the flattened workflow of tasks which it has to execute. There are three places where the Task Groups are expanded recursively to the list of tasks.

  1. During a Build creation.
  2. During a Release creation.
  3. During a Draft Release creation.

The (3) is an interesting scenario where we can see before execution how the Task Group input values actually  replace the $(...) variables in the tasks contained inside Task Group. We will be able to see the whole flattened list of tasks that will be executed on the agent. The same expansion happens in (1) and (2) but we will go through (3) since it is much easier to understand.

Let's get started with an example.

Our Task Group will contain a single PS task with two variables $(filePath) and $(moreArgs) 1

You can see that the Task Group has the same two filePath and moreArgs variables as its inputs.

2

Let's add this Task Group to a Release Definition and put value Foo for filePath and Bar for moreArgs. Save the definition.

3

Create a draft release. You will see that Foo has replaced the $(filePath) and Bar has replaced $(moreArgs) 4

Instead of Foo and Bar, let's put some variables.

5

And then create a draft release. You can see that $(filePath) in our Task Group is replaced by $(filePathVar) and $(moreArgs) is replaced by $(moreArgsVar) 6

So we can see that variables inside tasks are simply replaced by Task Group input values before the workflow is sent to the agent for execution.

What is a setvariable logging command and how to use it with Task Groups?

It is special log statement in a task which can be used to dynamically set a variable to a value which can then be consumed by tasks later in the workflow. See this link for more details

With Task Groups and setvariable logging commands, there are three possible scenarios.

(1) How to set a variable outside Task Group and use it inside Task Group ?

  • Pass it as one of the Task Group inputs. For eg. If you want to pass the variable $(PassMe) to a Task Group. You will need to have a Task Group input to which you will set $(PassMe) and you will set $(PassMe) in one of your tasks before the Task Group.

(2) How to set a variable inside Task Group and use it inside Task Group?

  • This is hacky scenario since any variable used inside of Task Group is exposed as an input which has to be filled with some value. Fill the input with the same variable name as the input7
  • You may not need to define this variable in your Variables section, you will just need to make sure that you are setting it in one of your tasks in Task Group before consuming it in the same Task Group.
  • When the Task Group is expanded $(filePath) in tasks will be replaced by $(filePath) and you will be setting filePath in one of your tasks before consuming it.

(3) How to set a variable inside Task Group and use it outside of Task Group?

  • Simply set the value inside Task Group. Since the syntax to set the value doesn’t involve $(...), it won’t become an input to the Task Group.
  • You can then use this value in any of the tasks which are after Task Group in the workflow.

 

If you have any more questions, drop in a comment.

Github issues for reference:

  1. Unexpected ##vso[task.setvariable]value behavior with Task Groups · Issue #3116 · Microsoft/vsts-task
  2. Write-Host "##vso[task.setvariable variable=someVar;]someValue" sets are (sometimes) overwritten with 'default' values in subsequent PowerShell -Argument steps · Issue #2439 · Microsoft/vsts-tasks

Comments

  • Anonymous
    November 29, 2016
    Harshil,Thanks for the post. I think task groups are great. What's the roadmap for this feature? Are you planning to allow some sort of mechanism to control whether a task within the group is enabled or disabled during runtime? Perhaps via parameters passed in?
    • Anonymous
      November 29, 2016
      @JSDX - I don't have a roadmap for task groups at this moment. Thanks for the feedback. I have forwarded your feedback to the team. I will post a reply if I got something concrete about the future plans.
  • Anonymous
    November 29, 2016
    Oh, and how about collection- and account-level task groups?