Jenkins管道循环意外终止
问题描述:
我想验证一个tomcat中的所有Web应用程序在执行Jenkins管道后运行。Jenkins管道循环意外终止
我正在验证多个网址,所以我想在for循环中这样做。 它以集合中的第一个url结尾。
这里是我的代码
@NonCPS
def verifyServices(list) {
echo "Services: "+list.size()
def result = true
for(int i = 0; i < list.size(); i++){
if(result) {
result = testUrl(list[i])
}
}
return result
}
def verify = []
verify.add("http://example.com:8082")
verify.add("http://example.com:8082/rest/version")
verify.add("http://example.com:8082/mobile/version")
verifyServices(verify)
而且testUrl功能
def call(urlString) {
echo "Testing ${urlString}"
def url = new URL(urlString)
def HttpURLConnection connection = url.openConnection()
connection.setRequestMethod("GET")
connection.setDoInput(true)
try {
connection.connect()
def code = connection.getResponseCode()
echo "Response code ${code}"
return code == 200
} finally {
connection.disconnect()
}
}
这是我的日志
Proceeding
[Pipeline] echo
Services: 3
[Pipeline] echo
Testing http://example.com:8082
[Pipeline] echo
Response code 200
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
当我删除调用testUrl
功能verifyServices
它遍历所有的他们。
我做错了什么?或者for循环被破坏了?
答
不知何故@NonCPS
注释搞乱了你的方法调用。删除它,你会很好。另外,我不认为你需要它摆在首位,你的代码似乎并没有引入任何序列化的问题@NonCPS
注释会解决......
你可以how to serializable variables阅读更多的CPS technical design或教程建议。
+0
谢谢你的作品,我不知道为什么使用那个注解。 –
For循环不坏 –
太好了。那么我做错了什么?当我使用一个函数(或者只是sh里面的“echo test”for循环时,它会停止迭代... –
假设的'testUrl'函数叫做'call'?你确定这个调用不会出现异常,或者为什么有一个_finally_块? –