Last active
June 29, 2022 11:12
-
-
Save marcosborges/b32b5dc449b7e81d5e4a8125079af6ec to your computer and use it in GitHub Desktop.
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 org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException | |
import hudson.util.Secret | |
import com.cloudbees.plugins.credentials.CredentialsScope | |
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl | |
import com.cloudbees.plugins.credentials.domains.Domain | |
node { | |
try{ | |
withCredentials([ | |
string(credentialsId: 'CREDENTIAL_A', variable: "CREDENTIAL_A"), | |
string(credentialsId: 'CREDENTIAL_B', variable: "CREDENTIAL_B"), | |
]) { | |
println "Credentials finded" | |
} | |
} catch (CredentialNotFoundException e) { | |
def askqued = input ( | |
id: "CREDENTIALS", | |
message: "Enter the credentials.", | |
parameters: [ | |
[ | |
$class: "StringParameterDefinition", | |
defaultValue: "", | |
description: "", | |
name: "CREDENTIAL_A" | |
],[ | |
$class: "StringParameterDefinition", | |
defaultValue: "", | |
description: "", | |
name: "CREDENTIAL_B" | |
] | |
] | |
) | |
def store = Jenkins.getInstance().getExtensionList ( | |
'com.cloudbees.plugins.credentials.SystemCredentialsProvider' | |
)[0].getStore() | |
for (resp in askqued) { | |
def secretText = new StringCredentialsImpl( | |
CredentialsScope.GLOBAL, | |
resp.key, | |
"", | |
Secret.fromString(resp.key) | |
) | |
store.addCredentials(Domain.global(), secretText) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment