简体   繁体   English

在 Groovy (Jenkins) 中循环执行 HTTP Post

[英]Execute HTTP Post in a loop in Groovy (Jenkins)

I'm developing a shared library in Jenkins to help with interacting with an internal API.我正在 Jenkins 中开发一个共享库,以帮助与内部 API 进行交互。 I can make single call which starts a long running process to create an object.我可以进行单个调用,启动一个长时间运行的进程来创建一个对象。 I have to continue to query the API to check for the process' completion.我必须继续查询 API 以检查进程是否完成。

I'm trying to get this done in using a simple loop, but I keep getting stuck.我正在尝试使用一个简单的循环来完成这项工作,但我一直被卡住。 Here's my function to query the API until it's completed:这是我查询 API 直到它完成的函数:

def url = new URL("http://myapi/endpoint")
HttpURLConnection = http = (HttpURLConnection) url.openConnection()
http.setDoOutput(true)
http.setRequestMethod('POST')
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
def body = ["string", "anotherstring"].join('=')
OutputStreamWriter osw = new OutputStreamWriter(http.outStream)
osw.write(body)
osw.flush()
osw.close()
for(int i = 0; i < 30; i++) {
    Integer counter = 0
    http.connect()
    response = http.content.text
    def status = new JsonSlurperClassic().parseText(response)
    // Code to check values here
}

When I run this through a pipeline, the first iteration through the loop works fine.当我通过管道运行它时,循环中的第一次迭代工作正常。 The next iteration bombs with this error:下一次迭代会出现此错误:

Caused: java.io.NotSerializableException: sun.net.www.protocol.http.HttpURLConnection

I just started in Groovy, so I feel like I'm trying to do this wrong.我刚开始使用 Groovy,所以我觉得我试图做错了。 I've looked all over trying to find answers and tried several things without any luck.我四处寻找答案并尝试了几件事但没有任何运气。

Thanks in advance.提前致谢。

When running a pipeline job, Jenkins will constantly save the state of the execution so it can be paused and later on resumed, this means that Jenkins must be able to serialize the state of the script and therefore all of the objects that you create in your pipeline must be serializable.运行流水线作业时,Jenkins 将不断保存执行状态,以便它可以暂停并稍后恢复,这意味着 Jenkins 必须能够序列化脚本的状态,因此您在您的系统中创建的所有对象管道必须是可序列化的。
If an object is not serializable you will get the NotSerializableException whenever Jenkins will attempt to serialize your un-serializable object when it save the state.如果对象不可序列化,则每当 Jenkins 在保存状态时尝试序列化不可序列化对象时,您都会收到NotSerializableException

To overcome this issue you can use the @NonCPS annotation, which will cause Jenkins to execute the function without trying to serialize it.为了解决这个问题,您可以使用@NonCPS注释,这将导致 Jenkins 执行该函数而不尝试对其进行序列化。 Read more info on this issue at pipeline-best-practice .管道最佳实践中阅读有关此问题的更多信息。

While normal Pipeline is restricted to serializable local variables (see appendix at bottom), @NonCPS functions can use more complex, nonserializable types internally (for example regex matchers, etc).虽然普通管道仅限于可序列化的局部变量(请参阅底部的附录),但 @NonCPS 函数可以在内部使用更复杂的、不可序列化的类型(例如正则表达式匹配器等)。 Parameters and return types should still be Serializable, however.但是,参数和返回类型仍然应该是可序列化的。

There are however some limitations so read the documentation carefully, for example the return value types from @NonCPS methods must be serializable and you can't use any pipeline steps or CPS transformed methods inside a @NonCPS annotated function.但是有一些限制,所以请仔细阅读文档,例如,@NonCPS 方法的返回值类型必须是可序列化的,并且不能在 @NonCPS 注释函数中使用任何管道步骤或 CPS 转换方法。 Additional info can be found here . 可以在此处找到其他信息

One last thing, to overcome all these issues you can also use the Jenkins HTTP Plugin which includes all the HTTP abilities you will probably need wrapped in an easy to use build in interface.最后一件事,要克服所有这些问题,您还可以使用Jenkins HTTP 插件,该插件包含您可能需要的所有 HTTP 功能,并封装在易于使用的内置界面中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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