Search code examples
jenkinsgroovydevops

'pega_db_password' is of type 'Username with password' where 'org.jenkinsci.plugins.plaincredentials.StringCredentials' was expected


I was looking at example of reading a hidden password from Jenkins Credentials plugin and I tried to do this quick example and see how I can now use it in my script. I got this error

'pega_db_password' is of type 'Username with password' where 'org.jenkinsci.plugins.plaincredentials.StringCredentials' was expected

The script is below:

withCredentials([string(credentialsId: 'pega_db_password', variable: 'pega_db_password')]) {
        sh 'echo $pega_db_password'
      
      }

The settings is

WithCredentials Settings

I have tried to research this and not able to resolve this. I really need to use this just to put the password in a file inside the node agent.


Solution

  • There are 2 changes required:

    1. Please tick the check box for "Treat username as secret"

    2. Change the pipeline script to something like this:

    withCredentials([usernamePassword(credentialsId: 'pega_db_password', usernameVariable: 'PEGA_DB_USERNAME', passwordVariable: 'PEGA_DB_PASSWORD')]) {
        sh 'echo $PEGA_DB_PASSWORD' // warning will be thrown by jenkins
      
      }
    

    Further examples of credentials plugin for Jenkins can be found here.