Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using Groovy with Jenkins

2,257 views

Published on

Jenkins is a often used integration system and with continuous deployment
it has to do more than just building your code.
Since Jenkins has support for Groovy in the build chain many tasks
can be achieved much simpler.

This session shows some examples how to simplify your life with Jenkins using Groovy.

Published in: Software
  • Be the first to comment

Using Groovy with Jenkins

  1. 1. 1
  2. 2. 2
  3. 3. 3
  4. 4. 4
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. ▸ $ curl --data-urlencode "script=$(<./somescript.groovy)" $ curl --user 'username:password' . . . http://jenkins/scriptText 8
  9. 9. ▸ 9
  10. 10. 10
  11. 11. ▸ ▸ 11
  12. 12. ▸ 12
  13. 13. ▸ 13
  14. 14. ▸ 14
  15. 15. ▸ 15
  16. 16. 16
  17. 17. ▸ 17
  18. 18. ▸ 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. 19. 19
  20. 20. ▸ 20
  21. 21. 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. 22. 22
  23. 23. def labelMap = [ arm: "armcc", win: "vs2010", linux: "gcc" ] return labelMap."${binding.variables.platform}" 23
  24. 24. ▸ 24
  25. 25. 25
  26. 26. ▸ ▸ 26
  27. 27. 27
  28. 28. ▸ ▸ 28
  29. 29. ▸ 29
  30. 30. 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. 31. 31
  32. 32. ▸ ▸ 32
  33. 33. ▸ 33
  34. 34. def authors = "[hidden email]" if (build.environment["RELEASE_BUILD"]) { authors = build.environment["RELEASE_BUILD_EMAIL_DISTRIBUTION"] } return authors 34
  35. 35. 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. 36. 36

×