techno
techno

Reputation: 6500

Azure Devops Build Pipeline for solution with multiple project types

I have a solution that contains the following

When I hit the build button in Visual Studio every project is built. I have created a repo and uploaded the solution. Now I'm trying to figure out how to setup the pipeline to build each microservice and deploy them to individual Azure Web Apps.

I have the following Pipeline for the Angular Project. Should I define separate tasks like this? Is there a way to replicate the Visual Studio build here?

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: Npm@1
  inputs:
    command: install
    workingDir: 'd:\a\1\s\Ok.Web\ClientApp'

- task: Npm@1
  inputs:
    command: custom
    workingDir: 'd:\a\1\s\Ok.Web\ClientApp'
    customCommand: run build

- task: CopyFiles@2
  inputs:
    targetFolder: '$(Build.ArtifactStagingDirectory)'    

- task: PublishBuildArtifacts@1

Upvotes: 5

Views: 22579

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40663

You may apply one of two approaches here:

  1. One pipeline for whole repo
  2. One pipeline for project

In both cases you may use templates to avoid repeating yourself; so you will define common tasks for building a .NET project and then use them in pipelines. I recently made a blog post about this, but please take a look at the documentation too.

To achieve this you need to first define a YAML file with common steps. For instance:

parameters:
- name: buildConfiguration # name of the parameter; required
  default: 'Release'
- name: projectFolder
  default: ' '

steps:

- task: DotNetCoreCLI@2
  displayName: Restore nuget packages
  inputs:
    command: restore
    projects: '*.csproj'
    workingDirectory: '${{ parameters.projectFolder}}'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    command: build
    projects: '${{ parameters.projectFolder}}'
    arguments: '--configuration ${{ parameters.buildConfiguration }}'

# You just added coverlet.collector to use 'XPlat Code Coverage'
- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: test
    projects: '*Tests/*.csproj'
    arguments: '--configuration ${{ parameters.buildConfiguration }} --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
    workingDirectory: '${{ parameters.projectFolder}}'

- task: DotNetCoreCLI@2
  inputs:
    command: custom
    custom: tool
    arguments: install --tool-path . dotnet-reportgenerator-globaltool
  displayName: Install ReportGenerator tool

- script: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:${{ parameters.projectFolder}}/coverlet/reports -reporttypes:"Cobertura"
  displayName: Create reports

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: ${{ parameters.projectFolder}}/coverlet/reports/Cobertura.xml  

And then invoke this file from you main build file:

variables:
  buildConfiguration: 'Release'
  projectFolder: 'path to your project'

steps:

- template: build-and-test.yaml
  parameters:
      buildConfiguration: $(buildConfiguration)

- script: echo Some steps to create artifacts!
  displayName: 'Run a one-line script'

In approach number 1 you will build all projects even if you change just one project, so I would recommend you use approach number 2. For this you may define multiple pipelines (one per project) and limit triggers to changes in specific folder. Please take a look here.

Here you have an example of how you can limit triggers to specific folder for master branch:

trigger:
  branches:
    include:
    - master
  paths:
    include:
    - gated-checkin-with-template/*
    exclude:
    - gated-checkin-with-template/azure-pipelines-gc.yml

Upvotes: 13

Related Questions