Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcosborges/b32b5dc449b7e81d5e4a8125079af6ec to your computer and use it in GitHub Desktop.
Save marcosborges/b32b5dc449b7e81d5e4a8125079af6ec to your computer and use it in GitHub Desktop.
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