问题
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?
}
回答1:
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"
}
回答2:
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)
}
回答3:
Following on from @Humberds answer, the equivalent for powershell
is:
withCredentials([file(credentialsId: 'PRIVATE_KEY', variable: 'my-private-key')]) {
bat "powershell Copy-Item $appSettings -Destination src\\main\\resources "
}
来源:https://stackoverflow.com/questions/49460520/how-to-copy-jenkins-secret-files