Easily catch error from maven install task in build pipeline in Azure Devops? - maven

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

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 and endLine parameters here.

I added a line to top of powershell script to solve the error
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
If you don't have any problem about tls you don't need to add this line.
Powershell:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$uriLogs = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$env:BUILD_BUILDID/logs?api-version=5.0"
$response = Invoke-RestMethod -Uri $uriLogs -Method Get -Headers #{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$logUrl = $response.value[-1].url
$webContent = Invoke-WebRequest -Uri $logUrl -Method GET -Headers #{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$ErrorMessage = $webContent.Content.tostring() -split "[`r`n]" | select-string "[[error]]"
Write-Output "Error lines found " $ErrorMessage

Related

The Win32 internal error "A device attached to the system is not functioning" 0x1F occurred while setting 2 the console window title

I have a PowerShell script that get a response from an API:
$test = Invoke-RestMethod -Uri $url -Method Get -ContentType application/json -UseDefaultCredentials
Then, iterate the response with foreach:
$test.value | foreach {
# Do something
}
In my PC it works great, but I want it will run automatically so I created a TFS build definition and it runs in our build servers.
During the build I get the following error in the middle of the foreach:
foreach : The Win32 internal error "A device attached to the system is not functioning" 0x1F occurred while setting
the console window title. Contact Microsoft Customer Support Services.
At D:\AGT\Test\01\_temp\test.ps1:9 char:17
+ $builds.value | foreach {
+ ~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [ForEach-Object], HostException
+ FullyQualifiedErrorId : SetConsoleWindowTitle,Microsoft.PowerShell.Commands.ForEachObjectCommand
If I have log-in to the server and run the script, it works. only during the TFS build it doesn't work.
I googled it a lot but couldn't solve it. any idea?

Get Azure VM size inside on a windows machine

I do only have access on a Windows VM which is hosted on Azure.
Is there a way to find out the VM size inside of the virtual machine, lets say for example over commandline (powershell / cmd), without having further information about the envrionment like for example Ressource Group or VM Ressource Name?
I did find a way like this, but it requires the information about the name of VM and Ressource group:
$VM = Get-AzureRMVM –Name HSG-Linux1 –ResourceGroupName HSG-AzureRG
$VM.HardwareProfile
Source
You can use the metada to get the VM information inside the VM like this:
Invoke-RestMethod -Headers #{"Metadata"="true"} -URI http://169.254.169.254/metadata/instance/compute?api-version=2019-03-11 -Method get
For more details, see Azure Instance Metadata service. You can also put the output as a variable, then get the special properties. For example:
$vm = Invoke-RestMethod -Headers #{"Metadata"="true"} -URI http://169.254.169.254/metadata/instance/compute?api-version=2019-03-11 -Method get
$vm.vmSize
Or get the property directly through the request like this:
$vmSize = Invoke-RestMethod -Headers #{"Metadata"="true"} -URI "http://169.254.169.254/metadata/instance/compute/vmSize?api-version=2019-03-11&&format=text" -Method get

Ansible Tower REST API: Is there any way to get the logs/output of a job?

I have a Ansible job started by another Process. Now I need to check the status of the current running job in Ansible Tower.
I am able to track the status whether it is running/success/failed/canceled with /jobs/{id} using REST API.
But I would also need the information of the console logs/ouputs of the task for processing as well. Is there any direct API call for the same?
You can access the job log via a link similar to:
https://tower.yourcompany.com/api/v1/jobs/12345/stdout?format=txt_download
Your curl command would be similar to:
curl -O -k -J -L -u ${username):${password} https://tower.company.com/api/v1/jobs/${jobnumber}/stdout?format=txt_download
obviously replacing ${username}, ${password}, and ${jobnumber} with your own values
The curl flags used:
-O : output the filename that is actually downloaded
-k : insecure SSL (don't require trusted CAs)
-J : content header for file download https://curl.haxx.se/docs/manpage.html#-J
-L : follow redirects
-u : username and password
You can do this via their restful call.
To get the job number use a GET against https://yourtowerinstance/api/v2/job_templates/
this will return your templates, and their IDs
To get the output in real time I use this powershell code
$stdouturl = "https://yourtowerinstance/api/v2/jobs/$($templateResult.id)/stdout/?format=txt"
$resultstd = Invoke-Restmethod -uri $stdouturl -Method 'Get' -Headers $authHeader
while ($resultstd -notmatch 'PLAY RECAP') {
$resultstd = Invoke-Restmethod -uri $stdouturl -Method 'Get' -Headers $authHeader
start-sleep -s 5
}
$resultstd
Once you launch a template you get the job-id in response but I don't think there is a API to get the output of the job. However from the dashboard under jobs section you can download the individual job output.

PowerShell: Invoke-Parallel throwing error with "start-process"

I'm using the Invoke-Parallel CmdLet that can be found at the link here. When I have some dummy code in my script block, it works correctly, but when I add a start-process... command it fails with an error saying
Get-RunspaceData : This command cannot be run due to the error: The system
cannot find the file specified.
At c:\crm\Interfaces\Batches\Invoke-Parallel.ps1:592 char:13
My script block looks like so. Long story short, I'm feeding file names into a block, and I am telling a 3rd party application to use that file as an input for a process. When the start-process line is removed, it works, with it, it fails.
$childFiles| Invoke-Parallel -ImportVariables {
$importformat = '-f ' + '"CU Imp P19 FI"'
$importfile = '-d ' + $_ + " " + $user + " " + $pass
Write-Host $importformat + " " + $_
start-process .\mmim.exe -ArgumentList $user, $pass, '-l:1',
$importformat, $importfile -Wait -NoNewWindow
return "blah"
}
Does anyone have any idea of what might be going on? My PowerShell version is the following
Major:5
Minor:0
Build: 10586
Revision: 117
Any help is greatly appreciated.
Each PowerShell runspace have its own current location. If Invoke-Parallel does not change current location of spawned runspaces to match current location of main runspace, then relative path .\mmim.exe may be resolved to entire different executable or not resolved at all and produce given error.

Invoke-RestMethod : Content-Length or Chunked Encoding cannot be set for an operation that does not write data

I am looking for cURL equivalent command in powershell and then found the below mentioned URL :
https://superuser.com/questions/344927/powershell-equivalent-of-curl
Based on the above URL I tried the below mentioned powershell script in a system which has Powershell version 4.0
Invoke-RestMethod -Uri www.discoposse.com/index.php/feed -Method Get -OutFile C:\Temp\DiscoPosseFeed.xml
Once I run the above command I see a xml file in the specified location but if now I specify the encoding as mentioned below:
Invoke-RestMethod -Uri www.discoposse.com/index.php/feed -TransferEncoding compress -Method Get -OutFile C:\Temp\DiscoPosseFeed.xml
I am getting an error :
Can anyone help me to know is there anything I am missing here?
The explanation is tha your command line does not write anything on the line. TransferEncoding specifies a value for the transfer-encoding HTTP response header. Valid values are Chunked, Compress, Deflate, GZip and Identity.

Resources