How to extract plugin version compatibility details with Jenkins

How to extract plugin version compatibility details with Jenkins

jenkins-plugin-new

Here in this article we will try to extract the plugin version and also the required jenkins version for that plugin to operate in a compatible mode from the jenkins plugin inventory using a python script.

Test Environment

Fedora 35 workstation

Python 3.x

What is Jenkins

Jenkins is the most popular opensource build automation tool which is widely used by many organisation to automate build and deployment cycle. There are a lot of built in and third party plugins available which provide additional features and integration capabilities by extending Jenkins. Most of these plugins evolve by adding new enhancements and features with new versions made available for use. But each plugin version may not be compatible with the currently installed version on Jenkins.

Let’s get started.

If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.

Procedure

Step1: Create a list of plugins

As per the list of plugins that you are currently using in your organisation you can create a list similar to below with comma separated plugin name and url.

Here is the Jenkins Plugin Portal.

[admin@fedser plugin_matrix]$ cat plugin.list 
Plugin,URL
ant,https://updates.jenkins.io/download/plugins/ant/
build-name-setter,https://updates.jenkins.io/download/plugins/build-name-setter/
build-pipeline-plugin,https://updates.jenkins.io/download/plugins/build-pipeline-plugin/
build-timestamp,https://updates.jenkins.io/download/plugins/build-timestamp/
clone-workspace-scm,https://updates.jenkins.io/download/plugins/clone-workspace-scm/
credentials,https://updates.jenkins.io/download/plugins/credentials/
cvs,https://updates.jenkins.io/download/plugins/cvs/

Plugin name

Plugin version list

Step2: Extract Plugin Links

This python script takes the plugin url as an argument and get the list of all the versions that are available for that plugin and it prepares plugin download url for each plugin and writes it to a file named link.txt.

Here i am using requests module to get the plugin url content and then using the beautifulsoup module to extract the href items with attribute “class” set to value “version” and extracting and prepare the download url for each version of the plugin.

[admin@fedser plugin_matrix]$ cat plugin_links.py 
import sys
import requests
from bs4 import BeautifulSoup

baseurl = "https://updates.jenkins.io"
#pluginurl = "https://updates.jenkins.io/download/plugins/ant"
print(sys.argv[1])
pluginurl = sys.argv[1]

res = requests.get(pluginurl).text
soup = BeautifulSoup(res, 'html.parser')
#print(soup.prettify())
items = soup.find_all('a', attrs={"class":"version"})
#print(items)

for each in items:
    link = each.get("href")
    if "latest" in link:
        pass
    else:
        with open("link.txt", "a") as f:
            pluginlink = baseurl+link
            #print(pluginlink)
            f.write(pluginlink + "\n")

Step3: Extract Plugin version and Required Jenkins version

In our last step we were able to prepare the download url for each plugin version, Now let prepare our master script which take each specific plugin url from plugin.list file and passes it as an argument to the plugin_links.py script from extractLinks(). Once the plugin download links are prepared and written to the link.txt file we can iterate through this link.txt file one by one in the extractVersion() function and unzip the *.hpi plugin zip and get the Jenkins version from the MANIFEST.MF file as shown in the script below. Finally we are printing the specific plugin versio and required jenkins version for that plugin in the output screen.

[admin@fedser plugin_matrix]$ cat plugin_version.sh 
#!/bin/bash

# Extract and Fetch Plugin links
extractLinks()
{

	echo "python plugin_links $pluginurl"
	python plugin_links.py $pluginurl
	extractVersion

}

# Extract Compatible Jenkins version
extractVersion()
{

for eachLink in `cat ./link.txt`; do
	pluginver=`echo $eachLink | awk -F"/" '{print $(NF-1)}'`
	wget -q $eachLink -P extract
	unzip -qq extract/*.hpi -d extract
	jenkinsver=`cat extract/META-INF/MANIFEST.MF  | grep Jenkins-Version | awk -F ": " '{print $2}'`
	echo "$pluginver---$jenkinsver"
	rm -rf ./extract
done
rm -rf ./link.txt

}

# Main Section
for plugin in `cat ./plugin.list | grep -v "Plugin"`; do
	pluginurl=`echo $plugin | awk -F"," '{print $2}'`
	#echo $pluginurl
	extractLinks $pluginurl
done

Step4: Validating the Script Execution

Now we can execute the below script and see the output of the plugin version and the required jenkins version as shown below.

[admin@fedser plugin_matrix]$ ./plugin_version.sh
python plugin_links https://updates.jenkins.io/download/plugins/ant/
https://updates.jenkins.io/download/plugins/ant/
1.13---2.249.3
1.12---2.249.3
1.11---2.150.1
1.10---2.150.1
1.9---2.121.2
1.8---1.651.3
1.7---1.651.3
1.6---1.642.3
1.5---1.596.1
1.4---1.596.1
1.3---1.596.1
1.2---1.456
1.1---1.431
1.0---1.432
python plugin_links https://updates.jenkins.io/download/plugins/build-name-setter/
https://updates.jenkins.io/download/plugins/build-name-setter/
2.2.0---2.277.2
2.1.0---2.121.3
2.0.4---2.15
2.0.3---2.15
2.0.2---2.15
2.0.1---2.15
2.0.0---2.15
1.7.1---1.625.3
1.7.0---1.625.3
1.6.9---1.625.3
1.6.8---1.625.3
1.6.7---1.609
1.6.5---1.480.3
1.6.3---1.480.3
1.6.0---1.480.3
1.5.1---1.480.3
1.5---1.480.3
...

These details might help you in deciding which plugins can updated with actually going for a jenkins version upgrade. Also if a plugin upgrade requires you a specific version of jenkins installed then you will get to know about it in this output. Please note it is always a best practice and recommended to take the backup of your jenkins_home before going for any upgrade with respect to plugins or jenkins versions.

Hope you enjoyed reading this article. Thank you..