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
- replace
your.jenkins.host.com
with the domain of your Jenkins server - 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
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}/"