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:
  - group: webapp

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

  - repository: DemoWebbApp
    type: git
    name: DemoWebbApp/DemoWebbApp
    ref: master

stages:
    - stage: ctemp
      jobs:
        - job: test_module_Build_Artifacts

          steps:
           - checkout: DemoWebbApp
           - template: temp.yml@Template


temp.yml template script as follows.

steps:
- task: UseDotNet@2
  displayName: 'Use .NET Core sdk 2.2.207'
  inputs:
    version: 2.2.207

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: 'DemoWebbApp.csproj'

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

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    publishWebProjects: false
    projects: 'DemoWebbApp.csproj'
    arguments: '--configuration $(BuildConfiguration) --no-restore --no-build --output $(Build.ArtifactStagingDirectory)'
    zipAfterPublish: true
    modifyOutputPath: true

- task: PublishBuildArtifacts@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