Search code examples
.net-coreazure-devopsazure-pipelinesazure-artifactsazure-devops-pipelines

Azure DevOps Pipeline dotnet ef migrations fails with local artifacts


My Azure DevOps Pipeline needs to access some NuGet packages stored in Azure DevOps Artifacts feed.

The package source artifact is defined in <packageSources> of 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>

The dotnet restore works because of the vstsFeed input:

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

The dotnet publish goes in 401 when access to the Artifacts feed. So I follow the hint from https://stackoverflow.com/a/57263923 and added the --no-restore parameter. Now it works:

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

But now the dotnet ef migrations do not works: 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'

I tried with --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'

... but another error come:

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

Also adding the feedsToUse and vstsFeed inputs changes nothing:

- 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'

Solution

  • Thanks to the Levi Lu-MSFT comment I've fixed my YAML adding 2 tasks before the ef migrations one.

    Because Azure DevOps do not allow user to specify the authorization to access the private Azure DevOps Artifacts Feed on the restore operation in build and ef migrations tasks, the correct sequence is:

    1. restore task with authorization by feedsToUser and vstsFeed
    2. build task with --no-restore parameter
    3. ef migrations task with --no-build parameter

    So the complete YAML is:

    - 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'