... keeping update ...
Forked from marslo/# groovy scripts for jenkins credentials.md
Created
April 10, 2024 11:40
-
-
Save jimklimov/b1f223741ca42de5ece3b575ef6107bd to your computer and use it in GitHub Desktop.
jenkins universal credential closure
This file contains hidden or 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 com.datapipe.jenkins.vault.credentials.common.VaultSSHUserPrivateKeyImpl | |
import com.datapipe.jenkins.vault.credentials.common.VaultUsernamePasswordCredentialImpl | |
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl | |
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey | |
import com.cloudbees.plugins.credentials.CredentialsProvider | |
import com.cloudbees.plugins.credentials.common.StandardCredentials | |
def withCredential( String credentialsId, Closure body ) { | |
Closure foo = { cid -> | |
[ | |
( VaultSSHUserPrivateKeyImpl ) : { body([ username: cid.username, sshkey: cid.privateKeyKey ]) }, | |
( BasicSSHUserPrivateKey ) : { body([ username: cid.username, sshkey: cid.privateKeys.join('\n') ]) }, | |
( VaultUsernamePasswordCredentialImpl ) : { body([ username: cid.username, password: cid.passwordKey ]) }, | |
( UsernamePasswordCredentialsImpl ) : { body([ username: cid.username, password: cid.passwordKey ]) } | |
].find { it.key.isAssignableFrom( cid.getClass() ) }.value.call() | |
} | |
foo CredentialsProvider.lookupCredentials( StandardCredentials.class, jenkins.model.Jenkins.instance).find { credentialsId == it.id } | |
} | |
withCredential( 'BASIC_CREDENTIAL' ) { account -> | |
println "${account.password} -> ${account.username}" | |
} | |
withCredential( 'SSH_CREDENTIAL' ) { account -> | |
println "${account.sshkey} -> ${account.username}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment