问题
In Azure Devops when your Maven build task fails it will display an an error like below in the build summary:
Maven build error
Now if you click in and scroll up you will be able to find the error next to the reactor summary. This is easy enough to do at home with a mouse and a big screen but for some of our devs on trains or in cafes it can be quite an effort for them to find the error. I want to be able to make the error easier to find. Some suggestions I've thought of but not sure how to implement are below. Do you have any other good ideas?
- Reduce output of maven logs - I've tried the -q flag but it doesn't seem to work for this task?
- Use rest api somehow to find the error and then send it to a slack channel with a webhook - Not sure how to find the log or error with the rest api though
回答1:
So I can help with how you might grab the error out of the build logs and send it over to slack.
Assuming we are going to run this task as the next task after the build error.
- We can get the build logs via the API. Make sure to add the
system_access token
so you can auth. - Once we have all the logs grab the url out of the response to download the last log
- Download the last log
- Parse it and find the
[[error]]
lines - Marshal the message to slack \ or do whatever else you want with it
YAML
- pwsh: |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $env:System_AccessToken)))
$uriLogs = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$env:BUILD_BUILDID/logs?api-version=5.1"
$response = Invoke-RestMethod -Uri $uriLogs -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$logUrl =$response.value[-1].url
$webContent = Invoke-WebRequest -Uri $logUrl -Method 'GET' -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$ErrorMessage = $webContent.Content.tostring() -split "[`r`n]" | select-string "[[Error]]"
Write-Output "Error lines found " $ErrorMessage
$postSlackMessage = @{token=$env:SLACK_TOKEN;channel="@user,#channel-name";text=$ErrorMessage}
Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage
env:
system_accesstoken: $(System.AccessToken)
condition: failed()
Other alternatives:
- parse the log, find the error, and then send a link to the failure directly at that line number. There is a well known format for direct links to each line.
- send a direct link to download just the error part of the log. See
startLine
andendLine
parameters here.
来源:https://stackoverflow.com/questions/60004185/easily-catch-error-from-maven-install-task-in-build-pipeline-in-azure-devops