Project

General

Profile

Actions

JenkinsGroovy » History » Revision 17

« Previous | Revision 17/28 (diff) | Next »
Andreas Kohlbecker, 08/20/2013 02:36 PM


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://wp5.e-taxonomy.eu/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://wp5.e-taxonomy.eu/mavenrepo/eu/etaxonomy/cdm-server/maven-metadata.xml")
def list = []
metadata.versioning.versions.version.each{
  list.add(it)
}
return return list.reverse(true)

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

Updated by Andreas Kohlbecker over 10 years ago · 17 revisions