Azure DevOps Pipeline Templates and External Repositories

PR Code - Christopher Pateman
2 min readOct 14, 2020

--

Working with Azure DevOps you can use YAML to create the build and deployment pipelines. To make this easier and more repeatable you can also use something called templates. However, if you want to use them in multiple repositories you don’t want to repeat yourself. There is a method to get these shared as I will demo below.

When I format my folders for holding the YAML files, I like to mirror how they were built in the UI editor in Azure DevOps website. That is with Tasks like DotNetCli and Group Tasks that are a collection of Tasks to complete a job like Build Dotnet Core Application.

DevOps
-Tasks
— DotNetCli.yml
-GroupTasks
— BuildDotnetApp.yml

In this method the ‘BuildDotnetApp.yml’ would inherit the ‘DotNetCli.yml’ and other Group Tasks could also inherit it as well. This makes them more reusable and dynamic, plus easier to upgrade if you need to change a Task version or add a new parameter.

This would be the Dot Net Core CLI Task:

parameters: diplayName: 'DotNetCoreCLI' projects: '' arguments: '' command: build customScript: '' continueOnError: false steps: - task: DotNetCoreCLI@2 displayName: ${{parameters.diplayName}} inputs: publishWebProjects: false command: ${{parameters.command}} projects: ${{parameters.projects}} arguments: ${{parameters.arguments}} zipAfterPublish: false custom: ${{parameters.customScript}} continueOnError: ${{parameters.continueOnError}}

And can then be called in like below. Remember that the folder path is relative to where this file is hosted.

steps: - template: ../Tasks/_DotNetCoreCLI.yml parameters: diplayName: 'Restore .NetCore Projects' projects: '**/MicroServices/**/*.API.csproj' arguments: '--packages $(Build.SourcesDirectory)\packages' command: restore - template: ../Tasks/_DotNetCoreCLI.yml parameters: diplayName: 'Build .NetCore Projects' projects: '**/*.csproj' arguments: '--configuration $(BuildConfiguration) --output $(Build.SourcesDirectory)\bin\$(BuildConfiguration)' command: build

You can read more on using templates in the Azure DevOps Documentation.
https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops

Find out more how to use the templates section at the original story:

http://prcode.co.uk/2020/10/14/azure-devops-pipeline-templates-and-external-repositories/

Originally published at http://prcode.co.uk on October 14, 2020.

--

--

PR Code - Christopher Pateman
PR Code - Christopher Pateman

Written by PR Code - Christopher Pateman

I’m a Azure DevOps Engineer with a wide knowledge of the digital landscape. I enjoy sharing hard to find fixes and solutions for the wider community to use.

No responses yet