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

Azure DevOps Pipeline dotnet ef migrations fails with local artifacts

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

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

<?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。所以我按照 的提示添加了 --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 ...:[=​​29=]

- 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\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 不允许用户在 buildef migrations 任务中的 restore 操作中指定访问私有 Azure DevOps Artifacts Feed 的授权,正确的顺序是:

  1. restore 任务由 feedsToUservstsFeed
  2. 授权
  3. build 任务 --no-restore 参数
  4. 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'