Project

General

Profile

Actions

Groovy Scripts for Jenkins


You can evaluate groovy scripts in the Jenkins Script Console.

There are several plugins which can be programmed with Groovy:


The most up to date scripts are found in the jenkins scriptler folders


Get all versions of an artifact from a maven repository

def metadata = new XmlSlurper().parse("http://cybertaxonomy.org/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
metadata.versioning.versions.version.each{
      println "> " + it
}

doc:

the same for the Dynamic Parameter Plug-in which is very nice in conjunction with the *Repository Connector Plugin

def metadata = new XmlSlurper().parse("http://cybertaxonomy.org/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
def list = []
metadata.versioning.versions.version.each{
  list.add(it)
}
return return list.reverse(true)

list datasource bean names in datasources.xml

/**
 * define the instances to excluded from the axix:
 */
def excludes = ['col', 'vibrant_index']

// list datasource bean names in datasources.xml
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
def instances = []

beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
    if (! excludes.contains(it.attribute("id"))) {
        instances += it.attribute("id")
    }
}
return  instances

find last bean in datasources.xml

// find last bean in datasources.xml
def beans = new XmlParser().parse("/etc/cdmserver/datasources.xml")
def lastBean = null
beans.bean.findAll{it.attribute("class") == "com.mchange.v2.c3p0.ComboPooledDataSource"}.each{  
    lastBean = it
}
println lastBean

monitor cdm server until all instances are started up

import groovy.json.JsonSlurper
import groovy.time.TimeCategory

// Configuration
def cdmserverUrl = "http://test.e-taxonomy.eu/cdmserver/"
def usr = "xxx"
def pwd = "yyy"
def timeout = 1 // minutes

// init
def timeOver
use(TimeCategory) {
  timeOver = timeout.minutes.from.now
}

def addr = cdmserverUrl + 'manage/BootloaderService.jsp'
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
def instances = null
def lastInstance = null
def allInstancesUp = false
def message = "Timeout"

// monitor the cdmserver until the last instance is up or until the time is over
while (new Date() < timeOver) {
  def conn = addr.toURL().openConnection()

  conn.setRequestProperty( "Authorization", "Basic ${authString}" )
  if( conn.responseCode == 200 ) {
    instances = new JsonSlurper().parseText( conn.content.text )
    lastInstance = instances.pop()
    println lastInstance.configuration.instanceName + ": " + lastInstance.status
    if(lastInstance.status == "started") {
      message "OK"
      allInstancesUp = true
      break
    }

  } else {
    message "Error on requesting for status information${conn.responseCode}: ${conn.responseMessage}"
  }
  println new Date()
  sleep(10000) // milliseconds
}

println message

return allInstancesUp

execute other scriptler script

by using eval()

// prepare variables for subscript and evaluate it:
String subscript = "cdmserver-monitor-instance-status.groovy";
println "executing subscript ${subscript}"
// variables to be passed to the subscript must not be bound to the current scope, so no def or String here
status = (action == "start" ? "started" : "stopped")

def s = ScriptlerConfiguration.getConfiguration().getScriptById(subscript)
File scriptSrc = new File(ScriptlerManagment.getScriptDirectory(), s.getScriptPath());

by GroovyScript

see org.jenkinsci.plugins.scriptler.util.ScriptHelper

MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false, new StreamTaskListener(sos)));

Updated by Andreas Müller almost 2 years ago · 28 revisions