Load script from groovy script

If you don’t mind the code in file2 being in a with block, you can do: new GroovyShell().parse( new File( ‘file1.groovy’ ) ).with { method() } Another possible method would be to change file1.groovy to: class File1 { def method() { println “test” } } And then in file2.groovy you can use mixin to add … Read more

Can you break from a Groovy “each” closure?

Nope, you can’t abort an “each” without throwing an exception. You likely want a classic loop if you want the break to abort under a particular condition. Alternatively, you could use a “find” closure instead of an each and return true when you would have done a break. This example will abort before processing the … Read more

Creating a Jenkins environment variable using Groovy

Jenkins 1.x The following groovy snippet should pass the version (as you’ve already supplied), and store it in the job’s variables as ‘miniVersion’. import hudson.model.* def env = System.getenv() def version = env[‘currentversion’] def m = version =~/\d{1,2}/ def minVerVal = m[0]+”.”+m[1] def pa = new ParametersAction([ new StringParameterValue(“miniVersion”, minVerVal) ]) // add variable to … Read more

Including a groovy script in another groovy

evaluate(new File(“../tools/Tools.groovy”)) Put that at the top of your script. That will bring in the contents of a groovy file (just replace the file name between the double quotes with your groovy script). I do this with a class surprisingly called “Tools.groovy”.

Groovy executing shell commands

Ok, solved it myself; def sout = new StringBuilder(), serr = new StringBuilder() def proc=”ls /badDir”.execute() proc.consumeProcessOutput(sout, serr) proc.waitForOrKill(1000) println “out> $sout\nerr> $serr” displays: out> err> ls: cannot access /badDir: No such file or directory

How do I get the output of a shell command executed using into a variable from Jenkinsfile (groovy)?

The latest version of the pipeline sh step allows you to do the following; // Git committer email GIT_COMMIT_EMAIL = sh ( script: ‘git –no-pager show -s –format=\’%ae\”, returnStdout: true ).trim() echo “Git committer email: ${GIT_COMMIT_EMAIL}” Another feature is the returnStatus option. // Test commit message for flags BUILD_FULL = sh ( script: “git log … Read more

Split a string in groovy

You can use the Java split(regex) method to achieve your first goal and then groovy syntactic sugar to help with the rest: def str = “,,,,,” def arr = str.split(/,/, -1) println arr.size() // 6 arr[0] = 1 arr[1] = 2 arr[2] = 3 println arr // [1, 2, 3, , , ] See also … Read more