2016-08-18 146 views
1

我想驗證一個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循環被破壞了?

+0

For循環不壞 –

+0

太好了。那麼我做錯了什麼?當我使用一個函數(或者只是sh裏面的「echo test」for循環時,它會停止迭代... –

+0

假設的'testUrl'函數叫做'call'?你確定這個調用不會出現異常,或者爲什麼有一個_finally_塊? –

回答

1

不知何故@NonCPS註釋搞亂了你的方法調用。刪除它,你會很好。另外,我不認爲你需要它擺在首位,你的代碼似乎並沒有引入任何序列化的問題@NonCPS註釋會解決......

你可以how to serializable variables閱讀更多的CPS technical design或教程建議。

+0

謝謝你的作品,我不知道爲什麼使用那個註解。 –