I’m seeking some advice on how to write a plugin that extends an existing plugin and automatically configures that plugin’s extension object. I wish I started this work last month so I could have asked some of the Gradleware developers while attending the Gradleware summit (which was great, btw), but hopefully this forum will suffice.
What I’m trying to do is automatically configure the jenkins plugin (found here: https://github.com/ghale/gradle-jenkins-plugin)
The jenkins plugin provides a mechanism for generating jenkins job configuration xml files, as well as tasks for uploading those configurations to a server. The plugin provides a DSL for defining jenkins server and the job definitions.
A build.gradle file using this plugin might look like this:
jenkins {
servers {
main {
url 'http://localhost:7070'
}
}
defaultServer servers.main
jobs {
myJob {
definition {
xml {
// xml is a markup builder closure
doit()
}
}
}
}
}
What I would like to do is write a plugin that automatically configures the jenkins plugin job definitions based on my own model provided by my plugin.
For example, I’ve written a “base” plugin which defines a ProductDefinition and I attach that as an extension object to the project, allowing me to have my build.gradle file look like this:
apply plugin: 'myPlugin'
product {
team = ["dev-team@myCompany.com"]
branches {
// main (this is created by default. don't need to put it here
featureA
release
}
}
In the case above, what I would like to do is create a series of jenkin’s job derived from my above model. My model is very simple at this point as I’m just getting started. I have a product. A product is associated with a team and can have a number of branches. Each branch can have it’s own team, or it can use the main project team. By default, my plugin creates a main branch. There can be others.
To simply the use case, for each of these branches, I want to create 1 jenkin’s job and I want my product/branch model to be used to derive the configuration of the jenkin’s job. One example is that I want to configure the notification of the job such that build failures get sent via email to the team associated with the branch being built.
I took some stabs at getting this to work, but have not had any luck. Here is the latest in what I tried to do:
public class ProductJenkinsPlugin implements Plugin<Project> {
Project project
public void apply(Project project) {
this.project = project
// This is my plugin that defines a 'product' model
project.getPlugins().apply(ProductBasePlugin.class);
// This is the plug that provides the 'jenkin's model that I want to automatically configure based on my product model.
project.getPlugins().apply(JenkinsPlugin.class);
project.afterEvaluate {
createJenkinsJobs()
}
}
final void createJenkinsJobs() {
def jobs = project.container(com.terrafolio.gradle.plugins.jenkins.JenkinsJob)
project.product.branches.all { ProductBranch branch ->
def jobName = "${project.product.name}-${branch.name}-commit"
def job = jobs.create(jobName)
println "Adding job: ${job.name}"
// TODO: what is the proper way to configure the this new jenkins job
}
}
}
Then my project build.gradle looks like this:
product {
name = "myproduct"
team = ["dev-team@myCompany.com"]
branches {
// main (this is created by default. don't need to put it here
featureA
release
}
}
jenkins {
servers {
dummy {
url 'http://localhost:7070'
}
}
defaultServer servers.dummy
jobs {
blah {
definition {
xml {
doit()
}
}
}
}
}
task doIt << {
// diagnostic stuff
println "${product.name} Branches:"
product.branches.each { branch ->
println "${branch.name} - team: ${branch.team}"
}
println "jenkins plugin jobs:"
jenkins.jobs.each { job ->
println "${job.name}"
}
}
Running the doIt task provides the following output:
Adding job: myproduct-featureA-commit
Adding job: myproduct-release-commit
Adding job: myproduct-trunk-commit
:apps:gpserver:doIt
myproduct Branches:
featureA - team: [dev-team@myCompany.com]
release - team: [dev-team@myCompany.com]
trunk - team: [dev-team@myCompany.com]
jenkins plugin jobs:
blah
So I can see that the code in my plugin is adding jenkin’s job’s, but when I run my diagnostic ‘doIt’ task to iterate over the jenkin’s job, all I see is the one i explicitly defined in my build.gradle file (called blah). The ones I dynamically created from my model are not showing up.
What is the best way to accomplish what I’m trying to do?
Thanks in advance.
Doug