Last active
September 20, 2022 00:11
-
-
Save cmaggiulli/102960d56d1a5b9aa8eb7cd23934b39f to your computer and use it in GitHub Desktop.
A variety of Jenkins Groovy Script Console scripts for administering Jenkins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def job = Hudson.instance.getJob('job-name') | |
| def cardinality = job.builds.size() | |
| def summation = job.builds*.duration.sum() | |
| print( ( ( summation/cardinality/1000 as double ) ).round(1) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| item = Jenkins.instance.getItemByFullName("your-build-name") | |
| //THIS WILL REMOVE ALL BUILD HISTORY | |
| item.builds.each() { build -> | |
| try { | |
| println "Deleting build " + build.number | |
| build.delete() | |
| // After clearing out all the builds using delete | |
| // Try another run using dropLinks() to clear out some orphaned stuff | |
| // build.dropLinks() | |
| } | |
| catch(Exception e) { | |
| println "Unable to delete build" | |
| println e.message | |
| } | |
| } | |
| item.updateNextBuildNumber(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hudson.model.* | |
| disableChildren( Hudson.instance.items ) | |
| def disableChildren( items ) { | |
| for ( item in items ) { | |
| if ( item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder' ) { | |
| item.disabled = true | |
| item.save() | |
| println( item.name ) | |
| } else { | |
| disableChildren( ( ( com.cloudbees.hudson.plugins.folder.Folder ) item ).getItems() ) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import jenkins.model.* | |
| Jenkins.instance.getAllItems(AbstractProject.class) | |
| .findAll { it.logRotator } | |
| .each { | |
| it.logRotator.perform(it) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.text.SimpleDateFormat; | |
| import java.util.GregorianCalendar; | |
| df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
| starttime = new GregorianCalendar(); | |
| endtime = new GregorianCalendar(); | |
| buildEnd = new GregorianCalendar(); | |
| starttime.setTime( df.parse("2020-11-01 00:00:00") ); | |
| endtime.setTime( df.parse("2020-1-30 23:59:59") ); | |
| for ( hudson.model.AbstractProject p : hudson.model.Hudson.instance.projects ) { | |
| count = 0; | |
| for ( hudson.model.AbstractBuild b : p.getBuilds() ) { | |
| if( b == null) break; | |
| if ( b.isBuilding() || b.timestamp.compareTo(endtime) >= 0 ) continue; | |
| if( b.timestamp != null ) { | |
| buildEnd.setTimeInMillis( b.timestamp.timeInMillis + b.duration ); | |
| if ( buildEnd.compareTo(starttime) < 0 ) | |
| break; | |
| count++; | |
| } | |
| } | |
| if( count == 2 ) | |
| println p.toString() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def buildNum = 1 | |
| def jobName = "name" | |
| Jenkins.instance.getItemByFullName(jobName).builds.find{ build -> buildNum == build.getNumber().toInteger() }.each{ it.doKill() && it.delete() } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Kill Jenkins Script Console threads without raising ThreadDeath. This often happens with heavy HTTP Post Requests | |
| Thread.getAllStackTraces().keySet().findAll{ | |
| it.id != Thread.currentThread().id && it.name ==~ /^Handling\sPOST\s\/jenkins\/script.*/ | |
| }.each { try { println it.interrupt() } catch(e) { pritnln it.stop() } } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( | |
| com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, | |
| Jenkins.instance, | |
| null, | |
| null | |
| ); | |
| for (c in creds) { | |
| println( ( c.properties.privateKeySource ? "ID: " + c.id + ", UserName: " + c.username + ", Private Key: " + c.getPrivateKey() : "")) | |
| } | |
| for (c in creds) { | |
| println( ( c.properties.password ? "ID: " + c.id + ", UserName: " + c.username + ", Password: " + c.password : "")) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hudson.console.PlainTextConsoleOutputStream | |
| import java.io.ByteArrayOutputStream | |
| import java.util.Date | |
| import jenkins.model.Jenkins | |
| import org.apache.commons.io.IOUtils | |
| import org.jenkinsci.plugins.workflow.job.WorkflowJob | |
| import org.jenkinsci.plugins.workflow.job.WorkflowRun | |
| final jenkins = Jenkins.instance | |
| final lookBack = -1 // Number of days to look back | |
| // Using legacy Calendar instead of Instant due hudson.model.Run.timestamp parameter | |
| final before = Calendar.instance | |
| final after = (Calendar) before.clone() | |
| after.add(Calendar.HOUR, lookBack); | |
| println sprintf("Jobs executed between %s and %s with a non-null build description", before.time, after.time) | |
| jenkins.getAllItems( Job.class ).each{ job -> | |
| job.builds.findAll{ run -> | |
| run.timestamp?.before( before ) && | |
| run.timestamp?.after( after ) && | |
| run.result == Result.SUCCESS | |
| }.each { run -> | |
| println sprintf( "%s | %o | %s | %s", job.name, run.number, run.description, run.time ) | |
| ByteArrayOutputStream os = new ByteArrayOutputStream() | |
| InputStreamReader is = run?.logText.readAll() | |
| PlainTextConsoleOutputStream pos = new PlainTextConsoleOutputStream(os) | |
| IOUtils.copy(is, pos) | |
| String text = os.toString() | |
| Boolean result = text.contains("Record Contains") | |
| os.close() | |
| is.close() | |
| pos.close() | |
| println result | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hudson.model.* | |
| import Jenkins.* | |
| import hudson.security.Permission | |
| import hudson.security.GlobalMatrixAuthorizationStrategy | |
| def userId = "gfarkas" | |
| def userPermissionList = [hudson.model.Item.CONFIGURE] | |
| Hudson instance = Jenkins.get() | |
| GlobalMatrixAuthorizationStrategy authStrategy = Jenkins.instance.getAuthorizationStrategy() | |
| // Removing each permission from list | |
| userPermissionList.each { permission -> | |
| permission.enabled = false | |
| authStrategy.add(permission, userId) | |
| instance.setAuthorizationStrategy(authStrategy) | |
| } | |
| instance.save() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hudson.model.* | |
| def jobs = Jenkins.instance.getAllItems(Job.class) | |
| jobs.findAll{ !it.logRotator && !it.disabled }.each { job -> | |
| job.logRotator = new hudson.tasks.LogRotator ( "30", "", "", "" ) | |
| println "$job.name" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment