Monday, December 21, 2020

Configure YAML Pipeline Using YAML templates in Remote Repo in Azure DevOps

While we are configuring CI/CD pipelines for team projects there can be requirements to configure many CI/CD pipelines. If we could maintain set of CI/CD pipeline templates, it would be very helpful when configuring extensive number of pipelines. In this blog, we are going to discuss how to configure a pipeline when the YAML pipeline templates and YAML pipeline are in two different Azure DevOps repos. Further, you would be able to learn how to build a web app code base in third repo in same team pipeline.

Pre requisites: 

                * Repo containing Web App code 
                * Repo containing YAML CI/CD templates 
                * Repo to create YAML pipeline 

In this sample we use three Repos in same Azure DevOps team project. Team project name and repo names defined as follows.



WebbApp - Azure DevOps team project name

Template - Name of the repo which maintain YAML pipeline templates

ASPApp - Name of the repo which has the YAML pipeline

WebbApp - Name of the repo which has core web app code


We can define a Azure DevOps YAML pipeline to build the core app as explained in following YAML script. Let's discuss  each section of the full pipeline script to get proper understanding about how to refer resources in remote repos.

variables are defined in the Azure DevOps library  called "webapp" and script refer the library values.

variables:
- group: webapp

Provide details of repositories which contains YAML templates and core web app code which refer from this pipeline. Provide repository name, type and branch name of both repos under resources section.


resources:
repositories:
- repository: Template
type: git
name: DemoWebbApp/Template
ref: main - repository: DemoWebbApp
name: DemoWebbApp/DemoWebbApp
type: git
ref: master

Checkout the WebbApp repo to download the core app code which we going to build using the
pipeline.


       - checkout: DemoWebbApp

Call temp.yml template file in Template repo using following script. Note we are referring to
template YAML with @ to indicate which repo it is coming from.


       - template: temp.yml@Template

Find the full pipeline script as follows.


trigger:
none
 
pool:
  vmImage'windows-latest'

variables:
  - groupwebapp

resources:
  repositories:
  - repositoryTemplate
    typegit
    nameDemoWebbApp/Template
    refmain

  - repositoryDemoWebbApp
    typegit
    nameDemoWebbApp/DemoWebbApp
    refmaster

stages:
    - stagectemp
      jobs:
        - jobtest_module_Build_Artifacts

          steps:
           - checkoutDemoWebbApp
           - templatetemp.yml@Template


temp.yml template script as follows.

steps:
taskUseDotNet@2
  displayName'Use .NET Core sdk 2.2.207'
  inputs:
    version2.2.207

taskDotNetCoreCLI@2
  displayName'dotnet restore'
  inputs:
    commandrestore
    projects'DemoWebbApp.csproj'

taskDotNetCoreCLI@2
  displayName'dotnet build'
  inputs:
    projects'DemoWebbApp.csproj'
    arguments'--configuration $(BuildConfiguration) --no-restore'

taskDotNetCoreCLI@2
  displayName'dotnet publish'
  inputs:
    commandpublish
    publishWebProjectsfalse
    projects'DemoWebbApp.csproj'
    arguments'--configuration $(BuildConfiguration) --no-restore --no-build --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublishtrue
    modifyOutputPathtrue

taskPublishBuildArtifacts@1
  inputs:
    PathtoPublish'$(Build.ArtifactStagingDirectory)'
    ArtifactName'drop'
    publishLocation'Container'

Once build is triggered and succeeded, you would be able to see execution page similar to following image.


This blog explained how to connect three repos in Azure DevOps team project to configure a build pipeline. Further, we learned how to configure YAML pipeline using templates in a remote git repo to build the code in another repo.

No comments:

Post a Comment