Jenkins plugin: create new work programmatically

How to create a new Jenkins work in a plugin?

I have a Jenkins plugin that listens for a message queue and when a message arrives, fires a new event to create a new job (or fires a launch).

I am looking for something like:

Job myJob = new Job(...); 

I know that I can use the REST API or CLI, but since I'm in the plugin, I would use an internal Java solution.

+6
source share
2 answers

You can create a new hudson / jenkins work by simply doing:

 FreeStyleProject proj = Hudson.getInstance().createProject(FreeStyleProject.class, NAMEOFJOB); 

If you want to be able to handle updates (and you already have config.xml ):

 import hudson.model.AbstractItem import javax.xml.transform.stream.StreamSource import jenkins.model.Jenkins final jenkins = Jenkins.getInstance() final itemName = 'name-of-job-to-be-created-or-updated' final configXml = new FileInputStream('/path/to/config.xml') final item = jenkins.getItemByFullName(itemName, AbstractItem.class) if (item != null) { item.updateByXml(new StreamSource(configXml)) } else { jenkins.createProjectFromXML(itemName, configXml) } 

Make sure that you have the main .jar file before doing this.

+1
source

Use the DSL Performance Plugin .

On the plugin page:

Jenkins is a great build management system and people like to use their user interface to set up jobs. Unfortunately, as the number of jobs grows, saving them becomes tedious, and the paradigm of using the user interface falls apart. In addition, the overall picture in this situation is to copy tasks to create new ones; these โ€œchildrenโ€ have a habit of diverging from their original โ€œtemplateโ€, and therefore it becomes difficult to maintain consistency between these tasks.
Jenkins job-dsl-plugin tries to solve this problem by allowing jobs to be defined with the absolute minimum required in software form using templates that synchronize with the generated jobs. The goal is for your project to be able to identify all the tasks that they want to associate with their project, declaring their intention to complete the tasks, leaving things common to templates that were previously defined or were hidden behind DSL.
+5
source

All Articles