Pavel Polivka
Pavel Polivka

Reputation: 878

Jenkins Pipeline loop ends unexpectedly

I am trying to verify that all my web applications in one tomcat are running after execution of Jenkins pipeline.

I am verifing more than one url so I want to do that in for loop. It ends on first url in collection.

Here is my code

@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)

And testUrl function

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()
    }
}

Here is my log

Proceeding
[Pipeline] echo
Services: 3
[Pipeline] echo
Testing http://example.com:8082
[Pipeline] echo
Response code 200
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

When I remove call to testUrl function in verifyServices it iterates over all of them.

Am I doing something wrong? Or for loops are just broken?

Upvotes: 1

Views: 1129

Answers (1)

Pom12
Pom12

Reputation: 7880

Somehow the @NonCPS annotation is messing with your methods calls. Remove it and you will be just fine. Plus I don't think you need it in the first place, your code doesn't seem to introduce any serializable issue @NonCPS annotation would fix...

You can read more on CPS technical design or tutorial recommendations on how to serializable variables.

Upvotes: 1

Related Questions