Skip to content

Instantly share code, notes, and snippets.

@martyngigg
Last active August 29, 2015 14:18
Show Gist options
  • Save martyngigg/887d01ab6f04ea5541ae to your computer and use it in GitHub Desktop.
Save martyngigg/887d01ab6f04ea5541ae to your computer and use it in GitHub Desktop.
Useful Jenkins Groovy Scripts

List all scm urls

import jenkins.model.*;
import hudson.model.*;
import hudson.tasks.*;
import hudson.plugins.git.*;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;

for(project in Hudson.instance.items) {
  scm = project.scm;
  if (scm instanceof hudson.plugins.git.GitSCM) {
    for (RemoteConfig cfg : scm.getRepositories()) {
      for (URIish uri : cfg.getURIs()) {
        println("SCM " + uri.toString() + " for project " + project);    
      }
    } 
  }  
}

List all loggers

import java.util.logging.*;

LogManager.getLogManager().getLoggerNames().each() {
  println "${it}";
}

List directory contents

new File("PATH").eachFile() { file->  
    println file.getName()  
}  

Delete directory tree

// Create a ref for closure
def delClos


// Define closure
delClos = { println "Dir ${it.canonicalPath}";
it.eachDir( delClos );
it.eachFile {
println "File ${it.canonicalPath}";
it.delete()
}
it.delete()
}


// Apply closure
delClos( new File("PATH") )

Run process

Process p = "cmd /c dir".execute()
println "${p.text}"

kill process on windows


Process p = "cmd /c Taskkill /F /IM MantidPlot.exe".execute()
println "${p.text}"

Delete directory with python

"C:\\python27\\python.exe -c \"import shutil; shutil.rmtree('C:/Jenkins/workspace/master_clean/label/win7-build/build')\"".execute().text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment