Azure DevOps适用于包含多种项目类型的解决方案的构建流水线

4
我有一个包含以下内容的解决方案:
  • 几个Asp.net项目 (微服务和网关)

  • .Net Core + Angular 8 (前端)

当我在Visual Studio中点击构建按钮时,所有项目都会被构建。我已经创建了一个仓库并上传了解决方案。现在我正在尝试弄清如何设置管道以构建每个微服务并将它们部署到单独的Azure Web应用程序中。
我为Angular项目准备了以下管道。我应该像这样定义单独的任务吗?是否有一种方法可以在此处复制Visual Studio构建呢?
# 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

朋友,这个问题有任何更新吗?请检查Krzysztof Madej的答案是否有助于解决此问题,只是提醒一下 :) - LoLance
@LanceLi-MSFT 我已经使用 VSBuild 完全构建了解决方案。 - techno
好主意,它能在你目前的情况下很好地工作吗?也许你可以将其添加为自我回答。 - LoLance
@LanceLi-MSFT,我已经接受了Krzysztof的答案。 - techno
1个回答

12
您可以采用以下两种方法之一:
  1. 针对整个代码库使用一个流水线
  2. 为项目使用一个流水线
在这两种情况下,您都可以使用模板来避免重复; 因此,您将定义通用的构建 .NET 项目任务,并在管道中使用它们。我最近写了一篇关于这个问题的博客文章,但也请参阅文档
要实现这一点,您需要首先定义一个 YAML 文件,其中包含通用步骤。例如:
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  

然后从你的主构建文件中调用此文件:

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。对于此,您可以定义多个管道(每个项目一个)并将触发器限制为特定文件夹中的更改。请参阅此处。这里是一个示例,演示如何将触发器限制为master分支的特定文件夹:
trigger:
  branches:
    include:
    - master
  paths:
    include:
    - gated-checkin-with-template/*
    exclude:
    - gated-checkin-with-template/azure-pipelines-gc.yml

你现在有空讨论这个吗? - techno
请告诉我您何时有空,以及您所在的时区。 - techno
即使现在,在时区8AM-4PM,10 PM-01AM UTC。 - Krzysztof Madej
请进入聊天室 https://chat.stackoverflow.com/rooms/211687/room-for-techno-and-krzysztof - techno

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接