Increasing Code Reusability Using Task Groups in Azure DevOps

Let’s assume a scenario in which you are repeating a few tasks from your pipeline into multiple stages and/or pipelines or projects. In that case, it gets really tiring to repeat and configure each task individually over and over again. Azure DevOps provides the feature of Task Group in which we can encapsulate a sequence of tasks from our build or release pipelines and reuse those tasks in other pipelines.

What is Azure DevOps?

Now, let’s talk about Azure DevOps, it is a mixture of the simplest technologies and best practices. Therefore we can go as far as saying that it is the Next Big Thing in the IT Industry. Azure DevOps is a Software as a service (SaaS) platform from Microsoft providing an end-to-end DevOps toolchain for developing and deploying software. Microsoft launched this as they understood the fact that DevOps has become vital to a team’s success.

Task Group

task group facilitates the encapsulation of a sequence of tasks, defined already in a build or a release pipeline, into a single reusable task that can be added to a build or release pipeline (like any other task). We can, as per our choice, extract parameters from the encapsulated tasks as configuration variables, and abstract the rest of the task information.

The new task group is automatically added to the task catalog, ready to be added to other releases and build pipelines. At the project level, the task groups are stored and are not accessible outside the project scope.

Let’s Get Started

Step 1: Let’s go to one of our release pipelines and select a few steps that we would like to create a group of.

image

Step 2: Now Right Click over the selected tasks and select create task group.

image

Step 3: Save the Task Group.

image
image

Now we can reuse the same task group in any of our release pipelines.

Similarly, we can perform the same steps to save a task group from the build pipeline.

Step 4: Now let’s reuse the same Task Group.

image
image

Step 5: Search for the Task Group you just created and add the task into your pipeline.

image

And that’s how we can reuse the repeating tasks into different pipelines.

image

Useful Tips:

It is recommended to ensure that all of the tasks you want to include in a task group have their parameters defined as variables, such as $(MyVariable), where you want to be able to configure these parameters when you use the task group. The variables used in the tasks are automatically extracted and converted into parameters for the task group. With that, the values of the given configuration variables will be converted into their default values for the task group.

If we specify the value (instead of a variable) for a parameter, that value becomes a fixed parameter value and cannot be exposed as a parameter to the task group.

Let’s see a lab for that too.

Let’s assume we have two Java-based projects

STEP 1: Create a Classic Pipeline for Project1 with maven template

STEP 2: For the Maven task give the absolute path of the pom.xml file

STEP 3: Now let’s save and run our job.

Our objective is to create a template of these steps but as we have observed that the directory containing the pom.xml file is different and we need to change the directory every single time.

So, let’s parameterize our directory argument.

STEP 4: Add a parameter in POM file path

STEP 5: Create a Task Group for the tasks.

STEP 6: Now create a new pipeline for the second project and add the task group that we previously added.

STEP 7: Now add the directory name for our Project2 and save and run the pipeline.

STEP 8: Let’s see the outputs.

And that’s how we can parameterize our arguments and make our pipeline extensively lively, reusable, and dynamic.

Conclusion:

Task Group is a really handy feature provided by Azure DevOps that comes into the picture when dealing with a big number of pipelines that requires repetition of tasks. It helps us simplify our pipelines and makes them more human-readable.

Thanks for reading this article! I hope you benefited from it.

Blog Pundits: Mehul Sharma and Sandeep Rawat

Opstree is an End to End DevOps solution provider.

Connect with Us

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: