Using CheatSheets To Apply Best Practices

Jenkins Groovy CheatSheet

Jenkins Groovy CheatSheet

1.1 Jenkins Pipeline

Name Comment
Specify parameter to run jobs build job:'job1', parameters:[string(name:'name1', value:va1)]
Run job in different agents node($agent_label) {...}
Use boolean parameter if (istrue == “false”) {…}
Ask for user input stage('stage2'){ input "OK to go?" }
Actively fail current pipeline job error("Build failed because of this and that..")
Keep going when previous stage has failed keepgoingwitherrors.groovy
Send slack notification in pipeline slacknotification.groovy
validate user input: ip address assert ip_address.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")
Read Environment Variables def env = System.getenv()

1.2 Array

Name Comment
Iterate a list (1..3).each { println "Number ${it}"}
Iterate a list for(item in [1,2,3,4]){ println item }
Add item to list def alist = [10, 9, 8]; alist << 7
List size def alist = [10, 9, 8]; alist.size()

1.3 String

Name Comment
Print stdout echo ‘Action is done’
Print stdout println “Hello World”
Split string with delimiter ‘1128-2’.tokenize ( ‘-‘ )

1.4 Integer

Name Comment
Basic caculation def a = 3, b = 7; println “$a + $b = ${a + b}”

1.5 Dict/Hashmap/Map

Name Comment
Create a map def m = ['fruit':'Apple', 'veggie':'Carrot']
Add an item to map m.put('denny','hello')
Check if key exists m.containsKey('key1')

1.6 Files

Name Comment
Read file content as a variable def content = readFile("/tmp/test.txt")

1.7 More Resources




Leave a Reply

Your email address will not be published. Required fields are marked *