Skip to content

baiyongzhen/cheatsheet-jenkins-groovy-A4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

1 CheatSheet: Jenkins & Groovy

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

1.1 Jenkins Pipeline

NameComment
Specify parameter to run jobsbuild job:'job1', parameters:[string(name:'name1', value:va1)]
Run job in different agentsnode($agent_label) {...}
Ask for user inputstage('stage2'){ input "OK to go?" }
Actively fail current pipeline joberror("Build failed because of this and that..")
Keep going when previous stage has failedkeep-going-with-errors.groovy
Send slack notification in pipelineslack-notification.groovy
Pass parameter across jenkins jobsjenkinsfile-pass-parameter.groovy
Set timeout & retryjenkinsfile-timeout-retry.groovy
Use finally to do cleanupjenkinsfile-finally.groovy
Run jenkins jobs in a sequential wayjenkinsfile-sequentially.groovy
Run jenkins jobs in paralleljenkinsfile-parallelly.groovy

1.2 Config Jenkins Via Groovy

NameComment
Set timezone for jenkinstimezone.groovy
Create jenkins views and add jobs to itjenkins-views.groovy
Configure default viewjenkins-views.groovy
Add default jobsCopy jobs config.xml to /usr/share/jenkins/ref/jobs/

1.3 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

1.4 Groovy String

NameComment
Convert list to stringl.join(";")
Create string with multi-linesmulti-line-string.groovy
Convert string to listsplit-string.groovy

1.5 Groovy Array

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

1.6 Groovy Dictionary

NameComment
Create a mapdef m = ['fruit':'Apple', 'veggie':'Carrot']
Add an item to mapm.put('denny','hello')
Check if key existsm.containsKey('key1')
Loop a maploop-map.groovy

1.7 Groovy Network

NameComment
Get hostname by ipget-ip-by-hostname.groovy
validate user input: ip addressassert ip_address.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")

1.8 Jenkins Summary

NameCommand
Test groovy code from Jenkins GUIManage Jenkins -> Script Console
Jenkins Slave Node

1.9 Jenkins Plugins

NameCommand
Backup jenkinsThinBackup Plugin
Load the ssh keyCredentials Plugin
Add timestamp to job outputTimestamper Plugin
Create dashboardDashboard View Plugin
Abort if job takes too longBuild-timeout Plugin
Retry failed a jobNaginator Plugin
Backup job configurationJobConfigHistory Plugin

1.10 Jenkins Groovy

NameCommand
Print message to stdoutSee print.groovy
Get environment variablesSee get-env.groovy
Convert string to listSee split-string.groovy
Create string with multi-linesSee multi-line-string.groovy
Loop a mapSee loop-map.groovy
Get hostname by ipSee get-ip-by-hostname.groovy
Convert list to stringl.join(";"), link: groovy list

1.11 More Resources

License: Code is licensed under MIT License.

linkedin github slack

About

📖 Groovy CheatSheet For Jenkins Usage In A4

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Groovy 98.0%
  • Shell 2.0%