0
votes

I have a build pipeline, where I have a Nuget restore step using NuGetCommand which works fine.

But the next step where the build is performed fails on missing nuget packages.

BuildOverview

It seems the build step tries to restore the nuget packages a second time, which does not work (It doesn't have the credentials to do so)

BuildLog

The yaml file for the build definition is as follows:

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:

- task: CopyFiles@2
  displayName: 'copy sql scripts'
  inputs:
    SourceFolder: 'Resources/TestProject/Migrations/Sql'
    Contents: '**'
    TargetFolder: '$(build.artifactstagingdirectory)/SqlScripts'

- task: NuGetCommand@2
  displayName: 'NuGet Restore'
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: './nuGet.config'
    externalFeedCredentials: 'TelerikFeed'

- task: DotNetCoreCLI@2
  displayName: 'Build ServiceHost projects'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: '**/ServiceHosts/**/*.csproj'
    arguments: '-c Release -r win-x64 --self-contained false -o $(Build.ArtifactStagingDirectory) /p:SourceRevisionId=$(Build.SourceVersion)'
    zipAfterPublish: false

- task: PublishBuildArtifacts@1
  displayName: 'Publish artifacts'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

I also have a nuGet.config file which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!-- remove any machine-wide sources with <clear/> -->
    <clear />    
    <add key="MyFeed" value="https://pkgs.dev.azure.com/MyCompany/_packaging/NugetFeed/nuget/v3/index.json" />    
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Telerik_NuGet" value="https://nuget.telerik.com/nuget" />
  </packageSources>
</configuration>

I don't understand why the build step needs to restore the nuget packages again.

Is there a way to point the DotNetCoreCli to the already restored packages?

Edit:

As suggested by @Ibrahim I added the --no-restore to the DotNetCoreCLI task. I then received the following error message:

C:\Program Files\dotnet\sdk\5.0.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: Assets file 'D:\a\1\s\ServiceHosts\TestProject\obj\project.assets.json' doesn't have a target for 'netcoreapp3.1/win-x64'. Ensure that restore has run and that you have included 'netcoreapp3.1' in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers.

This was mitigated by adding a task to the yaml file to install the newest version of nuget using

- task: NuGetToolInstaller@1
  inputs:
    versionSpec: 5.9.1

And adding RuntimeIdentifier win-x64 to the csproj files.

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AssemblyName>TestProject</AssemblyName>
    <RootNamespace>TestProject</RootNamespace>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>
1
That's normal behavior. build and publish both do a restore. You can tell it to not do a restore by passing the appropriate command line argument, --no-restore.Daniel Mann

1 Answers

1
votes

From the dotnet documentation

You don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack. To disable implicit restore, use the --no-restore option.

The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.

You should either use --no-restore in dotnet publish or remove the dotnet restore and let the packages restored implicitly by dotnet publish