Home > database >  Jenkins: How to access a secret file?
Jenkins: How to access a secret file?

Time:01-18

I'm trying to get my head around Jenkins, I've uploaded a secret file to credentials and made it global but I can't seem to use it in my stage.

I'm trying to just echo out the file for now

steps { echo("My File: ${credentials('local_instance')}") }

but in the log I don't see the contents of the file just My File: @credentials(<anonymous>=local_instance). I need to be able to get the contents of the file and write it to a file named local.env, or as I uploaded it with local.env can I just pull the file and have it stored in my root?

CodePudding user response:

The proper way to get access to credentials in Jenkins is withCredentials step.

   withCredentials([file(credentialsId: 'youCredId', variable: 'secretFile')]) {
     // do something with the file, for instance 
     sh 'cat $secretFile'
}

Where youCredId your file credential ID in Jenkins.

More information is here: https://www.jenkins.io/doc/pipeline/steps/credentials-binding/

There is another approach. In environment section of your declarative pipeline, you can assign an env variable with a credential value.

environment {
    SECRET_FILE = credentials('youCredId')
}

But the first approach is more preferable because it allows you to reduce the scope of secret variable.

More information is here: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#handling-credentials

  • Related