Jenkins: How to Analyze Your Plugin Usage
Ever wondered whether plugins in your Jenkins installation are really used at all? Or do you need to know what plugins you need to install, to take over some existing jobs from another Jenkins instance? Here is how you can find out about your plugin usage, and a tiny shell script to analyze your Jenkins configuration and get an index all the needed plugins.
#The XML Configuration Files
Jenkins stores all its configuration in a bunch of XML Files in the jenkins-home directory. The layout of the directory is described jenkins-ci: Administering Jenkins.
Here is an example of a job configuration file, that contains a system groovy script:
<?xml version='1.0' encoding='UTF-8'?> |
The interesting part is the plugin="groovy@1.27"
attribute, which contains the plugin name and the version of it.
This information is used by jenkins to warn about missing plugins and to migrate older configuration versions
to new formats.
So why not parse the XML files and extract the information?
#List Configuration Files with Plugin Usage Information
For extracting the information from the XML files we use the shell script tools xml2
and awk
. The xml2
utility
converts an XML file to a simple line by line representation so you can parse it with standard unix tools like grep
and awk
.
The xml2
output of the above file is:
/project= /project= ... stripped standard job configuration parameters ... /project= /project/builders/hudson.plugins.groovy.SystemGroovy/@plugin=groovy@1.27 /project/builders/hudson.plugins.groovy.SystemGroovy/scriptSource/@class=hudson.plugins.groovy.StringScriptSource /project/builders/hudson.plugins.groovy.SystemGroovy/scriptSource/command= ... groovy code left out ... /project/builders/hudson.plugins.groovy.SystemGroovy/scriptSource/command= |
Next we use awk
to extract only the plugin information:
$ cat config.xml | xml2 | awk -F= '$1 ~ /^.*@plugin/ { print $2; }' groovy@1.27 |
Now let’s scan through all XML files of a Jenkins installation and extract the plugin information.
cd $JENKINS_HOME |
This tiny script produces a Markdown formatted output with configuration file as headline and a list of the used plugins. Example output:
## cloudbees-disk-usage-simple.xml |
#Plugin Usage Index
The above result is not too bad. However, we want to know which plugin is used by which jobs. So let’s reverse the
index. The script is actually almost the same, we just need to twist the list output of the inner awk
script:
cd $JENKINS_HOME |
The result:
## analysis-collector@1.45 |
#Missing Information with Workflow
In the above example the job build=cache2k
is a Maven job. But, with the new Jenkins Workflow plugin, things change
how you define your Jenkins jobs. Instead of a structured XML file, which contains the plugin configuration,
with workflow, a job is just a groovy script, or, to be more precise, a groovy-ish Workflow DSL. So, no plugin
information any more, just script code.
It should be technically feasible to log the information of the used plugins at runtime. For this I opened JENKINS-31582, please vote for it.
#Alternatives
There is a Plugin Usage Plugin in commercial CloudBees Jenkins Platform offering. There is also Plugin Usage Plugin Community)
While these plugins are useful, and just run OOTB, I think using the presented approach above has some values of its own:
- easily analyze a Jenkins configuration without running
- Simple customizable text output, to be included in an infrastructure documentation