Skip to content

Instantly share code, notes, and snippets.

@WadeBarnes
Last active May 1, 2024 19:31
Show Gist options
  • Save WadeBarnes/a0642e9976f28b7b125441362a404d71 to your computer and use it in GitHub Desktop.
Save WadeBarnes/a0642e9976f28b7b125441362a404d71 to your computer and use it in GitHub Desktop.
Selectivity extract credentials from Jenkins
def credentialId='credential-id-goes-here'
import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.Credentials
import com.cloudbees.plugins.credentials.domains.Domain
import jenkins.model.Jenkins
def indent = { String text, int indentationCount ->
def replacement = "\t" * indentationCount
text.replaceAll("(?m)^", replacement)
}
Jenkins.get().allItems().collectMany{ CredentialsProvider.lookupStores(it).toList()}.unique().forEach { store ->
Map<Domain, List<Credentials>> domainCreds = [:]
store.domains.each { domainCreds.put(it, store.getCredentials(it))}
if (domainCreds.collectMany{ it.value}.empty) {
return
}
def shortenedClassName = store.getClass().name.substring(store.getClass().name.lastIndexOf(".") + 1)
println "Credentials for store context: ${store.contextDisplayName}, of type $shortenedClassName"
domainCreds.forEach { domain , creds ->
println indent("Domain: ${domain.name}", 1)
creds.each { cred ->
if (cred.id =="${credentialId}"){
cred.properties.each { prop, val ->
println indent("$prop = \"$val\"", 2)
}
println indent("-----------------------", 2)
}
}
}
}
@WadeBarnes
Copy link
Author

Modified version of script from here; https://devops.stackexchange.com/a/8692

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment