Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy Jenkins secret files

Tags:

jenkins

groovy

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? } 
like image 369
Humberd Avatar asked Mar 24 '18 02:03

Humberd


People also ask

Where are Jenkins secret files stored?

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.

How do I use hidden files in Jenkins?

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.

How do I download Jenkins credentials?

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.


2 Answers

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" } 
like image 54
Humberd Avatar answered Sep 17 '22 06:09

Humberd


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)     } 
like image 25
Vahe Gharibyan Avatar answered Sep 20 '22 06:09

Vahe Gharibyan