[英]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.我正在使用Jenkins在PowerShell命令步骤中调用Web ZDB974238714CAA8DE634A7CE1D08服务I am calling the service using Invoke-WebRequest .
我正在使用Invoke-WebRequest调用该服务。
This service call has to be in Jenkins, because I need to do it only if other Jenkins jobs have completed successfully.此服务调用必须在 Jenkins 中,因为只有在其他 Jenkins 作业成功完成时我才需要这样做。
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.
如果我没有TimeoutSec参数,则该步骤会在 Web API 完成之前以超时完成。 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.
如果我有一个较大的参数,比如36000,那么Web API 正常完成,但是这个步骤持续了10个小时。 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.我正在寻找一种在 Web API 完成后立即完成 Jenkins 步骤的好方法,而不是更早或更晚。
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我决定用Invoke-RestMethod go
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.