简体   繁体   中英

How To Correctly Use Jenkins PowerShell step to call Web API using Invoke-WebRequest or Invoke-RestMethod

I am using Jenkins , in a PowerShell command step, to call a Web API service.I am calling the service using Invoke-WebRequest .

This service call has to be in Jenkins, because I need to do it only if other Jenkins jobs have completed successfully.

The service can run for several hours. If I don't have the TimeoutSec parameter, then the step completes with a timeout, before the Web API has completed. If I have the parameter with a large value, such as 36000, then the Web API completes normally, but the step goes on for 10 hours. The other parameters have no relation to this issue.

I am seeking a good way to have the Jenkins step complete as soon as the Web API completes, not earlier and not later.

try
{ 
    $url = "https://ourserver.com/modules/OurService"
    $response = Invoke-WebRequest -Uri $url -UseDefaultCredentials -Method Get -TimeoutSec 36000 -UseBasicParsing  
}
catch 
{    
    $err=$_.Exception  
    Write-Host '-----------------------'
    Write-Host $err        
    Write-Host '-----------------------'  
    exit -1
}
exit 0

I tried a different approach, and I think I am closer to where I want to be now...

I decided to go with Invoke-RestMethod

A strange issue remains: when my long running service is called and that service completes, I fall down into the catch, but the Exception has nothing in it. Per the logs for my service, I know the service completed successfully. So, at this point I am treating it as successful.

try
{
  $url = "https://ourserver.com/modules/OurService"
  $response = Invoke-RestMethod -Uri $url -UseDefaultCredentials -Method Get -TimeoutSec 18000
  $response  
  Write-Host 'Exit with success'
  exit 0
}
catch
{
  if (($_.Exception) -and ($_.Exception.Response))
  {
    $respStream = $_.Exception.Response.GetResponseStream()
    $reader = New-Object System.IO.StreamReader($respStream)
    $reader.BaseStream.Position = 0
    $responseBody = $reader.ReadToEnd() | ConvertFrom-Json
    '------------------------------------------------------------------------------------------------------------------------' 
    'Error Message:'
    $responseBody.Message
    $_.Exception
    '------------------------------------------------------------------------------------------------------------------------'
    exit -1
  }
  else
  {
    '------------------------------------------------------------------------------------------------------------------------' 
    'Got to catch, but no exception (we should not have gotten to "catch" area, possible PowerShell bug)'  
    '------------------------------------------------------------------------------------------------------------------------'
    exit 0
  }
  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM