Marina19
Marina19

Reputation: 19

How does this jenkins groovy script work?

This is my groovy Script on Jenkins

def gitURL = "http://bitbucket.webapp.intern.de/scm/myproject.git"
def command = "git ls-remote -h $gitURL"

def proc = command.execute()  // line 3
proc.waitFor()              

if ( proc.exitValue() != 0 ) {
   println "Error, ${proc.err.text}"
   System.exit(-1)
}

def branches = proc.in.text.readLines().collect {    // line 9
    it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '')    // line 10
}

return branches

First I do not understand anything. How on line 3 can you call execute on command which is supposed to be a string ??

What type is variable branches in line 9 ??? Is branches a String ??

What is it.replaceAll in line 10 ??? I know replaceAll as method from String. But it has 2 parameters. I do not see 2 parameters here.

Now I somehow understand branches contains all branches. What I want to do. I only want to have branches which contain "-REST" it their names. How can I do this ??

My intention was using java. But it does not work.

List<String> branchesNew = new ArrayList<String>();

for(String branch : branchesNew)
{
     if(branch.contains("-REST"))
            branchesNew.add(branch);
}

Upvotes: 0

Views: 346

Answers (1)

Lorenzo Orsatti
Lorenzo Orsatti

Reputation: 163

line 3: this is a Groovy feature, see http://groovy-lang.org/groovy-dev-kit.html#process-management

line 9: it's a collection (a list, for instance) given as output of the collect method. see also http://groovy-lang.org/groovy-dev-kit.html#_working_with_collections

line 10: the second argument is ''. it replaces the regexp matches (branch prefixes) with an empty string.

You can filter all your wanted branches using the findAll method on collections. Following the given example:

def branchesNew = proc.in.text.readLines().findAll{ it.contains('-REST') }

Look at Filtering and searching for all related manipulation methods.

Upvotes: 1

Related Questions