Created
October 11, 2022 05:13
-
-
Save xiaojueguan/f5f5e9bf9a3f7dd0df232733a1893c88 to your computer and use it in GitHub Desktop.
jenkins script get credentials to json string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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