Skip to content

Instantly share code, notes, and snippets.

@Java4all
Last active July 12, 2025 00:45
Show Gist options
  • Save Java4all/0c9206c687633c2030650d5f523eb99b to your computer and use it in GitHub Desktop.
Save Java4all/0c9206c687633c2030650d5f523eb99b to your computer and use it in GitHub Desktop.
Plugins dependency check
# cli.py
import argparse
import os
from main import analyze_plugin
def main():
parser = argparse.ArgumentParser(
description="\U0001F9E9 Jenkins Plugin Compatibility Analyzer",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"plugin",
help="Plugin name optionally with version (e.g. git or git:4.4.6)"
)
parser.add_argument(
"--url",
default=os.getenv("JENKINS_URL", "http://localhost:8080"),
help="URL of Jenkins instance"
)
parser.add_argument(
"--user",
default=os.getenv("JENKINS_USER", "admin"),
help="Jenkins username"
)
parser.add_argument(
"--token",
default=os.getenv("JENKINS_TOKEN", "admin"),
help="Jenkins API token"
)
parser.add_argument(
"--update-center",
default=os.getenv("JENKINS_UPDATE_CENTER_URL"),
help="Custom Jenkins update center base URL"
)
args = parser.parse_args()
analyze_plugin(
plugin_string=args.plugin,
jenkins_url=args.url,
username=args.user,
token=args.token,
update_center_override=args.update_center,
)
if __name__ == "__main__":
main()
# main.py
import requests
import json
from packaging.version import Version, InvalidVersion
def fetch_jenkins_core_version(jenkins_url, username, token):
response = requests.get(f"{jenkins_url}/api/json", auth=(username, token))
response.raise_for_status()
return response.json().get("version")
def fetch_update_center_data(core_version, custom_url=None):
url = custom_url or f"https://updates.jenkins.io/update-center.json?version={core_version}"
response = requests.get(url)
response.raise_for_status()
raw = response.text
json_start = raw.find('{')
data = json.loads(raw[json_start:])
return data.get("plugins", {})
def analyze_plugin(plugin_string, jenkins_url, username, token, update_center_override=None):
if ":" in plugin_string:
plugin_name, plugin_version = plugin_string.split(":", 1)
else:
plugin_name, plugin_version = plugin_string, None
print(f"Fetching Jenkins core version from {jenkins_url} ...")
core_version = fetch_jenkins_core_version(jenkins_url, username, token)
print(f"\n✅ Jenkins Core Version: {core_version}\n")
print(f"Fetching plugin metadata for core version {core_version} ...")
plugin_metadata = fetch_update_center_data(core_version, update_center_override)
if plugin_name not in plugin_metadata:
print(f"\u274C Plugin '{plugin_name}' not found in update center.")
return
plugin_data = plugin_metadata[plugin_name]
latest_version = plugin_data["version"]
required_core = plugin_data.get("requiredCore")
if plugin_version:
if Version(core_version) < Version(required_core):
print(f"\u274C Plugin version {plugin_version} requires Jenkins >= {required_core}")
return
if plugin_version != latest_version:
print(f"⚠️ Version {plugin_version} is older than latest {latest_version}")
else:
plugin_version = latest_version
print(f"Using latest compatible version: {plugin_version}")
print(f"\n✅ Plugin '{plugin_name}' version {plugin_version} is compatible with Jenkins {core_version}\n")
print("Dependencies:")
deps = plugin_data.get("dependencies", [])
if not deps:
print(" - (none)")
for dep in deps:
print(f" - {dep['name']} (required: {dep['version']}, optional: {dep['optional']})")
# README.md
# \U0001F9E9 Jenkins Plugin Compatibility Analyzer (v2)
This CLI tool checks whether a new Jenkins plugin (and its dependencies) are compatible with your current Jenkins installation.
## ✅ Features
- Uses Jenkins core version as anchor for compatibility
- Dynamically queries update-center.json based on Jenkins core version
- Supports plugin version validation with requiredCore comparison
- Analyzes dependency impact on already-installed plugins
- CLI and environment variable support for flexibility
## ⚙️ Usage
```bash
python cli.py <plugin[:version]> [--url JENKINS_URL] [--user USER] [--token TOKEN] [--update-center URL]
```
### Examples
```bash
python cli.py git
python cli.py git:4.4.6 --url http://jenkins.local --user admin --token secret
```
### Environment Variables
- `JENKINS_URL` – Jenkins instance URL
- `JENKINS_USER` – Jenkins username
- `JENKINS_TOKEN` – Jenkins token/API key
- `JENKINS_UPDATE_CENTER_URL` – optional custom update center
## 📄 Output
The tool prints:
- Jenkins Core version
- Plugin installation candidate (version, if omitted will fetch latest compatible)
- Compatibility check summary
- Dependency tree with compatibility analysis
- Impact report (if any plugin will be upgraded)
## ✉️ Note
Only stable plugins from Jenkins update center are currently supported. Extendable for GitHub metadata in future.
---
MIT Licensed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment