Using CheatSheets To Apply Best Practices

CheatSheet: Jenkins & Groovy

CheatSheet: Jenkins & Groovy

1.1 Config Jenkins Via Groovy

Name Comment
Set timezone for jenkins timezone.groovy
Create jenkins views and add jobs to it jenkins-views.groovy
Configure default view jenkins-views.groovy
Create a Jenkins user create-jenkins-user.groovy
logged-in users can do anything logged-in-users.groovy
Add default jobs Copy jobs config.xml to /usr/share/jenkins/ref/jobs/
Jenkins skip wizzard when initialization -Djenkins.install.runSetupWizard=false
Install jenkins plugins /usr/local/bin/ < /usr/share/jenkins/ref/plugins.txt
Configure jenkins slave Node  

1.2 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) {...}
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 keep-going-with-errors.groovy
Send slack notification in pipeline slack-notification.groovy
Pass parameter across jenkins jobs jenkinsfile-pass-parameter.groovy
Set timeout & retry jenkinsfile-timeout-retry.groovy
Use finally to do cleanup jenkinsfile-finally.groovy
Run jenkins jobs in a sequential way jenkinsfile-sequentially.groovy
Run jenkins jobs in parallel jenkinsfile-parallelly.groovy

1.3 Jenkins Basic

Name Comment
Deploy Jenkins via docker

1.4 Groovy Basic

Name Comment
Read file content as a variable def env = System.getenv(), def content = readFile("/tmp/test.txt")
Print stdout echo ‘Action is done’, println “Hello World”
Use boolean parameter if (istrue == “false”) {…}
Basic integer caculation def a = 3, b = 7; println “$a + $b = ${a + b}”
Get environment variables get-env.groovy
Run groovy online SaaS: Groovy Web console
Run groovy script from Jenkins Link: Jenkins Script Console
Reference Link: Apache Groovy

1.5 Groovy String

Name Comment
Convert list to string l.join(";")
Create string with multi-lines multi-line-string.groovy
Convert string to list split-string.groovy
Convert string to json string-to-json.groovy

1.6 Groovy Array

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

1.7 Groovy Dictionary

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')
Loop a map loop-map.groovy

1.8 Groovy Network

Name Comment
Get hostname by ip get-ip-by-hostname.groovy
validate user input: ip address assert ip_address.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")

1.9 Jenkins Groovy

Name Command
Print message to stdout See print.groovy
Get environment variables See get-env.groovy
Convert string to list See split-string.groovy
Create string with multi-lines See multi-line-string.groovy
Loop a map See loop-map.groovy
Get hostname by ip See get-ip-by-hostname.groovy
Convert list to string l.join(";"), link: groovy list

1.10 Jenkins Plugins

Name Command
Backup jenkins ThinBackup Plugin
Load the ssh key Credentials Plugin
Add timestamp to job output Timestamper Plugin
Create dashboard Dashboard View Plugin
Abort if job takes too long Build-timeout Plugin
Retry failed a job Naginator Plugin
Backup job configuration JobConfigHistory Plugin

Leave a Reply

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