如何在 Linux 上 运行 具有权限的 Azure Pipeline DotNetCoreCLI 任务?

How to run an Azure Pipeline DotNetCoreCLI task on Linux with permissions?

我在下面有以下 Azure 管道任务片段,运行在 Linux 自托管代理上。

  - task: DotNetCoreCLI@2
      displayName: Run Dotnet Test
      inputs:
        command: test
        #arguments: --blame-hang-timeout 2min (supported only in .net 5 and above)
        projects: 'Test/UnitTests/UnitTests.csproj'
        verbosityPack: detailed

如何使用 sudo 权限运行?我的部分测试需要执行一些具有 sudo 权限的外部进程(如 dmidecode 等)。

我能想到的唯一方法是放弃使用 DotNetCoreCLI 任务,转而使用常规 CmdLine 或其他 bash 脚本任务(实际上这是我之前使用的方法,我只是碰巧解决了没有将日志输出打印到 Windows 主机中的控制台输出的问题 - 通过从脚本块中的 运行ning a dotnet test 切换到 DotNetCoreCLI 任务 - 并认为这会在我的 linux 自托管代理中也更可取)。

运行 管道作业的用户已经在 /etc/sudoers 下配置(这是为了像我最初那样 运行ning sudo dotnet test 成功所必需的,如上所述)。

所以显而易见的答案是肯定的,运行 一项 CmdLine task and manually run your dotnet commands there, including dotnet test - and - prefixing them with sudo. As stated above, this is what I have been doing, I just thought that it would be better to use the DotNetCoreCLI 任务。

但我想使用 DotNetCoreCLI 的主要原因是它会自动发布测试结果并将其附加到作业 运行。

我最终找到了解决方法:

  1. 运行 您的 dotnet 命令通过 CmdLine 任务,根据需要以 sudo 为前缀
  2. 添加新任务 - Publish Test Results,紧随其后。

实现了相同的测试结果工作依恋行为。