My application using jks type keystore file. I have to secure it using jenkins credentials parameter’s secret file. Write file using below code to jenkins workspace.
withCredentials([file(credentialsId: 'KEYSTORE_FILE', variable: 'keystore_file')]) { writeFile file: 'keystore/keystore.jks', text: readFile(keystore_file) }
But keystore corrupted by above way. throw Invalid keystore format error
Caused by: java.io.IOException: Invalid keystore format at java.base/sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:659) at java.base/sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:222) at java.base/java.security.KeyStore.load(KeyStore.java:1479) at org.springframework.security.saml.key.JKSKeyManager.initialize(JKSKeyManager.java:117)
Please help me to write keystore file into jenkins workspace without corrupting it.
Advertisement
Answer
You have to copy the file by cp command. Using writeFile/readFile loses the format.
sh "mkdir -p keystore/" withCredentials([file(credentialsId: 'KEYSTORE_FILE', variable: 'keystore_file')]) { sh "cp -f "${keystore_file}" "keystore/keystore.jks"" }
Notice that we need to create before the path with the mkdir command and inside the cp command we scape the quotes in order to avoid a “cp: target ‘…’ is not a directory” error.