Return element from list

I’m trying to return an element from list but all I get is ‘Null’. Expected answer “el1” or “el2”

def list= ["el1", "el2", "el3" ]

def find_element(list)
{
    for (int i=0; i<list.size(); i++)
    {
        node(list[i])
        {
            if (bat(Check.exe)==0){
                return list[i]
            }
        }
    }
}

stage('Retturn value') 
{
    steps 
    {
    	script 
    	{
    	    echo find_elements(list)
......

You return only if bat() succeeds. Otherwise nothing is returned. This means bat() check never worked. I don’t know what node() and bat() are supposed to do.

Also not sure how bat(Check.exe) is supposed to work, is Check a valid object instance?

node() { … } runs code on an agent. It’s documented at node : Allocate node and implemented in https://github.com/jenkinsci/workflow-durable-task-step-plugin/blob/8fd69d0b8857c1bdd50bbae982d76c218d4af6cb/src/main/java/org/jenkinsci/plugins/workflow/support/steps/ExecutorStep.java.

bat(Check.exe) should probably be bat(returnStatus: true, script: 'Check.exe') or even specify the path of Check.exe.

@kon can the steps (node, bat) be included freely in Groovy functions?

AFAIK they can, but not in @NonCPS. Pipeline CPS Method Mismatches

1 Like