Last active
September 30, 2025 14:50
-
-
Save philippn/b4b86e3fb0b56b24156d411d13e2898c to your computer and use it in GitHub Desktop.
Jenkinsfile for extracting secret credentials from Jenkins
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
| 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