创建独立部署时如何解决 Azure Devops Pipeline 中的目标问题

How to resolve target-issues in Azure Devops Pipeline when creating Self-contained deployments

由于 Azure AppServices 不再(不再)支持具有框架相关部署的 x64 上的 .NET Core 2.1,我们目前正在发布 .NET Core 2.1 Web 的自包含 win-x64 版本 API .

我正在尝试在 Yaml 中设置 Azure Pipeline 用于 CI/CD 目的并将其部署到 Azure App Service 部署槽。

我要解决的问题是错误消息:project.assets.json' 没有 'netcoreapp2.1/win10-x64'[=13 的目标=]

/usr/bin/dotnet publish /home/vsts/work/1/s/MyApp.WebApi/MyApp.WebApi.csproj --configuration Release -f netcoreapp2.1 -r win10-x64 --self-contained true --no-restore --output /home/vsts/work/1/a/MyApp.WebApi

Microsoft (R) Build Engine version 16.8.3+39993bd9d for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/share/dotnet/sdk/5.0.102/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: Assets file '/home/vsts/work/1/s/MyApp.WebApi/obj/project.assets.json' doesn't have a target for 'netcoreapp2.1/win10-x64'. Ensure that restore has run and that you have included 'netcoreapp2.1' in the TargetFrameworks for your project. You may also need to include 'win10-x64' in your project's RuntimeIdentifiers. [/home/vsts/work/1/s/MyApp.WebApi/MyApp.WebApi.csproj]
##[error]Error: The process '/usr/bin/dotnet' failed with exit code 1

这是我的 yaml 文件:

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- develop

pool:
  vmImage: 'ubuntu-20.04'

variables:
  buildConfiguration: 'Release'
  buildPlatform: x64

steps:
- task: DotNetCoreCLI@2
  displayName: dotnet restore
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'config'
    nugetConfigPath: './NuGet.config'
    externalFeedCredentials: 'Hangfire Pro'

- task: DotNetCoreCLI@2
  displayName: dotnet publish
  inputs:
    command: 'publish'
    publishWebProjects: true
    feedsToUse: 'config'
    nugetConfigPath: './NuGet.config'
    externalFeedCredentials: 'Hangfire Pro'
    arguments: '--configuration $(BuildConfiguration) -f netcoreapp2.1 -r win10-x64 --self-contained true --no-restore --output $(build.artifactstagingdirectory)'

# this code takes all the files in $(Build.ArtifactStagingDirectory) and uploads them as an artifact of your build.
- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
    artifactName: 'MyAppWebApi'

我尝试通过添加这些来修改 CSPROJ 文件:

<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    **<RuntimeIdentifiers>win10-x64;win-x64</RuntimeIdentifiers>**

and

<PlatformTarget>x64</PlatformTarget>

请从您的项目中删除 obj 和 bin 文件夹。您的 .csproj 文件中的 <TargetFramework> 也是单数的。请尝试添加一个“s”,它变成"TargetFrameworks",看看是否有效。

  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.1</TargetFrameworks>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

您可以参考此以获得更多可能的解决方案。

顺便说一句,Microsoft-hosted Windows-2019 agents(which install Visual Studio Enterprise 2019) and your local machine, you could try to use self-hosted Windows agents 之间有不同的构建环境,无需额外的环境准备步骤即可构建您的项目。

我在工作中出了一些问题。最重要的是,我尝试进行的所有更改都被推送到 Develop-branch,而 Pipeline 在另一个分支上 运行。我对 Pipelines(以及 Git)缺乏经验是造成这种情况的主要原因。我的印象是 YAML 中的 trigger 行指向要恢复和发布的存储库。

我终于通过以下步骤完成了它的工作。就像 Edward Han 提到的,我尝试在我的本地机器上通过命令行恢复和发布。这些也给了我一些我需要修复的错误。就像将此行添加到所有 (30+) 个引用的 csproj 文件中:

<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>

然后将这个配置添加到webproject csproj:

<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>

那么我最终的 YAML 文件是这样的:

trigger:
- develop

pool:
  vmImage: 'windows-2019'

variables:
  buildConfiguration: 'Release'
  buildPlatform: x64

steps:
- task: UseDotNet@2
  displayName: 'Use .Net Core sdk 2.1.x'
  inputs:
    version: 2.1.x

- task: DotNetCoreCLI@2
  displayName: dotnet restore
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    arguments: '-r win-x64'
    feedsToUse: 'config'
    nugetConfigPath: './NuGet.config'
    externalFeedCredentials: 'Hangfire Pro'

- task: DotNetCoreCLI@2
  displayName: dotnet test
  inputs:
   command: 'test'
   projects: '**/*[Tt]ests/*.csproj'
   arguments: '--configuration $(BuildConfiguration)'
   testRunTitle: 'dotnet test 2'

- task: DotNetCoreCLI@2
  displayName: dotnet publish
  inputs:
    command: 'publish'
    publishWebProjects: true
    arguments: '--configuration $(BuildConfiguration) -r win-x64 --self-contained true --no-restore --output $(build.artifactstagingdirectory)'

# this code takes all the files in $(Build.ArtifactStagingDirectory) and uploads them as an artifact of your build.
- task: PublishBuildArtifacts@1
  inputs:
    pathtoPublish: '$(Build.ArtifactStagingDirectory)' 
    artifactName: 'MyAppWebApi'

一些重要的事情我 changed/added:

  • 明确告知使用 .NET Core 2.1。*
  • 将 -runtime 参数添加到 RESTORE 命令

结论

虽然我学习了一些 Pluralsight 课程、YouTube 视频和博客,但我还是最终阅读了 Microsoft Docs/MSDN。习惯于使用 Visual Studio 用户界面发布,切换到命令行确实是一个很大的不同。需要研究更多的依赖关系。提示:请认真阅读这些错误消息提供的网址,它们确实包含足够的信息来解决您的问题。至少,那是我学到的。