I have already added 2 secret files to Jenkins credentials with names PRIVATE-KEY
and PUBLIC-KEY
. How can I copy those 2 files to /src/resources
directory inside a job?
I have the following snippet
withCredentials([file(credentialsId: 'PRIVATE_KEY', variable: 'my-private-key'), file(credentialsId: 'PUBLIC_KEY', variable: 'my-public-key')]) { //how to copy, where are those files to copy from? }
Encryption of Secrets and Credentials. Jenkins uses AES to encrypt and protect secrets, credentials, and their respective encryption keys. These encryption keys are stored in $JENKINS_HOME/secrets/ along with the master key used to protect said keys.
Secret file - click the Choose file button next to the File field to select the secret file to upload to Jenkins. SSH Username with private key - specify the credentials Username, Private Key and optional Passphrase into their respective fields.
On the Configuration interface, under Build Environment, select Use secret text(s) or file(s). Click Add -> Secret file. This creates a new Secret file binding. Select Specific credentials, then from the drop-down menu below it select the secret file you would like to retrieve.
Ok, I think I managed to do it. my-private-key
variable is a path to the secret, so I had to copy that secret to the destination I needed.
withCredentials([file(credentialsId: 'PRIVATE_KEY', variable: 'my-private-key'), file(credentialsId: 'PUBLIC_KEY', variable: 'my-public-key')]) { sh "cp \$my-public-key /src/main/resources/my-public-key.der" sh "cp \$my-private-key /src/main/resources/my-private-key.der" }
Both solution is good for specific OS
(win, unix). There are some basic function to check is system unix isUnix()
. Instead of this you can use the read/write basic methods for any machine.
withCredentials([file(credentialsId: PRIVATE_KEY, variable: 'my_private_key'), file(credentialsId: PUBLIC_KEY, variable: 'my_public_key')]) { writeFile file: 'key/private.pem', text: readFile(my_private_key) writeFile file: 'key/public.pem', text: readFile(my_public_key) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With