Azure DevOps Pipeline dotnet ef 迁移因本地工件而失败

问题描述

我的 Azure DevOps Pipeline 需要访问存储在 Azure DevOps Artifacts 源中的一些 NuGet 包。

包源工件在 <packageSources>nuget.config 中定义:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="xxx" value="https://pkgs.dev.azure.com/xxx/_packaging/xxx/nuget/v3/index.json" />
  </packageSources>
</configuration>

由于 vstsFeed 输入,dotnet 恢复工作:

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    FeedsToUse: 'select'
    vstsFeed: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

访问 Artifacts 提要时,dotnet publish 进入 401。所以我按照 https://stackoverflow.com/a/57263923提示添加--no-restore 参数。 现在它可以工作了:

- task: DotNetCoreCLI@2
  inputs:
    command: publish
    publishWebProjects: True
    arguments: '--configuration $(BuildConfiguration) --output $(Build.ArtifactStagingDirectory) --no-restore'
    zipAfterPublish: True

但现在 dotnet ef migrations 不起作用:build Failed

YAML:

- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    custom: 'ef'
    arguments: 'migrations script --startup-project xxx/xxx.Portal/xxx.Portal.csproj --project xxx/xxx.Core/xxx.Core.csproj --output $(Build.ArtifactStagingDirectory)/sql/migrate.sql --context Dbxxx --idempotent'

我试过 --no-build ...:

- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    custom: 'ef'
    arguments: 'migrations script --no-build --startup-project xxx/xxx.Portal/xxx.Portal.csproj --project xxx/xxx.Core/xxx.Core.csproj --output $(Build.ArtifactStagingDirectory)/sql/migrate.sql --context Dbxxx --idempotent'

...但另一个错误来了:

The specified deps.json [D:\a\1\s\xxx\xxx.Portal\bin\Debug\net5.0\xxx.Portal.deps.json] does not exist

同时添加 FeedsToUsevstsFeed 输入不会改变任何内容

- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    custom: 'ef'
    arguments: 'migrations script --startup-project xxx/xxx.Portal/xxx.Portal.csproj --project xxx/xxx.Core/xxx.Core.csproj --output $(Build.ArtifactStagingDirectory)/sql/migrate.sql --context Dbxxx --idempotent'
    FeedsToUse: 'select'
    vstsFeed: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

解决方法

感谢 Levi Lu-MSFT 的评论,我已经修复了我的 YAML,在 ef migrations 之前添加了 2 个任务。

由于 Azure DevOps 不允许用户在 restorebuild 任务中的 ef migrations 操作上指定访问私有 Azure DevOps Artifacts Feed 的授权,因此正确的顺序是:

  1. restore 任务由 feedsToUservstsFeed 授权
  2. build 带有 --no-restore 参数的任务
  3. ef migrations 带有 --no-build 参数的任务

所以完整的 YAML 是:

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
    vstsFeed: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: 'xxx/xxx.Portal'
    arguments: '--no-restore'

- task: DotNetCoreCLI@2
  inputs:
    command: 'custom'
    custom: 'ef'
    arguments: 'migrations script --no-build --startup-project xxx/xxx.Portal/xxx.Portal.csproj --project xxx/xxx.Core/xxx.Core.csproj --output $(Build.ArtifactStagingDirectory)/SQL/migrate.sql --context Dbxxx --idempotent'