简体   繁体   中英

How do I add a build step to a Jenkins job programmatically?

I am writing a Jenkins plugin, and I want it to create a new job (in the perform(...) method, as a build step).

I know that I can create a job like this:

 FreeStyleProject proj =
       Hudson.getInstance().createProject(FreeStyleProject.class, "New job");

and I can add properties to it using proj.addProperty(someJobProperty) .

How can I then also add a build step to the project, programmatically, like the properties? Specifically, I would like to add an Execute shell and Copy Artifact build step.

I have been looking through the Jenkins JavaDoc, especially the Job page, and I haven't been able to find anything that would help me.

The project has a method getBuildersList() which returns the list of all the build steps. You can add a build step to the project by simply adding a build step to the list returned by this method. The object you are adding to the list must be a Builder .

I did it like this (using the plugin from the Jenkins HelloWorld tutorial as an example):

proj.getBuildersList().add(new HelloWorldBuilder("Bobbly"));

This adds a Hello World build step to the project.

Similarly, there is a getPublishersList() method which returns the list of all the post-build steps and contains Publisher objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM