microsoft / azure-pipelines-tasks

Tasks for Azure Pipelines
https://aka.ms/tfbuild
MIT License
3.47k stars 2.6k forks source link

[DotNetCoreCLIV2] dotnet tool restore should be a built-in command #16279

Closed hangy closed 11 months ago

hangy commented 2 years ago

Question, Bug, or Feature?
Type: Feature

Enter Task Name: DotNetCoreCLI@2

Environment

Issue Description

When using a tool manifest (.config\dotnet-tools.json in the current directory), you can restore tools using dotnet tool restore. Unfortunately, the DotNetCoreCLI task requires you to execute it as a custom command, and doesn't apply any feed settings.

          - task: DotNetCoreCLI@2
            displayName: Restore dotnet tools
            inputs:
              command: custom
              custom: tool
              arguments: restore

https://github.com/microsoft/azure-pipelines-tasks/blob/1fe93c42be2b82105c5b2e1c236dae1cbfe2ed9b/Tasks/DotNetCoreCLIV2/dotnetcore.ts#L47-L50

Thus, unlike the restore command, it doesn't use the agent's current accessToken to access any private NuGet feed. (restore picutred below for reference): https://github.com/microsoft/azure-pipelines-tasks/blob/acc64cc7292c98597908325e53af9f898a896189/Tasks/DotNetCoreCLIV2/restorecommand.ts#L58-L61

Consequently, if any private feed, which the agent has access to, is referenced in NuGet.config, then dotnet tool restore tends to fail, because it cannot access that feed.

Task logs

##[debug]custom=tool
##[debug]which 'dotnet'
##[debug]found: 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe'
Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
##[debug]which 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe'
##[debug]found: 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe'
##[debug]d:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe arg: tool
##[debug]d:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe arg: restore
##[debug]exec tool: d:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe
##[debug]arguments:
##[debug]   tool
##[debug]   restore
d:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe tool restore
d:\vsts-agent-3\_work\_tool\dotnet\sdk\6.0.202\NuGet.targets(130,5): warning : Der Anbieter für Plug-In-Anmeldeinformationen konnte keine Anmeldeinformationen abrufen. Die Authentifizierung erfordert möglicherweise ein manuelles Eingreifen. Erwägen Sie eine erneute Ausführung des Befehls mit "--interactive" für 'dotnet', /p:NuGetInteractive="true" für MSBuild, oder entfernen Sie die Option "-NonInteractive" für "NuGet". [C:\Users\user\AppData\Local\Temp\a34romvl.pjl\restore.csproj]
d:\vsts-agent-3\_work\_tool\dotnet\sdk\6.0.202\NuGet.targets(130,5): error : Der Dienstindex für die Quelle "https://tfs.in.domain.de/tfs/MyProjectCollection/_packaging/Feed%40Release/nuget/v3/index.json" konnte nicht geladen werden. [C:\Users\user\AppData\Local\Temp\a34romvl.pjl\restore.csproj]
d:\vsts-agent-3\_work\_tool\dotnet\sdk\6.0.202\NuGet.targets(130,5): error :   Response status code does not indicate success: 403 (Forbidden - Der Benutzer "domain\user" verfügt nicht über die Berechtigung zum Ausführen dieser Aktion. Sie müssen "ReadPackages" besitzen. (DevOps-Aktivitäts-ID: AD18E89D-8D48-4A6B-833F-CB5B8B919FCD)). [C:\Users\user\AppData\Local\Temp\a34romvl.pjl\restore.csproj]

Das Paket "dotnet-ef" konnte nicht wiederhergestellt werden. Ursache: Microsoft.DotNet.ToolPackage.ToolPackageException: Das Toolpaket konnte nicht wiederhergestellt werden.
   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)

Fehler bei der Wiederherstellung.
##[debug]Exit code 1 received from tool 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe'
##[debug]STDIO streams have closed for tool 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe'
##[error]Error: The process 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe' failed with exit code 1
##[debug]Processed: ##vso[task.issue type=error;]Error: The process 'd:\vsts-agent-3\_work\_tool\dotnet\dotnet.exe' failed with exit code 1
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://docs.microsoft.com/en-us/dotnet/core/tools/ and https://docs.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
##[debug]task result: Failed
##[error]Fehler beim dotnet-Befehl mit Exitcode ungleich 0 bei den folgenden Projekten: .
##[debug]Processed: ##vso[task.issue type=error;]Fehler beim dotnet-Befehl mit Exitcode ungleich 0 bei den folgenden Projekten: .
##[debug]Processed: ##vso[task.complete result=Failed;]Fehler beim dotnet-Befehl mit Exitcode ungleich 0 bei den folgenden Projekten: .
Abschließen: Restore dotnet tools

Note that the user does not need the permission (or access level), because dotnet tool restore could just use the agent's accessToken.

Additional Information

The custom command does not respect the selectOrConfig or includeNuGetOrg task parameters, so that it is not possible to simply exclude the private feed (Azure Artifacts hosted on the same instance of Azure DevOps Server, btw) by setting task parameters. There are probably ways to work around this, if you don't need tools from Azure Artifacts, but even then, the experience is not as expected.

I believe, the dotnet tool restore should be built-in and should respect the same feed settings as dotnet restore does.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 180 days with no activity. Remove the stale label or comment on the issue otherwise this will be closed in 5 days

hangy commented 1 year ago

This issue is stale because it has been open for 180 days with no activity. Remove the stale label or comment on the issue otherwise this will be closed in 5 days

Not done, yet.

garcipat commented 1 year ago

We discovered the same problem. We cannot get this to run. Is there any attention on this issue? We would like to restore our own dotnet tool from the internal feed using the authentication.

eskaufel commented 1 year ago

Same

mburumaxwell commented 1 year ago

Current workaround for this is to add a nuget.config file to the repository root:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear /> <!-- ensure only the sources defined below are used -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="contoso" value="https://pkgs.dev.azure.com/contoso/_packaging/my-feed/nuget/v3/index.json" />
  </packageSources>
</configuration>

The alter the pipeline to include a NuGetAuthenticate@1 task before restoration:

steps:
- task: UseDotNet@2
  displayName: 'Install .NET SDK'
  inputs:
    packageType: 'sdk'
    version: '7.x'

- task: NuGetAuthenticate@1
  displayName: 'NuGet Authentication'

- script: 'dotnet tool restore --interactive'
  displayName: dotnet tool restore

- task: DotNetCoreCLI@2
  displayName: 'dotnet publish'
  inputs:
    command: publish
    publishWebProjects: false # defaults to true
    zipAfterPublish: false # defaults to true
    projects: '**/*.csproj'

All tasks/operation requiring authentication to the private feed work after adding NuGetAuthentication@1 meaning you might

github-actions[bot] commented 12 months ago

This issue is stale because it has been open for 180 days with no activity. Remove the stale label or comment on the issue otherwise this will be closed in 5 days