0

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 ?

Shashi Kiran
  • 362
  • 2
  • 9
  • 27
  • Not sure you'll be able to do it as pointed in your example ... I would try through pure groovy and build an array which you then include in the dsl (as per the accepted answer in your referenced link) – Toni Van de Voorde Jan 17 '20 at 12:22
  • @ToniVandeVoorde Yes indeed that will work, but I need to re use the credential binding. For example I need both the creds for jobA and I would need only one of the creds for jobB or jobC etc – Shashi Kiran Jan 17 '20 at 14:15
  • Actually, I didn't look carefully enough ... the reference you provided is Jenkins pipeline code and not JobDSL code ... I'm afraid you will not succeed in the "traditional" way. It should be achievable through the Configure Block (https://github.com/jenkinsci/job-dsl-plugin/wiki/The-Configure-Block) way. If nobody else comes with a solution and/or you didn't find a way I'll see if I find time to work a solution out. – Toni Van de Voorde Jan 17 '20 at 14:33
  • @ToniVandeVoorde will give this a try – Shashi Kiran Jan 19 '20 at 09:09

1 Answers1

2

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.

  • 1
    I tired to use the configure block, but when the seed job ran (job used to create the other jobs) getting the below exception. Is there any prior configurations to be done ? ERROR: (CredentialBindingUtil.groovy, line 44) No signature of method: xxx.CredentialBindingUtil$__jasyptCredentialBinding1_closure4.configure() is applicable for argument types: (xxx.CredentialBindingUtil$__jasyptCredentialBinding1_closure4$_closure13) values: [xxx.CredentialBindingUtil$__jasyptCredentialBinding1_closure4$_closure13@e2c2d41] [Office365connector] No webhooks to notify Finished: FAILURE – Shashi Kiran Jan 21 '20 at 02:22
  • I would need to full seed job file you are using. Can you share that somewhere? Maybe a public git repo? – Toni Van de Voorde Jan 21 '20 at 07:04
  • you are awesome, this worked. I had to try out different versions of org.jenkins-ci.plugins:job-dsl-core, currently I'm using 1.68 and it is picking up my changes. – Shashi Kiran Jan 21 '20 at 07:29