Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xiaojueguan/f5f5e9bf9a3f7dd0df232733a1893c88 to your computer and use it in GitHub Desktop.
Save xiaojueguan/f5f5e9bf9a3f7dd0df232733a1893c88 to your computer and use it in GitHub Desktop.
jenkins script get credentials to json string
import groovy.json.JsonOutput
def get_credentials() {
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
Jenkins.instance,
null,
null
)
credentials = []
for(c in creds) {
def cred = [:]
if(c instanceof com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey){
cred['type'] = 'BasicSSHUserPrivateKey'
attrs = cred['attrs'] = [:]
attrs['id'] = c.id
attrs['desc'] = c.description
attrs['privateKeys'] = c.privateKeySource.getPrivateKeys()
attrs['passphrase'] = c.getPassphrase()
attrs['privateKey'] = c.getPrivateKey()
attrs['isUsernameSecret'] = c.isUsernameSecret()
attrs['username'] = c.getUsername()
attrs['scope'] = c.getScope()
}
if (c instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl){
cred['type'] = 'UsernamePasswordCredentialsImpl'
attrs = cred['attrs'] = [:]
attrs['id'] = c.id
attrs['desc'] = c.description
attrs['username'] = c.username
attrs['password'] = c.password
attrs['isUsernameSecret'] = c.isUsernameSecret()
attrs['scope'] = c.getScope()
}
credentials.add(cred)
}
return JsonOutput.toJson(credentials)
}
print(get_credentials())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment