Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to bulk update Jenkins projects?

Tags:

jenkins

We have hundreds of Jenkins projects (mostly created from a few templates), often need to make the same change to all of them. e.g. today I need to add a post-build step to delete workspace at the end. Next I need to change the step to copy build result to a shared drive to Nexus repository.

What's the best way to apply such kind of bulk change to Jenkins projects?

like image 989
Jirong Hu Avatar asked May 25 '16 15:05

Jirong Hu


People also ask

How do I clean up my Jenkins build?

To clean up the workspace before build: Under Build Environment, check the box that says Delete workspace before build starts. To clean up the workspace after the build: Under the heading Post-build Actions select Delete workspace when build is done from the Add Post-build Actions drop down menu.

How do you change the number of builds kept by Jenkins?

This is a feature that is given to you by Jenkins so you can change the number of builds you want to keep. If you are using a Declarative pipeline you can just add this to your code: pipeline { options { buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30')) } ... }

How do I clean my Jenkins master?

Login to Jenkins, click on “Manage Jenkins” > “Manage Plugins” > Click on the “Available” tab then search for “workspace cleanup“. You will see various plugins listed. Click on the checkbox for “Workspace Cleanup“, plugin then click on install without reboot tab below the page.


2 Answers

You could use Configuration Slicing Plugin which is designed to do this.

It supports many configuration options.

like image 94
R. Oosterholt Avatar answered Oct 23 '22 18:10

R. Oosterholt


Groovy is by far the best way to bulk update jobs. You may have to do a little digging into the jenkins / plugin api to figure out what api calls to make, but the script console (http://yourJenkinsUrl/script) provides an easy way to play around with the code until you get it right.

To get you started, you can add / remove post-build steps by calling the getPublishersList() method on a job and then calling the add / remove methods.

def publishersList = Jenkins.instance.getJob("JobName").getPublishersList()
publishersList.removeAll { it.class == whatever.plugin.class }
publishersList.add(new PluginConstructor())

If you're not sure what publisher class you need to delete the workspace, I would suggest manually adding the desired configurations to one job, and then run getPublishersList() from the script console on that job. You will see the class you are working with in the list, and then you can go look at the api to see what is required to construct it.

You can then iterate through all your jobs and add the publisher doing something like this:

Jenkins.instance.getView("All Jobs").items.each { job ->
    //Maybe some logic here to filter out specific jobs

    job.getPublishersList().add(new PluginConstructor())
}

Alternatively, you can use the Jenkins CLI api or the REST api, but in order to update post-build actions, you will have to modify the project configuration xml file (which isn't trivial programmatically configure) and then overwrite the job configuration with the new configuration file.

like image 34
TheEllis Avatar answered Oct 23 '22 17:10

TheEllis