简体   繁体   English

Azure DevOps Pipepine 与 YAML 用于许多项目的解决方案

[英]Azure DevOps Pipepine with YAML for solution with many projects

I have a .NET MVC solution in Visual Studio 2019 that contains 3 projects:我在 Visual Studio 2019 中有一个 .NET MVC 解决方案,其中包含 3 个项目:

  1. AdminWebApp AdminWebApp
  2. SharedCode (which is set as a dependency in both of the other projects in VS) SharedCode(在 VS 的其他两个项目中都设置为依赖项)
  3. FrontWebApp FrontWebApp

In Azure DevOps Pipelines I want to create separate builds for在 Azure DevOps Pipelines 中,我想为

AdminWebApp AdminWebApp

and

FrontWebApp FrontWebApp

which will both contain the这两者都将包含

SharedCode共享代码

because it contains helpers etc. I want to do it with the YAML way.因为它包含助手等。我想用 YAML 方式来做。 Should I create 1 or 2 pipelines (each artifact will be later published to its own Azure App Service)?我应该创建 1 个还是 2 个管道(每个工件稍后将发布到它自己的 Azure 应用服务)? What is the YAML code to achieve it?实现它的YAML代码是什么?

It would really matter on how the management and release cycle looks like.管理和发布周期的样子真的很重要。 The puristic way would be to redeploy everything every-time.最纯粹的方法是每次都重新部署所有内容。 The realistic way would be to group deployment pipelines together when it make sense.现实的方法是在有意义时将部署管道组合在一起。

In terms of what to do in the "YAML way".关于以“YAML方式”做什么。 Would look at using YAML templates会考虑使用YAML 模板

The template parameter would at least by the directory of the project to build.模板参数将至少由项目的目录来构建。 This is an example of a .net Core template but will give you an idea of the thought process: For example purposes this YAML file would be called something like build-corewebapp.yml这是 .net 核心模板的示例,但会让您了解思考过程:例如,此 YAML 文件将被称为 build-corewebapp.yml

parameters:
  SolutionPath: ''
  BuildConfiguration: 'Release'
  projectName: ''
  DependsOn: []
  publish: 'false'

jobs:
- job: Build_${{ parameters.projectName }}
  dependsOn: ${{ parameters.DependsOn }}
  steps:
  - task: DotNetCoreCLI@2
    displayName: 'dotnet restore'
    inputs:
      command: 'restore'
      projects: '$(Build.SourcesDirectory)/${{ parameters.SolutionPath }}/${{ parameters.projectName }}**/*.csproj'

  - task: DotNetCoreCLI@2
    displayName: 'dotnet build'
    inputs:
      projects: '$(Build.SourcesDirectory)/${{ parameters.SolutionPath }}/${{ parameters.projectName }}**/*.csproj'
      arguments: '--configuration ${{ parameters.BuildConfiguration }}'

  - task: DotNetCoreCLI@2
    displayName: 'dotnet test'
    inputs:
      command: test
      projects: '$(Build.SourcesDirectory)/${{ parameters.SolutionPath }}/${{ parameters.projectName }}.Tests/*.csproj'
      arguments: '--configuration ${{ parameters.BuildConfiguration }} --collect "Code coverage" '
      
- job: Publish_${{ parameters.projectName }}
  dependsOn: Build_${{ parameters.projectName }}
  condition: and(succeeded(),eq( ${{ parameters.publish }}, 'true')) 
  steps:
  - task: DotNetCoreCLI@2
    displayName: 'dotnet publish'
    inputs:
      command: publish
      publishWebProjects: false
      projects: '$(Build.SourcesDirectory)/${{ parameters.SolutionPath }}/${{ parameters.projectName }}**/*.csproj'
      arguments: '--configuration ${{ parameters.BuildConfiguration }} --output $(build.artifactstagingdirectory)'
      zipAfterPublish: True
  - task: PublishBuildArtifacts@1
    displayName: 'Publish Artifact: drop'

A template would be called by something similar to:模板会被类似的东西调用:

  jobs:
  - template: build-corewebapp.yml
    parameters:
      projectName: ${{ variables.appProjectName }}
      solutionPath: $(solutionPath)
      publish: 'true'

For max re usability I would recommend any kind of build template to exist in a separate repository so it can be used by other repos .为了最大的可重用性,我建议将任何类型的构建模板存在于单独的存储库中,以便其他存储库可以使用它 This would be setup in your pipeline by referencing the repo similar to:这将通过引用类似于以下内容的 repo 在您的管道中进行设置:

resources:
  repositories:
  - repository: repositoryTemplate
    type: git
    name: ProjectName/YAMLTEMPLATERepoName

The pro to using templates is then updating a task version or changing a build/deployment strategy can be updated and referenced in one place.然后,使用模板的优点是更新任务版本或更改构建/部署策略,可以在一个地方更新和引用。

You can use conditions to make separate builds, so that you can put all build steps in one pipeline.您可以使用条件进行单独的构建,以便您可以将所有构建步骤放在一个管道中。 Here's similar topic .这是类似的主题

Simple sample for your build steps:构建步骤的简单示例:

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $files=$(git diff HEAD HEAD~ --name-only)
      $temp=$files -split ' '
      $count=$temp.Length
      For ($i=0; $i -lt $temp.Length; $i++)
      {
        $name=$temp[$i]
        echo "this is $name file"
        if ($name -like "AdminWebApp/*")
          {
            Write-Host "##vso[task.setvariable variable=RunAdminWebApp]True"
          }
        if ($name -like "SharedCode/*")
          {
            Write-Host "##vso[task.setvariable variable=RunSharedCode]True"
          }
        if ($name -like "FrontWebApp/*")
          {
            Write-Host "##vso[task.setvariable variable=RunFrontWebApp]True"
          }
      }
- task: MSBuild@1
  inputs:
    solution: '**/AdminWebApp.csproj'
    msbuildArguments: 'xxx'
  condition: or(variables['RunAdminWebApp'], variables['RunSharedCode'])
- task: MSBuild@1
  inputs:
    solution: '**/FrontWebApp.csproj'
    msbuildArguments: 'xxx'
  condition: or(variables['RunFrontWebApp'], variables['RunSharedCode'])

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'
  • If any file in your AdminWebApp project changes, then only build the AdminWebApp and SharedCode project.(First build task)如果您的AdminWebApp项目中的任何文件发生更改,则仅构建AdminWebAppSharedCode项目。(第一个构建任务)

  • If any file in your FrontWebApp project changes, then only build the FrontWebApp and SharedCode project.(Second build task)如果您的FrontWebApp项目中的任何文件发生更改,则仅构建FrontWebAppSharedCode项目。(第二次构建任务)

  • And if the file in SharedCode changes, since the two projects depend on it, both two build tasks will run.如果SharedCode中的文件发生变化,由于两个项目都依赖它,两个构建任务都会运行。

You should specify the msbuild arguments(/t:publish...) so that the build task can generate zip package to deploy.您应该指定 msbuild 参数(/t:publish...),以便构建任务可以生成 zip package 进行部署。 (Otherwise you need to add additional task to zip the output files) (否则您需要向 zip 和 output 文件添加额外的任务)

Since you'll get two published zip files once the SharedCode project has changes.因为一旦SharedCode项目发生更改,您将获得两个已发布的 zip 文件。 Then your release pipeline should at least have two deploy tasks.那么你的发布管道应该至少有两个部署任务。 For you release: One PS task(determine whether the A.zip/B.zip exists and then set custom variables DeployA/DeployB) and two conditional Deploy tasks based of the value of DeployA/DeployB.(Just a suggestion,it's not about your original issue, so I won't talk much about it here...)为您发布:一个 PS 任务(确定 A.zip/B.zip 是否存在,然后设置自定义变量 DeployA/DeployB)和两个基于 DeployA/DeployB 值的条件部署任务。(只是一个建议,它不是关于你原来的问题,所以我不会在这里多说......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM