Search code examples
jenkinsdevopscredentialsjenkins-job-dsl

credential issues with curl in job dsl


I'm looking to get a get response on different jenkins pipelines to recreate them using job dsl plugin, but I'm facing issues with the credentials, so far i have been using the logic below but if trying to use the jenkins credentials in credentialsBinding, it fails to recognize them, if I use my own user and password it works fine

this is the logic im looking to implement

job('seed'){
  wrappers {
    credentialsBinding {
      usernamePassword('USER','PASSWORD', 'credentials')
                       }
            }
    label('centos')

    def confXml = "curl -s -XGET ${url} -u \$USER:\$PASSWORD".execute().text.replace("\n", "")
    //do something with the respose
    //recreate dsl after checking an attribute in the response
    pipelineJob("Sandbox_pipelines/pipelineName") {                                   
    definition {
            cpsScm {
                 scm {
                      git(repo_git, "master")
                      }                                      
          scriptPath("somepath")
                   }
               }             
           }
    }

when i run this job this should be creating the other pipelines, please let me know if you can help me on this. Thanks in advance


Solution

  • The issue is that credentialsBinding loads the credentials during the build of the job being created. You want to use the credential to decide what to create and that's just not how it works.

    You can use withCredentials though:

    def confXml
    withCredentials([usernameColonPassword(credentialsId: 'credentials', variable: 'USERPASS')]) {
        confXml = "curl -s -XGET ${url} -u \$USERPASS".execute().text.replace("\n", "")
    }