Skip to content

Instantly share code, notes, and snippets.

@kellyrob99
Created February 25, 2012 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellyrob99/1907119 to your computer and use it in GitHub Desktop.
Save kellyrob99/1907119 to your computer and use it in GitHub Desktop.
Samples of running Groovy scripts remotely on the Jenkins/Hudson server through the CLI
@GrabResolver(name='glassfish', root='http://maven.glassfish.org/content/groups/public/')
@GrabResolver(name="github", root="http://kellyrob99.github.com/Jenkins-api-tour/repository")
@Grab('org.kar:hudson-api:0.2-SNAPSHOT')
@GrabExclude('org.codehaus.groovy:groovy')
import org.kar.hudson.api.cli.HudsonCliApi
String rootUrl = 'http://localhost:8080'
HudsonCliApi cliApi = new HudsonCliApi()
def listPlugins = 'Hudson.instance.pluginManager.plugins.each { \
println("${it.longName} - ${it.version}") };'
def allFailedBuilds = '''hudsonInstance = Hudson.instance
allItems = hudsonInstance.items
activeJobs = allItems.findAll{job -> job.isBuildable()}
failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result == Result.FAILURE}
failedRuns.each{run -> println(run.name)}'''
def parseableAllFailedBuilds = '''hudsonInstance = Hudson.instance
allItems = hudsonInstance.items
activeJobs = allItems.findAll{job -> job.isBuildable()}
failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result == Result.FAILURE}
[activeJobs:activeJobs?.collect{it.name}, failedRuns:failedRuns?.collect{it.name}].inspect()'''
[listPlugins, allFailedBuilds, parseableAllFailedBuilds].each{ script ->
cliApi.runCliCommand(rootUrl, ['groovysh', script])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment