【问题标题】:How can I share a built .NET Core project between many jobs in Azure pipelines?如何在 Azure 管道中的多个作业之间共享构建的 .NET Core 项目?
【发布时间】:2019-12-04 08:03:36
【问题描述】:
- job: buildAndTestJob
 steps:
  - task: DotNetCoreCLI@2
    displayName: dotnet restore
    inputs:
      command: restore
      vstsFeed:  $(vstsFeed)
  - task: DotNetCoreCLI@2
    displayName: 'dotnet build'
    inputs:
      arguments: '--configuration ${{ parameters.buildConfiguration }}'
  - task: DotNetCoreCLI@2
    displayName: 'dotnet test'
    inputs:
      command: test
      arguments: '--configuration ${{ parameters.buildConfiguration }}'
  - task: CopyFiles@2
    displayName: copy bin files
    inputs:
      sourceFolder: '$(Build.SourcesDirectory)'
      contents: '**/bin/**/*'
      TargetFolder: '$(Build.ArtifactStagingDirectory)'
  - task: CopyFiles@2
    displayName: copy obj files
    inputs:
      sourceFolder: '$(Build.SourcesDirectory)'
      contents: '**/obj/**/*'
      TargetFolder: '$(Build.ArtifactStagingDirectory)'
  - task: PublishBuildArtifacts@1
    displayName: publish build artifacts
    inputs:
      pathtoPublish: '$(Build.ArtifactStagingDirectory)'
      artifactName: buildeffect
      publishLocation: 'Container'


-job: packAndPushJob
  steps:
  - task: DownloadBuildArtifacts@0
    displayName: download build artifacts
    inputs:
      buildType: 'current'
      downloadType: 'single'
      artifactName: 'buildeffect'
      downloadPath: '$(Build.ArtifactStagingDirectory)'
  - task: CopyFiles@2
    displayName: copy files to source directory
    inputs:
      sourceFolder: '$(Build.ArtifactStagingDirectory)/buildeffect'
      contents: '**/*'
      TargetFolder: '$(Build.SourcesDirectory)'
      allowPackageConflicts: true
  - task: DotNetCoreCLI@2
    displayName: 'dotnet pack'
    inputs:
      arguments: '--no-restore'
      nobuild: true
      command: pack
      projects: '$(Build.SourcesDirectory)'
      publishWebProjects: true
      publishVstsFeed: $(vstsFeed)
      includeNuGetOrg: true
  - task: NuGetCommand@2
    displayName: 'nuGet push'
    inputs:
      command: push
      projects: '$(Build.SourcesDirectory)'
      publishVstsFeed: $(vstsFeed)
      allowPackageConflicts: true

我有 2 份工作。首先,在构建工件中恢复、测试、构建和共享构建文件。其次是打包和推送nuget包。第一份工作成功完成了他们的工作,但第二份工作在打包任务中失败了。 nuget包有问题,例如:

/usr/share/dotnet/sdk/3.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(234,5): error NETSDK1064: Package Microsoft.CSharp, version 4.6.0 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/home/vsts/work/1/s/src/Spotio.Leads.Client/Spotio.Leads.Client.csproj]

那么,也许我们应该以另一种方式共享已构建的项目?或者也许可以添加一些带有提要的参数来恢复?我没有任何想法,所以请,如果您有任何建议,请帮助我们:)

【问题讨论】:

  • 您在发布之前尝试过 azure 管道的“nuget 命令”吗?并确保清除恢复 nuGet 包复选框
  • 您的意思是在第一份工作中发布工件之前放置 nuget 包吗?是的,它有效,但我们需要分开工作
  • @SebastianSarnecki 尝试在包参数中添加--no-build。
  • @ShaykiAbramczyk 添加后 --no-build 仍然是相同的结果
  • 使用dotnet pack 命令时,dotnet cli 将尝试确认引用的包是否存在,尽管项目已经构建并且程序集已经输出。我假设您使用的是托管代理,为此,您需要在第二个代理作业中使用 dotnet pack 命令之前恢复包...

标签: c# .net-core azure-devops azure-pipelines .net-core-3.0


【解决方案1】:

如何在 Azure 中的多个作业之间共享构建的 .NET Core 项目 管道?

1.参见this:使用PackageReference 格式的项目始终直接使用此文件夹中的包(%userprofile%\.nuget\packages)。

2.以.net core为目标的项目使用PackageReference格式,所以恢复的包存储在first agent的%userprofile%\.nuget\packages中。

3.对于您的第二个代理工作,devops 实际上会启动另一个 托管代理来运行您的任务。这意味着对于第二个代理,它没有%userprofile%\.nuget\packages 中引用的包。

4.我们应该知道的是:虽然我们已经将bin和obj的所有文件复制到第二代理,dotnet pack xx.csproj仍然会尝试确认引用的包是否存在,那么问题就出现了。

所以我建议你可以在 second agent job 中在 dotnet pack task 之前添加一个 dotnet restore 任务,以确保可以在 second agent 中找到丢失的包.

注意:

1.在一个构建管道中有两个代理作业,虽然这两个代理作业都使用托管代理,但这两个代理不是同一个实例。

2.确保您用于构建的配置与您用于打包的配置相同。

希望对您有所帮助。如果我有什么误解,请随时告诉我:)

【讨论】:

  • 好的,非常感谢,我们之前的版本也有类似的解决方案,但是我们认为它可以在不恢复步骤的情况下更快地工作。总之,问题解决了。
  • 很高兴知道它会有所帮助:)
猜你喜欢
  • 2020-06-16
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
相关资源
最近更新 更多