Shashi Kiran
Shashi Kiran

Reputation: 362

How to Set multiple credentials using withcredentials in jenkins pipeline groovy file

I need to set two or more credentials to a job, my plan is to use it separately like below, so that it can be used in multiple jobs

static void _artifactoryCredentialBinding(Job job) {
    job.with {
        wrappers {
            credentialsBinding {
                usernamePassword('USERNAME', 'PASSWORD', 'xxxxx')
            }
        }
    }
}

static void _jasyptCredentialBinding(Job job) {
    return job.with {
        wrappers {
            credentialsBinding {
                usernamePassword('', 'PASSWORD', 'jasypt-credentials')
            }
        }
    }
}

When I do this the first credential is getting over ridden by the second credential.

I will be calling these two methods as a helper method in where ever necessary in my groovy file.

I would require to add multiple credentials in few jobs and only one credential in a job.

Adding the credentials under one wrapper will work - multiple-credentials, but I will not be able to reuse if I add multiple under the same.

I tried returning the Job in the above methods and used the same methods to set the creds but getting the error while building -

ERROR: (CredentialBindingUtil.groovy, line 28) No signature of method: xxxx.CredentialBindingUtil$__pfJasyptCredentialBinding_closure3.wrappers() is applicable for argument types: (xxx.CredentialBindingUtil$__pfJasyptCredentialBinding_closure3$_closure9) values: [xxxx.CredentialBindingUtil$__pfJasyptCredentialBinding_closure3$_closure9@11b4d391] [Office365connector] No webhooks to notify

How do I make the credentials to be appended with the existing ones ?

Upvotes: 0

Views: 3697

Answers (1)

Toni Van de Voorde
Toni Van de Voorde

Reputation: 949

As discussed in the comments, it's possible to achieve this through the Configure Block.

static void _artifactoryCredentialBinding(def job) {
    job.with {
      configure { node ->

        node / 'buildWrappers' / 'org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper' / 'bindings' << 'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' {

          usernameVariable 'some-credential-id'
          credentialsId PASS1
          passwordVariable USER1

        }
      }
    }
}

static void _jasyptCredentialBinding(def job) {
  job.with {
    configure { node ->

      node / 'buildWrappers' / 'org.jenkinsci.plugins.credentialsbinding.impl.SecretBuildWrapper' / 'bindings' << 'org.jenkinsci.plugins.credentialsbinding.impl.UsernamePasswordMultiBinding' {

        usernameVariable 'some-credential-id'
        credentialsId PASS2
        passwordVariable USER2

      }
    }
  }
}

def a_job = job('a-temporaryjob')

_artifactoryCredentialBinding(a_job)
_jasyptCredentialBinding(a_job)


To understand how the Configure Block works I highly suggest reading the wiki page and an older blog post which explains step by step how to configure an unsupported plugin.

Upvotes: 2

Related Questions