1

I'm using Azure DevOps and would like to run dotnet tool restore --interactive in my Build Pipeline. In my code repo, there is nuget.config as follows. The file has a private feed hosted in Azure Artifact in addition to nuget.org.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="MyTools" value="https://mytools.pkgs.visualstudio.com/MyBot/_packaging/MyTools/nuget/v3/index.json" />
  </packageSources>
</configuration>

In the private feed in Azure Artifact, my private dotnet tools exist. To retrieve the tool I created following yml file.

    pool:
      vmImage: "windows-latest"
      demands:
        - msbuild
        - visualstudio
        - vstest
    variables:
      buildPlatform: "Any CPU"
      buildConfiguration: "Release"

    steps:
      - task: PowerShell@2
        inputs:
          targetType: inline
          script: "Invoke-WebRequest -Uri https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1 -OutFile ./installcredprovider.ps1 ;./installcredprovider.ps1;"
      - task: DotNetCoreCLI@2
        displayName: "Restore tools"
        inputs:
          command: custom
          custom: tool
          arguments: restore --interactive

However, as I run the yml file in the build process, following result appears. As the message shows, it requires sign-in, but I do not want to do device sign-in every time. So Is there any good way so that I do not have to do device log-in every time?

##[section]Starting: Restore tools
==============================================================================
Task         : .NET Core
Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version      : 2.162.0
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
[command]C:\windows\system32\chcp.com 65001
Active code page: 65001
[command]"C:\Program Files\dotnet\dotnet.exe" tool restore --interactive

Welcome to .NET Core 3.1!
---------------------
SDK Version: 3.1.100

Telemetry
---------
The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Find out what's new: https://aka.ms/dotnet-whats-new
Learn about the installed HTTPS developer cert: https://aka.ms/aspnet-core-https
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs
Write your first app: https://aka.ms/first-net-core-app
--------------------------------------------------------------------------------------
      [CredentialProvider]DeviceFlow: https://mytools.pkgs.visualstudio.com/MyBot/_packaging/MyTools/nuget/v3/index.json
      [CredentialProvider]ATTENTION: User interaction required. 

      **********************************************************************

      To sign in, use a web browser to open the page https://microsoft.com/devicelogin to authenticate.

      **********************************************************************

C:\Program Files\dotnet\sdk\3.1.100\NuGet.targets(123,5): error :     [CredentialProvider]Device flow authentication failed. User was presented with device flow, but didn't react within 90 seconds. [C:\Users\VssAdministrator\AppData\Local\Temp\wk4fkuq1.hqu\restore.csproj]
C:\Program Files\dotnet\sdk\3.1.100\NuGet.targets(123,5): error : Unable to load the service index for source https://mytools.pkgs.visualstudio.com/MyBot/_packaging/MyTools/nuget/v3/index.json. [C:\Users\VssAdministrator\AppData\Local\Temp\wk4fkuq1.hqu\restore.csproj]
C:\Program Files\dotnet\sdk\3.1.100\NuGet.targets(123,5): error :   Response status code does not indicate success: 401 (Unauthorized). [C:\Users\VssAdministrator\AppData\Local\Temp\wk4fkuq1.hqu\restore.csproj]
  Restore completed in 775.06 ms.


Tool 'dotnet-ef' (version '3.1.1') was restored. Available commands: dotnet-ef
Package "localenv" failed to restore, due to Microsoft.DotNet.ToolPackage.ToolPackageException: The tool package could not be restored.
   at Microsoft.DotNet.Tools.Tool.Install.ProjectRestorer.Restore(FilePath project, PackageLocation packageLocation, String verbosity)
   at Microsoft.DotNet.ToolPackage.ToolPackageInstaller.InstallPackageToExternalManagedLocation(PackageLocation packageLocation, PackageId packageId, VersionRange versionRange, String targetFramework, String verbosity)
   at Microsoft.DotNet.Tools.Tool.Restore.ToolRestoreCommand.InstallPackages(ToolManifestPackage package, Nullable`1 configFile)

Restore partially failed.
##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1
Info: Azure Pipelines hosted agents have been updated to contain .Net Core 3.x SDK/Runtime along with 2.2 & 2.1. Unless you have locked down a SDK version for your project(s), 3.x SDK might be picked up which might have breaking behavior as compared to previous versions. 
Some commonly encountered changes are: 
If you're using `Publish` command with -o or --Output argument, you will see that the output folder is now being created at root directory rather than Project File's directory. To learn about more such changes and troubleshoot, refer here: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
##[error]Dotnet command failed with non-zero exit code on the following projects : 
##[section]Finishing: Restore tools! (Including efcore tools)
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
shumach5
  • 551
  • 5
  • 19
  • Is the feed is exist i the same organization you run the pipeline? – Shayki Abramczyk Jan 19 '20 at 14:58
  • Why do you need `--interactive`? If you use that to pass user credentials to the private feed, then you chose the wrong way as when the pipeline is executed you won't have a chance to input things like password. Instead, you might use a personal access token, https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page – Lex Li Jan 19 '20 at 19:49
  • Thank you. I added "--interactive" because what following document said. https://github.com/microsoft/artifacts-credprovider#dotnet-1 If it is not needed, I will remove it. Another thing, if the personal access token is published, should I put the personal access token in nuget.config? Since my nuget.config and yml are already in git repo, I do not want to commit the token info. Is there any way to pass token info for the sake of build pipeline? – shumach5 Jan 19 '20 at 22:16

3 Answers3

4

Fail to execute “dotnet tool restore --interactive” in Build Pipeline when using Azure DevOps Artifact

You could not use the command line dotnet tool restore --interactive on the hosted agent, that because:

Microsoft decided not to put the plugin artifacts-credprovider in dotnet.exe because there were concerns that shipping auth integration to Azure DevOps within the sdk would've been unfair to other feed providers. To resolve your 401 issues you need to install the auth plugin manually.

That the reason why you get the 401 (Unauthorized) error.

In addition, you mentioned that you do not want to commit the token info in the nuget.config file, as a solution for this issue, you could add a nuget Service connections with PAT:

enter image description here

Then use this externalFeedCredentials: nugettest in the dotnet restore task instead of powershell task (there is no option to accept the certification Information):

- task: DotNetCoreCLI@2
  displayName: Restore
  inputs:
    command: restore
    projects: path/to/xxx.csproj
    feedsToUse: config
    nugetConfigPath: path/to/NuGet.Config
    externalFeedCredentials: nugettest

Then we could resoter the package from our private feed.

If you get the 403 403 (Forbidden) error with above method, you can check the thread here.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
0

You can't use the flag --interactive in a build pipeline, because this flag except to get an output from the user during the execution.

You can use the default restore option in the .Net core task:

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '$(Build.SourcesDirecotry)/path/to/csproj'
    feedToUse: 'config'
    nugetConfigPath: '$(Build.SourcesDirectory)/path/to/nuget.config'
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 1
    The yml setting worded in my environment, but what I want to do is "dotnet tool restore", and some of my dotnet tools exist in Azure Artifact (private feed). In that situation, could you tell me appropriate yml setting? Although I removed, "--interactive", but still pipeline shows 401 error.... – shumach5 Jan 19 '20 at 22:07
0

This one is because you should let agent know the nuget sources with pre-authenticated feed url prior to dotnet restore:

- task: NuGetCommand@2
  displayName: 'Add Nuget Feed'
  inputs:
  command: custom
  arguments: 'sources add -name <name_of_source> -source <feed_url>'

I hope this would be useful.

hemantsharma
  • 537
  • 1
  • 5
  • 12