loading...

Azure DevOps YAML pipeline : Template = task group

kenakamu profile image Kenichiro Nakamura ・2 min read

When I author build/release pipelines, I often used task group so that I can reuse the set of tasks in multiple pipelines.

Since last year, I switched to use YAML pipeline instead of GUI, and all of sudden, I need to say good-bye to task group, and say hello to Template

In this article, I create a repository which contains a set of shared .yml files for reuse.

Setup Azure DevOps

I have two repositories:

  • myapp: Store my application. I just put a text file for now.
  • taskgroup: Store shared yml.

Create shared yml file

I usually create pipeline yml by using pipeline editor as it has IntelliSense support.

1. Go to Pipelines and click New pipeline.

2. Select "Azure Repos Git".

3. Select "taskgroup" repository.

4. Select "Starter pipeline".

5. Change the file name to hello-user.yml and replace the code. This could be the simplest yml with a parameter. See Parameters for more detail.

parameters:
  - name: yourName
    type: string
    default: 'world'

steps:
  - script: echo Hello ${{ parameters.yourName}}
    displayName: 'Hello user'

6. Save the change to commit the file.

Create consumer pipeline

Next, I author a pipeline in myapp repository and use hellp-user.yml.

1. Go to Pipelines and click New pipeline.

2. Select "Azure Repos Git".

3. Select "myapp" repository.

4. Select "Starter pipeline".

5. Update the code. I keep the file name for now.

  • Use resources to reference taskgroup repository
  • The type is git which means AzureDevOps repository in this case
  • Use @ to reference the yml
  • Use parameters to set parameter values.
trigger:
- master

resources:
  repositories:
    - repository: taskgroup
      type: git
      name: taskgroup

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- template: hello-user.yml@taskgroup
  parameters:
    yourName: 'ken'

6. Save and run the pipeline.

Result

When I run the pipeline, I can see that the pipeline references both myapp and taskgroup repositories.

Alt Text

I also confirmed that the shared yml ran as expected with correct parameter value.

Alt Text

Summary

I can use template as if it's task group. The biggest difference for me is that I can move the shared tasks easier as it's all stored as source code.

Discussion

pic
Editor guide