Skip to content

Instantly share code, notes, and snippets.

@philippn
Last active September 30, 2025 14:50
Show Gist options
  • Select an option

  • Save philippn/b4b86e3fb0b56b24156d411d13e2898c to your computer and use it in GitHub Desktop.

Select an option

Save philippn/b4b86e3fb0b56b24156d411d13e2898c to your computer and use it in GitHub Desktop.
Jenkinsfile for extracting secret credentials from Jenkins
pipeline {
agent any
parameters {
string(name: 'NAME', defaultValue: 'xyz', description: 'The credential name')
}
stages {
stage('Extract creds') {
steps {
script {
File f = new File("creds.txt")
if (f.exists()) {
f.delete()
}
try {
withCredentials([file(credentialsId: NAME, variable: 'FILE')]) {
sh "cp \$FILE creds.txt"
sh "cat creds.txt"
}
} catch (err) {
echo err.getMessage()
}
try {
withCredentials([string(credentialsId: NAME, variable: 'MYCREDS')]) {
writeFile file: 'creds.txt', text: MYCREDS
sh "cat creds.txt"
}
} catch (err) {
echo err.getMessage()
}
try {
withCredentials([usernameColonPassword(credentialsId: NAME, variable: 'MYCREDS')]) {
writeFile file: 'creds.txt', text: MYCREDS
sh "cat creds.txt"
}
} catch (err) {
echo err.getMessage()
}
}
}
}
stage('Archive Artifacts') {
steps {
archiveArtifacts artifacts: 'creds.txt'
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment