用于具有多种项目类型的解决方案的 Azure Devops Build Pipeline [英] Azure Devops Build Pipeline for solution with multiple project types

查看:26
本文介绍了用于具有多种项目类型的解决方案的 Azure Devops Build Pipeline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的解决方案

I have a solution that contains the following

  • 几个 Asp.net 项目(微服务和网关)

  • Several Asp.net Projects (Microservices and Gateway)

.Net Core + Angular 8(前端)

.Net Core + Angular 8 (Front End)

当我在 Visual Studio 中点击构建按钮时,每个项目都会被构建.我已经创建了一个 repo 并上传了解决方案.现在我想弄清楚如何设置管道以构建每个微服务并将它们部署到单个 Azure Web 应用程序.

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.

我有以下用于 Angular 项目的管道.我应该像这样定义单独的任务吗?有没有办法在这里复制 Visual Studio 构建?

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://docs.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

推荐答案

您可以在此处应用以下两种方法之一:

You may apply one of two approaches here:

  1. 整个存储库的一个管道
  2. 一个项目管道

在这两种情况下,您都可以使用模板来避免重复;因此,您将定义构建 .NET 项目的常见任务,然后在管道中使用它们.我最近发表了一篇关于this的博客文章,但请查看 文档也是.

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.

要实现这一点,您需要先定义一个包含常见步骤的 YAML 文件.例如:

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'

在方法 1 中,即使您只更改一个项目,您也将构建所有项目,因此我建议您使用方法 2.为此,您可以定义多个管道(每个项目一个)并将触发器限制为特定文件夹中的更改.请看一看 这里.

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

这篇关于用于具有多种项目类型的解决方案的 Azure Devops Build Pipeline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆