Search code examples
jenkinsgroovy

How to copy Jenkins secret files


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?
}

Solution

  • 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"
    }