Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. # This is a hacky workaround to (at least) get some analyzer warnings in the Azure Devops build summary / build log for .NET Core projects
  2. # Be sure to change the DotNetCoreInstaller version to the needed version
  3. # Explanation:
  4. # 'dotnet build --no-incremental /flp:v=q /flp:logfile=MyLog.log'
  5. # no-incremental => makes sure that the solution is being rebuild instead of just build.
  6. # /flp:v=q => sets the verbosity to quiet (= errors + warnings) for the logging. The errors + warnings is what we need for the powershell task.
  7. # /flp:logfile=MyLog.log => the name of the logfile, if not specified the default value of msbuild.log is used.
  8.  
  9. # The display build issues task gets the content of the build log file (which only contain the needed errors + warnings)
  10. # Then a lot of magical regex is done which results in some fancy output in the build summary.
  11. # The build Log tab still has an open issue about only showing a max of 10 warnings, however in the Summary tab all the warnings are shown.
  12.  
  13. trigger: none
  14.  
  15. pool:
  16. vmImage: 'windows-latest'
  17.  
  18. variables:
  19. solution: '**/*.sln'
  20. buildPlatform: 'Any CPU'
  21. buildConfiguration: 'Release'
  22.  
  23. steps:
  24. - task: DotNetCoreInstaller@0
  25. displayName: 'Use .NET Core sdk 2.2.102'
  26. inputs:
  27. version: 2.2.102
  28.  
  29. - task: DotNetCoreCLI@2
  30. displayName: 'Dotnet build solution'
  31. inputs:
  32. command: build
  33. arguments: '--no-incremental /flp:v=q /flp:logfile=MyLog.log'
  34. projects: '$(solution)'
  35.  
  36. - task: PowerShell@2
  37. displayName: 'Display build issues'
  38. condition: succeededOrFailed()
  39. inputs:
  40. targetType: 'inline'
  41. script: '
  42. $output = Get-Content -Path "$(System.DefaultWorkingDirectory)/MyLog.log";
  43. $warnings = $output | Select-String -Pattern ".*:\s";
  44. $hasErrors = $false;
  45.  
  46. [regex]$issueTypeRegex = "(warning|error)";
  47. [regex]$issueLocationRegex = "(\d+,\d+)";
  48. [regex]$sourcePathRegex = "^[^/(]*";
  49. [regex]$issueCodeRegex = "(?<=(warning|error) )[A-Za-z0-9]+";
  50. [regex]$messageRegex = "(?<=((warning|error) [A-Za-z0-9]+: )).*";
  51.  
  52. $warnings | Foreach-Object {
  53. $issueLocationMatch = $issueLocationRegex.Matches($_)[0];
  54. $issueLocation = $issueLocationMatch.value.split(",");
  55. $issueType = $issueTypeRegex.Matches($_)[0];
  56. $sourcepath = $sourcePathRegex.Matches($_)[0];
  57. $linenumber = $issueLocation[0];
  58. $columnnumber = $issueLocation[1];
  59. $issueCode = $issueCodeRegex.Matches($_)[0];
  60. $message = $messageRegex.Matches($_)[0];
  61.  
  62. Write-Host "##vso[task.logissue type=$issueType;sourcepath=$sourcepath;linenumber=$linenumber;columnnumber=$columnnumber;code=$issueCode;]$message";
  63. if($issueType.Value -eq "error") { $hasErrors = $true; }
  64. };
  65. if($warnings.Count -gt 0 -and $hasErrors -eq $true) { Write-Host "##vso[task.complete result=Failed;]There are build errors"; }
  66. elseif($warnings.Count -gt 0 -and $hasErrors -eq $false) { Write-Host "##vso[task.complete result=SucceededWithIssues;]There are build warnings"; } '
  67.  
  68. - task: DotNetCoreCLI@2
  69. displayName: "Dotnet test solution"
  70. inputs:
  71. command: 'test'
  72. projects: '$(solution)'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement