Creating jenkins jobs from a gradle task


(Phil Swenson) #1

It seems to me creating Jenkins jobs to run gradle tasks (we have 20 different testing tasks) is a tedious, manual, very un-DRY process.

I know there is a Jenkins API. Has anyone tried creating Jenkins tasks via gradle?


(Justin Ryan) #2

A few people have been working on a DSL to create Jenkins jobs, which could be run from Gradle or from inside Jenkins itself. It’s not released yet, but it is functional:

https://github.com/JavaPosseRoundup/job-dsl-plugin

Expect something in the coming weeks. The focus right now is Jenkins integration with Gradle to follow later. Please read over what documentation we have and we’d be happy to hear any feedback you have.


(Michael Brand) #3

I’ve written a script that creates number of custom configured Jenkins jobs based on project parameters. The script interacts with the Jenkins via HttpBuilder and the Jenkins REST API. The key methods for this were:

def getConfig(job) {

getHttp().get( path : “/jenkins/job/$job/config.xml”) { resp, reader ->

def builder = new StreamingMarkupBuilder()

String xmlString = builder.bind{mkp.yield reader}

def project = new StringBuilder()

reader.each {

project << XmlUtil.serialize(it)

}

return project.toString()

}

}

def postConfig(job, config) {

return getHttp().request(POST, XML) {

uri.path = “/jenkins/createItem”

uri.query = [name: job]

body = config

response.success = { resp ->

logger.info “Job create/update response status: ${resp.statusLine}”

logger.info “response: $resp”

assert resp.statusLine.statusCode == 200

return resp

}

}

}

I built up the Jenkins config XML from a template file and relevant parameters:

def getConfigXml() {

def recipientsList = with {

switch(true) {

case {recipients instanceof Collection}: return recipients.join(",")

case {!recipients}: return “”

default: return recipients

}

}

def engine = new GStringTemplateEngine()

def template = engine.createTemplate(jobTemplate).make(description: description,

streamName: streamName, wallDisplayName: wallDisplayName,

buildType: buildType, recipients: recipientsList, tasks: tasks.join(" "),

childProjects: childProjects, timerTrigger: getTimerTrigger(),

scmTrigger: getScmTrigger())

return template.toString()

}

def setup() {

def configXml = getConfigXml()

logger.info “Creating or updating Jenkins job: $jobName”

logger.info “Jenkins configuration:\n$configXml”

new JenkinsConnection().postConfig(jobName, configXml)

}

I’m not sure if this is any better than Justin’s stuff, but hopefully this will help if you decide to write your own.