首页 > 解决方案 > dotnet build, exclude only one dependency from build

问题描述

Consider the following project structure:

I've set up a pipeline in Azure devOps that performs a restore, build and publish like this:

jobs:
  - job: api-a
    steps:
      - task: DotNetCoreCLI@2
        displayName: Restore
        inputs:
          command: restore
          projects: "MyProject.Api.a.csproj"
      - task: DotNetCoreCLI@2
        displayName: Build
        inputs:
          command: build
          projects: "MyProject.Api.a.csproj"
          arguments: "--configuration $(buildConfiguration)"
      - task: DotNetCoreCLI@2
        displayName: Publish
        inputs:
          command: publish
          projects: "MyProject.Api.a.csproj"
          publishWebProjects: false
          arguments: "--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/project-api-a-publish"
      - task: PublishPipelineArtifact@1
        displayName: Publish release Artifact
        inputs:
          targetPath: "$(Build.ArtifactStagingDirectory)/project-api-a-publish"
          artifactName: "a-publish"
  - job: api-b
    steps:
      - task: DotNetCoreCLI@2
        displayName: Restore
        inputs:
          command: restore
          projects: "MyProject.Api.b.csproj"
      - task: DotNetCoreCLI@2
        displayName: Build
        inputs:
          command: build
          projects: "MyProject.Api.b.csproj"
          arguments: "--configuration $(buildConfiguration)"
      - task: DotNetCoreCLI@2
        displayName: Publish
        inputs:
          command: publish
          projects: "MyProject.Api.b.csproj"
          publishWebProjects: false
          arguments: "--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory)/project-api-b-publish"
      - task: PublishPipelineArtifact@1
        displayName: Publish release Artifact
        inputs:
          targetPath: "$(Build.ArtifactStagingDirectory)/project-api-b-publish"
          artifactName: "b-publish"

problem is that MyProject.Data is not buildable from source, it needs an external tool to run first that generates some C# classes. Before this step, the project won't be able to build. So I added this:

      - task: DotNetCoreCLI@2
        displayName: "Restore tools"
        inputs:
          workingDirectory: "MyProject.Data"
          command: custom
          custom: tool
          arguments: restore --interactive --configfile ../NuGet.config
      - task: DotNetCoreCLI@2
        displayName: my-codegen-tool
        inputs:
          workingDirectory: "MyProject.Data"
          command: custom
          custom: tool
          arguments: run my-codegen-tool

This all works, but the codegen tool needs to run on each API project job I'm running, making my build quite slow. I was hoping there would be some way to only run the codegen tool once, and then all API projects could use the binaries from the data project where the files were generated?

Ideally, I would have to be able to prebuild the data project in a separate job, publish the dll's as an artifact, and then use those dll's in my subsequent API builds. I'm guessing this would be possible with dotnet build --no-dependencies, but that would implicate I need to build everything else separately as well, which is undesirable from a maintainability point of view.

标签: .net-coreazure-pipelines

解决方案


You can try to combine multiple jobs into one job. First generate the required C # classes through the first two tasks, and then build with wildcards (e.g. **/*.csproj for all .csproj files in all subfolders) in the dotnet build task. You can consider removing restore tasks, because dotnet restore is run implicitly in dotnet build.

This is stated here : You don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack. To disable implicit restore, use the --no-restore option.

- job: 
    steps:
      - task: DotNetCoreCLI@2
        displayName: "Restore tools"
        inputs:
          workingDirectory: "MyProject.Data"
          command: custom
          custom: tool
          arguments: restore --interactive --configfile ../NuGet.config
      - task: DotNetCoreCLI@2
        displayName: my-codegen-tool
        inputs:
          workingDirectory: "MyProject.Data"
          command: custom
          custom: tool
          arguments: run my-codegen-tool
      - task: DotNetCoreCLI@2
        displayName: Build
        inputs:
          command: build
          projects: "**/*.csproj"
          arguments: "--configuration $(buildConfiguration)"

推荐阅读