简体   繁体   English

如何正确使用 Jenkins PowerShell 步骤调用 ZC6E190B284633C48E39E55049DA3CCERequest8Z ZDB974238714CA8DE634A7CE1D083A1vokeF 使用 InMethodest-WebvokeF

[英]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.我正在使用JenkinsPowerShell命令步骤中调用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.

相关问题 如何更改Powershell Invoke-WebRequest或Invoke-RestMethod的ReadWriteTimeout? - how to change ReadWriteTimeout for powershell Invoke-WebRequest or Invoke-RestMethod? powershell invoke-webrequest 不起作用,但 invoke-restmethod 有效 - powershell invoke-webrequest does not work but invoke-restmethod works 使用-OutFile参数时如何在Invoke-WebRequest / Invoke-RestMethod上获取HTTP状态? - How to get HTTP status on Invoke-WebRequest/Invoke-RestMethod when using -OutFile parameter? 我正在尝试使用invoke-restmethod / invoke-webrequest在powershell中执行REST API,但在传递Json输入时失败 - I am trying to execute REST API within powershell using invoke-restmethod/invoke-webrequest but failed when I pass Json inputs 将curl命令转换为invoke-WebRequest或Invoke-RestMethod - convert curl command to invoke-WebRequest or Invoke-RestMethod Invoke-WebRequest 和 Invoke-RestMethod 的不同结果 - Different results from Invoke-WebRequest and Invoke-RestMethod 如何使用JSON来Powershell Invoke-RestMethod? - How to use JSON to Powershell Invoke-RestMethod? 如何使用PowerShell Invoke-WebRequest发布 - How to use PowerShell Invoke-WebRequest Post Powershell Invoke-RestMethod - Powershell Invoke-RestMethod 调用 RESTMethod PowerShell - Invoke-RESTMethod PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM