Azure Pipeline Template — Avoid Redundancy and Enable Reusability

Azure DevOps Pipelines

Azure DevOps Pipelines allows us to create a CI/CD Pipelines that can build, test and deploy the applications. Azure Pipelines gives us the feature to automate the entire CI/CD process.

Advantages of YAML Pipeline

  1. YAML Pipeline is based on Pipeline as code that gives us all the benefits of “something as code” strategy.
  2. Based on DRY Principle. Write once and reference at many places.
  3. Version controlled so each changes in the pipeline code is tracked.
  4. Easy to Migrate. Just copy the pipeline yaml and re-use it in other projects/repos/teams.
  5. Provides security against accidental deletion. If pipeline gets removed due to any reasons, then bringing a new pipeline based on YAML can be done within a minute.

Pipeline Templates in Azure DevOps

Templates helps us in reusing a piece of code multiple times at different places. If we have multiple pipelines that have common steps and task, then we can create a pipeline template in Azure DevOps and re-use the template to create the pipelines.

.NET CI Template

In this blog, we we will create an Azure Pipeline template that will build and test a .NET application.

  1. Build
  2. Run Unit Tests
  3. Calculate code coverage
  4. Generate coverage report
  5. Fail the build if code coverage is below certain threshold.
  6. Package artifact
  7. Publish artifacts
  1. Credential Scanner (CredScan)- To check for any credentials in the source code.

Template Code

.NET CI Pipeline Template
  1. We have set the code coverage threshold at 80 (line 67). You can configure this number according to your requirement.
  2. Apart from executing .NET build and test steps, we are also importing templates for whitesource scan(line 86) and template for running credential scanner(line 88).

Credential Scanner

Credential Scanner is a part of Microsoft Security Code Analysis platform and is used to scan the code for any credentials/secrets/certificates present in the code.

  1. SdtReport- To generate the scan report
  2. PublishSecurityAnalysisLogs- To store the generated logs
  3. PostAnalysis- To break the build if a credential/secrets is detected in repo.
Template to run credential scanner in pipelines

Whitesource Code Scanner

WhiteSource is a platform designed to automate open source security and compliance processes. It can scan for vulnerabilities in third party packages/applications. It generates a report which shows the list of high/medium/low vulnerabilities present in application.

Template to run Whitesource scan

Pipeline Variables in Template File

We can also define the variables that are used in templates in a separate variable template file. In the above templates, you can see that some tasks have condition parameter. Before executing the tasks, the condition is evaluated. If that condition is satisfied, then only the task is executed, otherwise it is skipped.

Template containing Variables

Creating a Pipeline with these Templates

Now, we have the required the templates to create a CI Pipeline now. This pipeline will reference the standard templates that we have created in above steps.

CI YAML Pipeline
  1. Modifying the description of a pipeline run. This feature is useful when you want custom description for each pipeline run(line-8).
  2. Declaring variables in pipeline by importing the variable template file (line 10–11). We have already created a variable template file above.
  3. Defining a BuildandPackage stage that references the .NET CI template that we created above (line 13–20).
  4. In BuildandPackage step, we are also specifying the parameter values that is required by the .NET CI template (line 9–10).

Defining the Architecture

Deployment Of Code

Till now, we have seen the CI part of the pipeline where we are building, testing and scanning the code. We can also deploy the code by writing a template that deploys the code on a particular service.


  1. In this blog, we went through the features and functionalities of YAML based pipelines in Azure DevOps.
  2. Converting the YAML Pipelines into templates can help us to reduce redundancy by reusing the templates.
  3. We can reference multiple templates in a single YAML pipeline.
  4. We can write a deploy template and reference in the same pipeline.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store