Search code examples
azure-pipelinessudoazure-pipelines-tasksdotnetcorecli

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


I have the following azure pipeline task snippet below, running on Linux self hosted agents.

  - 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

What is the way to run this with sudo permissions ? Part of my tests require the execution of some external processes with sudo permissions (like dmidecode, etc).

The only way I can think of is giving up using the DotNetCoreCLI task and using a regular CmdLine or other bash script task instead (actually this is the approach I was using before, I just happened to solve an issue of not having log output being printed to the console output in a Windows host - by switching from running a dotnet test within a script block to a DotNetCoreCLI task - and thought this would be preferable as well in my linux self hosted agents).

The user that runs the pipeline job is already configured under /etc/sudoers (this was required in order to succeed running sudo dotnet test as I originally did, as mentioned above).


Solution

  • So the obvious answer is yes, run a 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 task.

    But the main reason I wanted to use DotNetCoreCLI, is that it automatically publishes the test results and attaches it to the job run.

    I ultimately found a workaround for that:

    1. Run your dotnet commands via CmdLine task, prefixed with sudo, as desired
    2. Add a new task - Publish Test Results, right after.

    The same test results job attachment behavior is achieved.