If you need an overview of what credentials Jenkins uses to clone repositories, you'll find that the Jenkins API is a bit convoluted. This is what I did instead:
- From a Windows machine, access Jenkins as a UNC share and dump its contents:
Get-ChildItem -Path \\my-jenkins-instance\*\config.xml | ForEach-Object { $targetPath = "C:\Users\me\Desktop\jenkins\" + $_.Directory.Name + "_" + $_.Name; $_ | Copy-Item -Destination $targetPath -Force }
The output will be a zip file with XML files containing job configurations. The filename contains the project name (as that information is not otherwise contained in the XML file).
- From Linux, unzip the archive and run one of:
# To list which pipeline uses which credentials
grep -R '<credentialsId>'
# To list how often each credential occurs
xmllint --xpath '//hudson.plugins.git.UserRemoteConfig' *xml* --format 2>/dev/null | sort | uniq -c | sort -hr
# To list how often each (repo, credential) tuple occurs
xmllint --xpath '//hudson.plugins.git.UserRemoteConfig/credentialsId' *xml* --format 2>/dev/null | sort | uniq -c | sort -hr
This only works for credentials used to clone git repos and doesn't detect uses of a credential in the pipeline definition (i.e. groovy file).