Skill Sharing of Jenkins Task Batch Modification

  • 2021-07-03 00:02:03
  • OfStack

Batch modifying Jenkins tasks through script command line

Recently, the server where Jenkins of my team is located often reports that there is insufficient hard disk space. After investigation, it is found that "discard old builds" is not set for many tasks. Notifying all teams to check whether their Jenkins tasks are set to discard old builds is somewhat unrealistic.

The first thing that came to mind was to use API of Jenkins to implement batch modification of all Jenkins tasks. The author is not satisfied with this solution. After Google, I found that some students and I encountered the same problem. He uses a more "tricky" approach: Operate the Jenkins task by executing Groovy code on the Jenkins script command line.

Generally speaking, there are two steps:

Enter the menu: System management- > Script command line

In the input box, paste the following code:


import jenkins.model.Jenkins
import hudson.model.Job
import jenkins.model.BuildDiscarderProperty
import hudson.tasks.LogRotator
//  Traverse all tasks 
Jenkins.instance.allItems(Job).each { job ->

if ( job.isBuildable() && job.supportsLogRotator() && job.getProperty(BuildDiscarderProperty) == null) {
 println " \"${job.fullDisplayName}\"  In process "

 job.addProperty(new BuildDiscarderProperty(new LogRotator (2, 10, 2, 10)))
 println "$job.name  Updated "
}
}
return;

/**

LogRotator The construction parameters are: 
daysToKeep: If not -1, history is only kept up to this days.
numToKeep: If not -1, only this number of build logs are kept.
artifactDaysToKeep: If not -1 nor null, artifacts are only kept up to this days.
artifactNumToKeep: If not -1 nor null, only this number of builds have their artifacts kept.
**/

Script

Introduction to script command line

Script command line (Jenkins Script Console), which is a feature of Jenkins and allows you to execute any Groovy script in the runtime environment of Jenkins master and Jenkins agent. This means that we can do anything from the script command line, including shutting down Jenkins, executing the operating system command rm-rf/(so we can't use root users to run Jenkins agent) and other dangerous operations.

In addition to the above, using the interface to execute the Groovy script, it can also be executed through Jenkins HTTP API:/script. Please refer to the official documents for specific operation.

Question: After the code execution is completed, are the modifications to the tasks persisted?

When our code job. addProperty (new BuildDiscarderProperty (new LogRotator (2, 10, 2, 10)) is executed, is this modification persisted to the file system or not (all configurations of Jenkins are persisted to the file system by default)? Let's look at the source code of hudson. model. Job. There is persistence behind the addProperty method:


public void addProperty(JobProperty<? super JobT> jobProp) throws IOException {
((JobProperty)jobProp).setOwner(this);
properties.add(jobProp);
save();
}

Summary

This article only introduces the batch modification of the configuration of "discard old builds". If you want to modify other configurations, you can refer to hudson. model. Job source code.
I have to remind readers that the command line of Jenkins script is a double-edged sword. Please consider the scope of influence before you operate it. If necessary, please make a backup in advance.

Summarize


Related articles: