Monday 17 September 2018

Making a Task Group Parameter Optional – Azure DevOps Pipelines

Task groups are really useful to share common actions with multiple build or release pipelines in Azure DevOps (VSTS). You can group multiple tasks ang create a task group forma build or release definition. then these task groups can be utilized in other build or release definitions in a given team project. Parameters in a task group help to pass values from build or release definition to the tasks in the task group. You can add a default value for these parameters or have to provide value from the build or release definition that is using the task group. Making a task group parameter optional is not straight forward and you need some do work around to get it working. Let’s look at how we can make a parameter optional for a task group in Azure DevOps pipelines.


Below is a task group created with a PowerShell task accepting two parameters.image

$SqlDBServer and $SqlDBName  are used as the local variables and $(SqlDatabaseServer) and $(SqlDatabaseName) are the input parameters. Note the single quotes which is important in the PowerShell if you are having PowerShell scripts as your task group steps. This will make two parameters available for the task group. You can provide a description for each parameter and a default value. However, default value is not utilized in this sample as we are tying to make the parameters optional.image

If you try to use this task group in a build or a  release definition you will see it demands for both parameters as required. In a side note, you can see the descriptions added to parameters are visible to the release or build definition.image

How can we make one of the se parameters optional? For that you have no out of the box option available in Azure DevOps as of now. You have to export the task group and manually edit the json file to get it done. First export and save the json file.image

You can then open the downloaded json file with Visual Studio or Visual Studio Code. You can see the opened file as a single line of code which is hard to understand and modify.image

Press Ctrl+k Ctrl+D in Visual Studio or Shift+Alt+F in Visual Studio Code to format the json file (for that matter to format any code file). After formatting you can see there is a section named inputs and each of the parameters available there. You can set the required false for any input parameter in the json file and save it.image

Then you have to import the json file in task groups. Unfortunately this will only make a copy of the task group and you would not be able to overwrite existing task group.image

Browse for the file saved and import as a new Task group.image

image

Rename the imported task group name to your preferred name (you cannot overwrite existing task group) and save it.image

Go to the build or release definition add new task group (you may have to do full refresh of the build/ release definition page with a Ctrl+F5 to make the changes in task groups to be effectively visible to you). You would be able to see not the optional parameter from task group is not demanding for a value. Not a neat solution but it works!!!image


No comments:

Popular Posts