1
2
3
4
Jenkins.instance.doQuietDown()
Jenkins.instance.numExecutors	=	5
Jenkins.instance.slaveAgentPort	=	[55000]
def	global_domain	=	Domain.global()
def	credentials_store	=	Jenkins.instance.getExtensionList(
				'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].store
def	credentials	=	new	BasicSSHUserPrivateKey(	CredentialsScope.GLOBAL,	null,
				"root",	new	BasicSSHUserPrivateKey.UsersPrivateKeySource(),	"",	"")
credentials_store.addCredentials(global_domain,	credentials)
5
java	-jar	jenkins-cli.jar	[-s	JENKINS_URL]	command	[options…​ ]	[arguments…​ ]
java	-jar	jenkins-cli.jar	-s	 	groovy	somescript.groovy
java	-jar	jenkins-cli.jar	-s	 	groovysh
http://jenkins
http://jenkins
6
import	hudson.plugins.chucknorris.CordellWalkerRecorder
for(item	in	Jenkins.instance.items)	{
					item.publishersList.replace(	new	CordellWalkerRecorder()	)
}
disableChildren(Hudson.instance.items)
def	disableChildren(items)	{
		for	(item	in	items)	{
				if	(item.class.canonicalName	!=
																												'com.cloudbees.hudson.plugins.folder.Folder')	{
	 		if	(	m	=	item.name	=~	/^(Hudson_)(..*)$/	)
										item.renameTo('Jenkins_'	+	m.group(2))
				}	else	disableChildren(item.items)
		}
}
7
▸
$	curl	--data-urlencode	"script=$(<./somescript.groovy)"	
$	curl	--user	'username:password'	.	.	.
http://jenkins/scriptText
8
▸
9
10
▸ ▸
11
▸
12
▸
13
▸
14
▸
15
16
▸
17
▸
node	{
				//	Mark	the	code	checkout	'stage'....
				stage	'Checkout'
				//	Get	some	code	from	a	GitHub	repository
				git	url:	'https://github.com/jglick/simple-maven-project-with-tests.git'
				//	Get	the	maven	tool.
				//	This	'M3'	maven	tool	must	be	configured	in	the	global	configuration
				def	mvnHome	=	tool	'M3'
				stage	'Build'
				//	Run	the	maven	build
				sh	"${mvnHome}/bin/mvn	clean	install"
}
18
19
▸
20
def	project	=	'quidryan/aws-sdk-test'
def	branchApi	=	new	URL("https://api.github.com/repos/${project}/branches")
def	branches	=	new	groovy.json.JsonSlurper().parse(branchApi.newReader())
branches.each	{
				def	branchName	=	it.name
				def	jobName	=	"${project}-${branchName}".replaceAll('/','-')
				job(jobName)	{
								scm	{
												git("git://github.com/${project}.git",	branchName)
								}
								steps	{
												maven("test	-Dproject.name=${project}/${branchName}")
								}
				}
}
21
22
def	labelMap	=	[	arm:	"armcc",	win:	"vs2010",	linux:	"gcc"	]
return	labelMap."${binding.variables.platform}"
23
▸
24
25
▸ ▸
26
27
▸ ▸
28
▸
29
import	groovyx.remote.client.RemoteControl
import	groovyx.remote.transport.http.HttpTransport
def	transport	=	new	HttpTransport("http://myJenkins/plugin/groovy-remote")
def	remote	=	new	RemoteControl(transport)
//	This	code	runs	on	local.
def	name	=	'kiy0taka'
println	name
def	result	=	remote	{
				//	This	closure	runs	on	Jenkins	server.
				def	version	=	jenkins.version.value
				println	"Hi,	${name}!"
				//	Return	Jenkins	version.
				return	version
}
//	This	code	runs	on	local.
println	"Jenkins	version	was	${result}."
30
31
▸ ▸
32
▸
33
def	authors	=	"[hidden	email]"
if	(build.environment["RELEASE_BUILD"])	{
				authors	=	build.environment["RELEASE_BUILD_EMAIL_DISTRIBUTION"]
}
return	authors
34
build	result	is	${build.result}
build	url	is	${rooturl}${build.url}
<%	def	changeSet	=	build.changeSet
if(changeSet	!=	null)	{
				def	hadChanges	=	false
				changeSet.each()	{	cs	->
	 hadChanges	=	true
	 def	aUser	=	cs.author	%>
Revision:	(${aUser.displayName})	${cs.msgAnnotated}
<%	 cs.affectedFiles.each()	{	p	->	%>
${p.editType.name},${p.path}
<%						}
				}
				if(!hadChanges)	{
%>
No	Changes
<%		}
}	%>
35
36

Using Groovy with Jenkins