azureasp.net-core.net-coreazure-devopsazure-pipelines

Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core


Recently upgraded my application to .NET 8.0.

Getting below error in Azure pipeline.

##[error]Error: The process '/opt/hostedtoolcache/dotnet/dotnet' failed with exit code 2
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which is currently lts. Unless you have locked down an SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. 

##[error]Dotnet command failed with non-zero exit code on the following projects : [ '' ]

I have already added task to YAML for .NET SDK 8.0.302 and .NET Core runtime 8.0.6 which are successful and after that encountering this error. What is going wrong in pipeline?

YAML :

steps:
          - task: UseDotNet@2
            displayName: 'Install .NET Core runtime'
            inputs:
              packageType: "runtime"
              version: "8.0.6"
          - task: UseDotNet@2
            displayName: "Using DotNet@2"
            inputs:
              packageType: "sdk"
              version: "8.0.302"
          - task: DotNetCoreCLI@2
            displayName: "Install dotnet format"
            inputs:
              command: "custom"
              custom: "tool"
              arguments: "update -g dotnet-format"
          - task: DotNetCoreCLI@2
            displayName: "Lint dotnet"
            inputs:
              command: "custom"
              custom: "format"
              arguments: "--verify-no-changes --verbosity diagnostic"
              workingDirectory: backend

Detailed error message is shown below : Azure Pipeline Error


Solution

  • I can reproduce the same issue when using the dotnet format command.

    enter image description here

    Refer to this doc about dotnet format: dotnet format

    --verify-no-changes

    Verifies that no formatting changes would be performed. Terminates with a non zero exit code if any files would have been formatted.

    This is an expected behavior when using dotnet format command. When the command do any changes to the files, it will return a non zero exit code. But the Pipeline will read the exit code as error and fail the task.

    To ignore this expected exit code, you can change to use PowerShell task to run the dotnet format command. Then you can set the ignoreLASTEXITCODE to true to ignore the non zero exit code.

    For example:

    steps:
    
    - task: UseDotNet@2
      displayName: 'Install .NET Core runtime'
      inputs:
        packageType: "runtime"
        version: "8.0.6"
    - task: UseDotNet@2
      displayName: "Using DotNet@2"
      inputs:
        packageType: "sdk"
        version: "8.0.302"
    - task: DotNetCoreCLI@2
      displayName: "Install dotnet format"
      inputs:
        command: "custom"
        custom: "tool"
        arguments: "update -g dotnet-format"
    
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: 'dotnet format --verify-no-changes --verbosity diagnostic'
        ignoreLASTEXITCODE: true
        pwsh: true 
        workingDirectory: backend
    

    Result:

    enter image description here