Posts

Showing posts with the label Groovy

Jenkins Groovy email api example

emailext mimeType: 'text/html',                attachLog: true/false,                attachmentsPattern: '**/target/......./text.txt',                subject: ".........",                body: "text/html body",                to:  "recipient emails"         

Jenkins Groovy to read file example

body = readFile(".........path/text.txt")

Jenkins groovy file to read credentials properties from Jenkins application credentials

stages{ ....... stage(){ steps{ withCredentials([usernamePassword(credentialsId:'id',passwordVariable:'pass',usernameVariable:'urd')]){ script{ use ....... ={$id}  {$pass} ........... } } } } .......... }

Groovy : Use Collect with Initial Collection Value

The collect() method in Groovy can be used to iterate over collections and transform each element of the collection. The transformation is defined in as a closure and is passed to the collect() method. But we can also add an initial collection to which the transformed elements are added. // Collect without // initial collection. assert [0,2,4,6] == (0..3).collect { it * 2 } assert ['Groovy', 'Grails'] == [lang: 'Groovy', framework: 'Grails'].collect { it.value } // Collect with initial collection argument. assert [0, 1, 2, 3] == [2, 3].collect([0, 1]) { it } assert [0, 3, 6, 9] == [2, 3].collect([0, 3], { it * 3}) assert ['Gradle', 'groovy', 'grails'] == ['Groovy', 'Grails'].collect(['Gradle']) { it.toLowerCase() } assert ['m','r','h','a','k','i'] == [4, -3, 7, 5].collect(['m', 'r']) { (it + 100) as char }

Groovy : Regular Expressions example

In Groovy we use the =~ operator (find operator) to create a new matcher object. If the matcher has any match results we can access the results by invoking methods on the matcher object. But Groovy wouldn't by groovy if we could access the results easier. Groovy enhances the Matcher class so the data is available with an array-like syntax. If we use groups in the matcher the result can be accessed with a multidimensional array. Although the result of the =~ operator is a matcher object in a conditional statement the result will be converted to a Boolean values. We can use a second operator, ==~ (match operator), to do exact matches. With this operator the matches() method is invoked on the matcher object. The result is a Boolean value. def finder = ('groovy' =~ /gr.*/) assert finder instanceof java.util.regex.Matcher def matcher = ('groovy' ==~ /gr.*/) assert matcher instanceof Boolean assert 'Groovy rocks!' =~ /Groovy/  // =~ in conditional cont...