Creating jenkins jobs from a gradle task

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?

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:

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.

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 -> “Job create/update response status: ${resp.statusLine}” “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() “Creating or updating Jenkins job: $jobName” “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.