Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Jenkins Plugin licenses

Get List of Jenkins Plugin Licenes

The following script provides you the licences of Jenkins plugins (e.g. for Open Source compliance checks) read from their pom.xml file on the respective Github repository.

Before using the script, you have to

  1. replace your.jenkins.host.com with the domain of your Jenkins server
  2. create a Jenkins API token for your user and put it into ~/.jenkins-api-token

Afterwards you can run the script with

./jenkins-plugin-licenses.sh > output.txt

The output format will be Confluence markdown - it can easily be adopted to any other style.

#!/usr/bin/env bash

## Adapt to your Jenkins host
jenkins_host="your.jenkins.host.com"

## Generate an API token for your Jenkins user
jenkins_api_token="$(cat ~/.jenkins-api-token)"

tmpfile=$(mktemp /tmp/jenkins-plugins.XXXXXX)

read -p "jenkins user: " jenkins_user

curl -u "${jenkins_user}:${jenkins_api_token}" "https://${jenkins_host}/pluginManager/api/json?depth=1" | jq ".plugins" > ${tmpfile}

# cat ${tmpfile} | jq .

echo "|| Plugin Name || Short Name || Version || Jenkins / Github URL || License ||"
cat ${tmpfile} | jq ".[] | {longName: .longName, shortName: .shortName, url: .url, version: .version}" | jq -s . | jq -r '.[]|[.longName, .shortName, .version, .url] | @tsv' |
  while IFS=$'\t' read -r long short version url; do
      github_url=$(curl "https://plugins.jenkins.io/${short}" | egrep -o "(http(s)?://github.com){1}[^'\"]+" | head -1)
      raw_content_url=$(echo $github_url | sed s/github.com/raw.githubusercontent.com/g | sed s/blob\\///g)
      pom_content=$(curl "${raw_content_url}/master/pom.xml")
      license_name=$(echo "$pom_content" | xpath '//project/licenses/license/name/text()')
      license_url=$(echo "${pom_content}" | xpath '//project/licenses/license/url/text()')
      echo "| ${long} | ${short} | ${version} | ${url} ${github_url} | [${license_name}|${license_url}] [pom.xml|${raw_content_url}/master/pom.xml] |"
  done
@pmgupte

This comment has been minimized.

Copy link

@pmgupte pmgupte commented Sep 10, 2019

lovely! I customized this to download update-center json and then run jq and xpath to get license name, and finally do sort | uniq | sort -bgr to get count of plugins by license. Here's my version of script: https://gist.github.com/pmgupte/43f8f8b782b839fd648e13637317cd18

@thiru9054

This comment has been minimized.

Copy link

@thiru9054 thiru9054 commented Feb 18, 2020

This script is very useful. I had faced a couple of issues Since SSL is not enabled for my jenkins server I had updated the Jenkins URL to use HTTP instead of Https
Modified this line from curl -u "${jenkins_user}:${jenkins_api_token}" "https://${jenkins_host}/pluginManager/api/json?depth=1" | jq ".plugins" > ${tmpfile} to ->
curl -u "${jenkins_user}:${jenkins_api_token}" "http://${jenkins_host}/pluginManager/api/json?depth=1" | jq ".plugins" > ${tmpfile}
The second issue is forward slash was missing when curl command was executed to identify the github_url
I changed this line from github_url=$(curl "https://plugins.jenkins.io/${short}" to -> github_url=$(curl "https://plugins.jenkins.io/${short}/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.