How to copy Jenkins secret files

限于喜欢 提交于 2021-02-06 09:44:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!