Photo by Josh Redd

Easily modify and reuse your task groups using Azure DevOps

Share your task groups!

Posted by Damien Aicheh on 05/28/2022 · 5 mins

When you create a new Releases Pipeline using the Classic interface (without the YAML) you need to configure it manually all inside the Azure DevOps interface.

To be able to reuse a your scripts and tasks multiples times, you need to use task groups. As a reminder, here is an example:

Task groups

To version it or share it to your different projects you need to export it as JSON by clicking on the Export button like you can see above.

Based on the example above, you will download a JSON file named like your task group.

{
    "tasks": [
        {
            "environment": {},
            "displayName": "terraform init",
            "alwaysRun": false,
            "continueOnError": false,
            "condition": "succeeded()",
            "enabled": true,
            "timeoutInMinutes": 0,
            "retryCountOnTaskFailure": 0,
            "inputs": {
                "targetType": "inline",
                "filePath": "",
                "arguments": "",
                "script": "terraform init \\\n-input=false \\\n-backend-config=\"resource_group_name=$(RESOURCE_GROUP)\" \\\n-backend-config=\"storage_account_name=$(TF_STORAGE_ACCOUNT)\" \\\n-backend-config=\"container_name=tfstate\" \\\n-backend-config=\"key=backend.$(ENV_NAME).terraform.tfstate\" \\\n-reconfigure",
                "workingDirectory": "",
                "failOnStderr": "false",
                "bashEnvValue": ""
            },
            "task": {
                "id": "6c731c3c-3c68-459a-a5c9-bde6e6595b5b",
                "versionSpec": "3.*",
                "definitionType": "task"
            }
        }, 
        // The other lines of the file are voluntary omitted for clarity
    ], 
}

As you will note, the issue with this export is the readability of the scripts. If you want to put it in a source code manager like Git and add some pull requests management with your team it will be complex to modify without Azure DevOps.

YAML to the rescue

To help you doing this you can use a simple tool called yq. This will help you to convert your JSON file into a readable YAML file.

By running this simple command on your file:

yq -P . YOUR_TASK_GROUP_FILE.json > YOUR_TASK_GROUP_FILE.yaml

You will end up with a readable and editable yaml script:

- environment: {}
    displayName: terraform init
    alwaysRun: false
    continueOnError: false
    condition: succeeded()
    enabled: true
    timeoutInMinutes: 0
    retryCountOnTaskFailure: 0
    inputs:
      targetType: inline
      filePath: ""
      arguments: ""
      script: |-
        terraform init \
        -input=false \
        -backend-config="resource_group_name=$(RESOURCE_GROUP)" \
        -backend-config="storage_account_name=$(TF_STORAGE_ACCOUNT)" \
        -backend-config="container_name=tfstate" \
        -backend-config="key=backend.$(ENV_NAME).terraform.tfstate" \
        -reconfigure
      workingDirectory: ""
      failOnStderr: "false"
      bashEnvValue: ""
    task:
      id: 6c731c3c-3c68-459a-a5c9-bde6e6595b5b
      versionSpec: 3.*
      definitionType: task
    # The other lines of the file are voluntary omitted for clarity

The script part is displayed exactly like in Azure DevOps and as a bonus the file exported is shorter: we go from 240 to 180 lines in this example.

Re-import your task groups

When you have to import your task group again to Azure DevOps, you just have to run the command below to generate the JSON file from the YAML:

yq -o=json '.' YOUR_TASK_GROUP_FILE.yaml > YOUR_TASK_GROUP_FILE.json

Then you can import it:

Import Task groups

Final touch

With this method, you will be able to save some time by reusing your Tasks groups and version them. Also it will be more easy to read and maintain them outside of Azure DevOps.

Happy coding !

Do not hesitate to follow me on to not miss my next tutorial!