DevOps 管道 - 在构建步骤中找不到包

DevOps Pipeline - can't find packages during build step

我正在尝试将现有的 ASP.NET MVC Web 应用程序从 TeamCity 转移到 Azure DevOps,但是我似乎无法让我的管道找到它从 NuGet 包恢复的包恢复任务。

该解决方案有多个项目,它们使用了大量的 NuGet 包。我创建了一个多步骤管道,目前有一个 NuGet 恢复任务和一个 VS 构建任务。

解决方案结构是这样的:

\source
..\SolutionFolder
...Solution.sln
..\..\Project1
....Project1.csproj
..\..\Project2
....Project2.csproj
..\..\Packages
azure-pipelines.yaml
NuGet.config

管道 YAML 如下所示:

trigger:
- PipelineBranch

pool:
  vmImage: 'windows-latest'

stages:

## NuGet Restore ##
- stage: nuget_restore
  displayName: 'Restore nuget packages'
  jobs:
  - job: nuget_package_restore
    steps:
    - task: NuGetCommand@2
      displayName: 'NuGet restore'
      inputs:
        command: 'restore'
        restoreSolution: 'source/SolutionFolder/Solution.sln'
        feedsToUse: 'config'
        nugetConfigPath: 'NuGet.Config'
        restoreDirectory: '$(Build.SourcesDirectory)\source\SolutionFolder\packages'

## Build ##
- stage: build_for_int
  displayName: 'Build to int)'
  jobs:
  - job: run_build
    pool:
      vmImage: 'windows-latest'
    steps:
      - task: VSBuild@1
        displayName: 'Build Solution.sln (int)'
        inputs:
          solution: 'source/SolutionFolder/Solution.sln'
          msbuildArgs: '
          /p:DeployOnBuild=true 
          /p:WebPublishMethod=Package 
          /p:PackageAsSingleFile=true 
          /p:SkipInvalidConfigurations=true 
          /p:PackageLocation="$(Build.ArtifactStagingDirectory)"'
          platform: '$(BuildPlatform)'
          configuration: '$(BuildConfiguration)'

当管道运行时,恢复任务成功,输出如下:

<example package>

Added package 'postal.1.2.2' to folder 'd:\a\s\source\SolutionFolder\packages'

<loads more packages>

Installed:
    190 package(s) to packages.config projects

当构建任务在解决方案上运行时,它在遇到的第一个项目上失败并出现以下错误:

[warning]C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): 
Warning MSB3245: Could not resolve this reference. Could not locate the assembly "postal, Version=1.2.14706.0, Culture=neutral, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
...
Considered "d:\a\s\source\SolutionFolder\packages\postal.1.2.2\lib\postal.dll", but it didn't exist.

每个包依赖项都会出现相同的错误。看起来包恢复任务将包恢复到这里:

d:\a\s\source\SolutionFolder\packages\

并且根据构建日志,它正在分层包文件夹结构中查找它们,例如:

d:\a\s\source\SolutionFolder\packages\<package>\<lib>\<dll>

我已经尝试了很多事情,包括从恢复任务中删除 restoreDirectory 参数,运行 本地 MSBuild 命令(它有效)以及删除一些包,但是我'我仍然看到相同的构建错误。

一些项目文件也包含提示路径;例如格式:

  <ItemGroup>
    <Reference Include="<some package>">
      <HintPath>..\packages\<package>\lib\<dll></HintPath>
    </Reference>

有人有什么见解吗?谢谢。

您应该在同一个 job[=22= 下包含任务 VSBuild@1NuGetCommand@2 ].

我在本地托管代理上进行了测试,发现运行 vsbuild 任务的第二个作业将清除工作文件夹并重新获取源代码,这会删除由第一份工作任务 nugetcommand。这可能是导致错误的原因。

所以管道 yaml 应该是这样的

    trigger:
    - PipelineBranch
    pool:
      vmImage: 'windows-latest'
    stages:
    ## NuGet Restore ##
    - stage: nuget_restore
      displayName: 'Restore nuget packages'
      jobs:
      - job: nuget_package_restore
        steps:
        - task: NuGetCommand@2
          displayName: 'NuGet restore'
          inputs:
            command: 'restore'
            restoreSolution: 'source/SolutionFolder/Solution.sln'
            feedsToUse: 'config'
            nugetConfigPath: 'NuGet.Config'
            restoreDirectory: '$(Build.SourcesDirectory)\source\SolutionFolder\packages'

        - task: VSBuild@1
          displayName: 'Build Solution.sln (int)'
          inputs:
            solution: 'source/SolutionFolder/Solution.sln'
            msbuildArgs: '
            /p:DeployOnBuild=true 
            /p:WebPublishMethod=Package 
            /p:PackageAsSingleFile=true 
            /p:SkipInvalidConfigurations=true 
            /p:PackageLocation="$(Build.ArtifactStagingDirectory)"'
            platform: '$(BuildPlatform)'
            configuration: '$(BuildConfiguration)'