jenkinsjenkins-pipelinejenkins-groovycicdjenkins-cli

Jenkins SecretFile Decrypt


Normally, I can decrypt a password or a secret-text like below

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null
);
for (creds in jenkinsCredentials) {
if(creds.id == 'XYZ'){ 
for password-------> println(creds.password)  
-----OR------  
for secret-text-----> println(creds.secret) 
                           }
}

But I could not find a way to decrypt a secret-file with this method. When I need to get the last version of the replaced file this becomes a problem. How can I get the uploaded or the replaced last version secret file that is registered in Jenkins credentials?


Solution

  • I have found the working function format for the secretFile

    import jenkins.*
    import jenkins.model.*
    import hudson.*
    import hudson.model.*
    import org.apache.commons.io.IOUtils
    def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.Credentials.class,
    Jenkins.instance,
    null,
    null
    );
    for (creds in jenkinsCredentials) {
    if(creds.id == 'XYZ'){ 
    println(IOUtils.toString(creds.getContent(), "UTF-8"))  
                         }
    }
    

    So, the new println line works after the fuction add