Job DSL and Credentials

4,064 views
Skip to first unread message

Kyle Leinen

unread,
Sep 20, 2013, 7:14:50 PM9/20/13
to job-dsl...@googlegroups.com
I have only been a user of this awesome plugin for a short time, but I am hitting a wall with a need.

I am trying to dev a dsl groovy script that will create a job that uses the Mercurial scm, but also uses the new credentials ability.  I know the groovy code to get the credentials ID, but I can't seem to get it working in through the Job DSL plugin.  Can anyone help?

Here is what I know will get me the credentials ID for the "foobar" credentials:

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
    Jenkins.instance,
    null,
    null
);
for (c in creds) {
  if (c.description == "foobar") {
     println(c.id)
     break
  }  
}

But when I put this into a dsl, I get the following:

FATAL: No such property: com for class: test
Possible solutions: jm
groovy.lang.MissingPropertyException: No such property: com for class: test
Possible solutions: jm
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
	at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
	at test.run(test.dsl:21)
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngineForParent(DslScriptLoader.java:68)
	at javaposse.jobdsl.dsl.DslScriptLoader.runDslEngine(DslScriptLoader.java:92)
	at javaposse.jobdsl.plugin.ExecuteDslScripts.perform(ExecuteDslScripts.java:159)
	at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
	at hudson.model.Build$BuildExecution.build(Build.java:199)
	at hudson.model.Build$BuildExecution.doRun(Build.java:160)
	at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:567)
	at hudson.model.Run.execute(Run.java:1604)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
	at hudson.model.ResourceController.execute(ResourceController.java:88)
	at hudson.model.Executor.run(Executor.java:246)

Any pointers or ways of positive guidance is much appreciated!  Thanks!

Andrew Harmel-Law

unread,
Oct 1, 2013, 7:50:21 AM10/1/13
to job-dsl...@googlegroups.com
Hi Kyle,

Apologies for taking so long to get back to you.  Is this the entirety of your DSL script?  If there is any more could you post it for a bit of context?

Cheers, Andrew

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/0c1b4d8c-1924-4ea7-a4aa-26aa213209a5%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Justin Ryan

unread,
Oct 3, 2013, 4:47:13 PM10/3/13
to job-dsl-plugin
Kyle, the classes you're referring to aren't in the classpath and Groovy is giving you an indirect error which should have been ClassNotFound. If I'm wrong, you could try doing some import statements.

The job-dsl-core recently started to access the credentials functionality. You might be able to access it by calling jm.getCredentialsId("foo"). If you have more use-cases for the credentials plugin, in addtion to the SSH Agent plugin, we'd be happy to hear about it. The basic logic is all there now to bake it into the DSL directly.


Kyle Leinen

unread,
Oct 3, 2013, 7:01:33 PM10/3/13
to job-dsl...@googlegroups.com

I think that is exactly what I am looking for, nothing complicated, just the ability to specify the credentials by name.  Thanks for the great insight and help.  I hope to be able to help contribute on the future as I am using write a few home grown "helpers" with this DSL awesomeness that may be good to expand the core functionality of the plugin.

You received this message because you are subscribed to a topic in the Google Groups "job-dsl-plugin" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/job-dsl-plugin/oE62HT0kbPM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to job-dsl-plugi...@googlegroups.com.

To post to this group, send email to job-dsl...@googlegroups.com.

Daniel Spilker

unread,
Nov 21, 2013, 5:35:43 PM11/21/13
to job-dsl...@googlegroups.com

You can get the credentials from the JobManagement interface. Here is an example how I set the credentials for the new Git Plugin 2.0:

gitHubCredentials = jm.getCredentialsId('my-credentials-description')

job {
  ..
  scm {
    github('some/project', branch) {
      it / userRemoteConfigs / 'hudson.plugins.git.UserRemoteConfig' / credentialsId(gitHubCredentials)
    }
  }
  ..
}



For more options, visit https://groups.google.com/groups/opt_out.



--

    daniel spilker

    ma...@daniel-spilker.com
    +49.179.1093202
    geschwister-scholl-straße 62, 20251 hamburg
    http://daniel-spilker.com

Sergiu Nacu

unread,
Jan 14, 2014, 6:26:57 PM1/14/14
to job-dsl...@googlegroups.com
Thanks this worked for me! 
The credential description is  a jenkins generated string of the following form: a51dc0cf-2082-4117-9b28-0422a6c8db7e
This string appears in the jenkins url when going to global credentials configuration.

-sergiu 

Gregg Lowrimore

unread,
Oct 21, 2015, 7:10:14 PM10/21/15
to job-dsl-plugin
How would this scm github configure block change if I'm using scm git remote to an internal github enterprise appliance?

Kyle Leinen

unread,
Oct 21, 2015, 7:16:48 PM10/21/15
to job-dsl-plugin

I don't think so.  We are using it against a private Github Enterprise server.


Gregg Lowrimore

unread,
Oct 27, 2015, 11:32:42 AM10/27/15
to job-dsl-plugin
I'm a little hesitant to just stick this code in to my job snippet outlined below. Would this be the correct implementation, buried inside this URL snippet like so?

job {
   scm {
      git {
 remote {
   url("https://github.mycompany.com/" + org + "/" + repo + ".git") {
   it / userRemoteConfigs / 'hudson.plugins.git.UserRemoteConfig' / credentialsId(gitHubCredentials)
           }
 }
       }
   }
 [...snip...]

Daniel Spilker

unread,
Oct 27, 2015, 11:40:31 AM10/27/15
to job-dsl...@googlegroups.com
You can pass the host name of your GitHub Enterprise server as third parameter to the github method and use the built-in credentials method:
job('example') {
    scm {
        git {
            remote {
                github('org/repo', 'https', 'github.example.org')
                credentials('github-ci-key')
            }
        }
    }
}

Regards,
Daniel


Reply all
Reply to author
Forward
0 new messages