<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Harshil Lodhi</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/harshillodhi/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/harshillodhi/feed.xml</link>
    <description>DevOps, Programming, Visual Studio Team Services</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:39:49 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>VSTS/TFS: Understanding Task Groups and its various use cases with setvariable logging command.</title>
      <link>https://docs.microsoft.com/archive/blogs/harshillodhi/vststfs-understanding-task-groups-and-its-various-use-cases-with-setvariable-logging-command</link>
      <pubDate>Tue, 29 Nov 2016 11:16:35 GMT</pubDate>
      <dc:creator><![CDATA[Harshil Lodhi]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harshillodhi/?p=16</guid>
      <description><![CDATA[What is a Task Group?

A Task Group (formerly called Meta Task) lets you encapsulate a sequence of...]]></description>
      <content:encoded><![CDATA[<strong>What is a Task Group?</strong>

<span>A <em>T</em></span><em>ask Group (formerly called Meta Task)</em><span> 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 <a href="https://www.visualstudio.com/en-us/docs/release/author-release-definition/understanding-tasks#metatasks">here</a> to first understand them.</span>

<strong>How do Task Groups get executed on the agent?</strong>

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.
<ol>
 	<li>During a Build creation.</li>
 	<li>During a Release creation.</li>
 	<li>During a <a href="https://www.visualstudio.com/en-us/docs/release/managing-releases/create-release#draftrelease">Draft Release</a> creation.</li>
</ol>
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 <em>$(filePath)</em> and <em>$(moreArgs)</em>

<img src="https://msdnshared.blob.core.windows.net/media/2016/11/140.png" alt="1" width="949" height="428" class="alignnone wp-image-25" />

You can see that the <span>Task Group</span> has the same two <em>filePath</em> and <em>moreArgs </em>variables as its inputs.

<img src="https://msdnshared.blob.core.windows.net/media/2016/11/236.png" alt="2" width="951" height="667" class="alignnone wp-image-35" />

Let's add this Task Group to a Release Definition and put value <em>Foo</em> for <em>filePath</em> and Bar for <em>moreArgs</em>. Save the definition.

<img src="https://msdnshared.blob.core.windows.net/media/2016/11/325.png" alt="3" width="951" height="180" class="alignnone wp-image-45" />

Create a draft release. You will see that <em>Foo</em> has replaced the <em>$(filePath)</em> and <em>Bar</em> has replaced <em>$(moreArgs)</em>

<img src="https://msdnshared.blob.core.windows.net/media/2016/11/423.png" alt="4" width="946" height="140" class="alignnone wp-image-55" />

Instead of <em>Foo</em> and <em>Bar</em>, let's put some variables.

<img src="https://msdnshared.blob.core.windows.net/media/2016/11/519.png" alt="5" width="949" height="181" class="alignnone wp-image-65" />

And then create a <a href="https://www.visualstudio.com/en-us/docs/release/managing-releases/create-release#draftrelease">draft release</a>. You can see that <em>$(filePath)</em> in our Task Group is replaced by <em>$(filePathVar)</em> and <em>$(moreArgs)</em> is replaced by <em>$(moreArgsVar)</em>

<img src="https://msdnshared.blob.core.windows.net/media/2016/11/619.png" alt="6" width="1887" height="339" class="alignnone size-full wp-image-26" />

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.

<strong>What is a setvariable <a href="https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md">logging command</a> and how to use it with Task Groups?</strong>

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 <a href="https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md">link</a> for more details

With Task Groups and setvariable logging commands, there are three possible scenarios.

(1) How to set a variable outside <span>Task Group</span> and use it inside <span>Task Group</span> ?
<ul>
 	<li>Pass it as one of the Task Group inputs. For eg. If you want to pass the variable <em>$(PassMe)</em> to a <span>Task Group</span>. You will need to have a <span>Task Group</span> input to which you will set <em>$(PassMe)</em> and you will set <em>$(PassMe)</em> in one of your tasks before the <span>Task Group</span>.</li>
</ul>
(2) How to set a variable inside <span>Task Group</span> and use it inside <span>Task Group</span>?
<ul>
 	<li>This is hacky scenario since any variable used inside of <span>Task Group</span> is exposed as an input which has to be filled with some value. Fill the input with the same variable name as the input<img src="https://msdnshared.blob.core.windows.net/media/2016/11/718.png" alt="7" width="679" height="46" class=" wp-image-27 alignnone" /></li>
 	<li>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 <span>Task Group</span> before consuming it in the same <span>Task Group</span>.</li>
 	<li>When the Task Group is expanded <em>$(filePath)</em> in tasks will be replaced by <em>$(filePath)</em> and you will be setting <em>filePath</em> in one of your tasks before consuming it.</li>
</ul>
(3) How to set a variable inside <span>Task Group</span> and use it outside of <span>Task Group</span>?
<ul>
 	<li>Simply set the value inside <span>Task Group</span>. Since the syntax to set the value doesn’t involve $(...), it won’t become an input to the Task Group.</li>
 	<li>You can then use this value in any of the tasks which are after <span>Task Group</span> in the workflow.</li>
</ul>
&nbsp;

If you have any more questions, drop in a comment.

Github issues for reference:
<ol>
 	<li><a href="https://github.com/Microsoft/vsts-tasks/issues/3116">Unexpected ##vso[task.setvariable]value behavior with Task Groups · Issue #3116 · Microsoft/vsts-task</a></li>
 	<li><a href="https://github.com/Microsoft/vsts-tasks/issues/2439"> 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</a></li>
</ol>]]></content:encoded>
    </item>
    <item>
      <title>Hello world!</title>
      <link>https://docs.microsoft.com/archive/blogs/harshillodhi/hello-world</link>
      <pubDate>Mon, 28 Nov 2016 11:18:57 GMT</pubDate>
      <dc:creator><![CDATA[Poorna Sekhar Sainu]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/harshillodhi/?p=1</guid>
      <description><![CDATA[Welcome to Developer Network. This is your first post. Edit or delete it, then start blogging!]]></description>
      <content:encoded><![CDATA[Welcome to <a href="https://blogs.msdn.microsoft.com/">Developer Network</a>. This is your first post. Edit or delete it, then start blogging!]]></content:encoded>
    </item>
  </channel>
</rss>