Created
January 8, 2024 23:11
-
-
Save ethanhinson/995533f6382cfb85021f189f50a1d530 to your computer and use it in GitHub Desktop.
Extract secrets stored in Jenkins credentials.
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.plaincredentials.StringCredentials | |
import com.cloudbees.plugins.credentials.CredentialsProvider | |
import com.cloudbees.plugins.credentials.domains.DomainRequirement | |
import groovy.json.JsonBuilder | |
import jenkins.model.Jenkins | |
import hudson.model.Job | |
String jobName = "YourFolder/YourMultibranch/YourJob" | |
Job job = Jenkins.instance.getItemByFullName(jobName, Job.class) | |
if (job == null) { | |
println("Job not found: $jobName") | |
return | |
} | |
def creds = CredentialsProvider.lookupCredentials( | |
StringCredentials.class, // Class for secret text credentials, you can use different classes for different contexts/secret types. | |
job, | |
null, | |
new ArrayList<DomainRequirement>() | |
) | |
def jsonBuilder = new JsonBuilder() | |
def credentialsMap = [:] | |
creds.each { c -> | |
if (c instanceof StringCredentials) { | |
credentialsMap[c.id] = c.getSecret().getPlainText() | |
} | |
} | |
jsonBuilder(credentialsMap) | |
println(jsonBuilder.toPrettyString()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment