Skip to content

Instantly share code, notes, and snippets.

@pwalkr
Created January 11, 2023 18:22
Show Gist options
  • Save pwalkr/3c0fe61464872fd83544cf0c801c7f6d to your computer and use it in GitHub Desktop.
Save pwalkr/3c0fe61464872fd83544cf0c801c7f6d to your computer and use it in GitHub Desktop.
Extract file credential from Jenkins using script console
import java.nio.charset.StandardCharsets
import org.apache.commons.io.IOUtils
// https://stackoverflow.com/questions/57107486/how-to-read-jenkins-credentials-at-folder-level
def folder = Jenkins.instance.getItems(com.cloudbees.hudson.plugins.folder.Folder).find({
// TODO handle sub-folders as required
it.getFullName() == 'folder-name'
})
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
// Or just Jenkins.instance for global credentials
folder,
null,
null
);
found = creds.find({
it.id == 'credentials-id'
})
println "$found.id\n${found.getFileName()}\n$found.description\n" + IOUtils.toString(found.getContent(), StandardCharsets.UTF_8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment