Obtaining SCM CredentialsId and failing the seed job if not

1,510 views
Skip to first unread message

Ed Randall

unread,
Feb 9, 2015, 8:48:17 AM2/9/15
to job-dsl...@googlegroups.com
Hi,

I recently started using job-dsl-plugin with the goal of setting up 80+ jobs...

The first issue I have to solve is setting the SCM credentials correctly.   We have a number of different
credentials are set up on Jenkins under "Global Credentials" and it appears that the credentialsId is
needed for the SCM configuration; this is not a human-readable value, something  like:

9b2e5a52-c95c-4d0f-b64e-3b8a38077c76

I discovered (from a bug report, JENKINS-19705) that this Id can be obtained using

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

So far, so good. But if I mistype 'my-credentials-description' then I get a null credentialsId.

1. This "jm" variable really ought to be in the Wiki FAQ
2. It would be nice if there were a way it iterate over credentials and/or match them against a regexp
3. When I get a null credentialsId, I would like my script to exit and fail the build rather than appear to have worked.

Is there an easy answer to #3 please, how to return immediately from my DSL script and "fail" the build?
Thanks
Ed



Ed Randall

unread,
Feb 9, 2015, 10:28:05 AM2/9/15
to job-dsl...@googlegroups.com
Whilst investigating how to do this, I was surprised to discover that there's no SecurityManager protection in place.
I (rather recklessly) just now took out the whole Jenkins master tomcat server with a System.exit.
This is my script:

def jobname = 'my-project-name'
def giturl = 'g...@myhost.mydomain.com:mygroup/projectname.git'

def scmuser = 'username@myhost.mydomain.com'
def credsId = jm.getCredentialsId(scmuser)
if (credsId == null) {
      println
("Fatal - credentialsId not found for ${scmuser}")
     
System.exit(1)
     
return
}

println
('Creating job: '+jobname+' for: '+giturl)
job
{
      name
(jobname)
      displayName
(jobname+'.compile')
      label
('compile-host')
      disabled
(true)
      quietPeriod
(120)
      logRotator
(14, 20, 7)
      concurrentBuild
(false)
      jdk
('JDK_16')
    scm
{
      git
{
        remote
{
              url
(giturl)
              credentials
(credsId)
       
}
     
}
   
}
    triggers
{
        scm
('H/5 * * * *')
   
}
    steps
{
        maven
('--errors --batch-mode clean test')
   
}
}




Daniel Spilker

unread,
Feb 10, 2015, 6:48:37 AM2/10/15
to job-dsl...@googlegroups.com
Hi Ed,

the Job DSL plugin does not provide any extra security mechanisms on top of Jenkins. You can also use the script console, the system Groovy step from the Groovy plugin and probably also the Groovy CLI command to take out Jenkins. If that's a problem, please start a new thread here in the Google Group.

I consider the jm variable to be an implementation detail which leaks into the scripts. You can use it, but it may change on short notice. It's not going to be documented in the wiki.

As mentioned in the wiki, you do not need to use jm.getCredentialsId since the credentials method already accepts the description of the credentials:

The arguments for the credentials method is the description field or the UUID generated from Jenkins | Manage Jenkins | Manage Credentials. The easiest way to find this value, is to navigate Jenkins | Credentials | Global credentials | (Key Name). Then look at the description in parenthesis or using the UUID in the URL.
...
// add a Git SCM for a GitHub repository and use the given credentials for authentication
git {
    remote {
        github('account/repo', 'ssh')
        credentials('GitHub CI Key')
    }
}
Daniel

--
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/0ad4296e-a104-49a7-a4d8-0ee2cc903eed%40googlegroups.com.

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

Indra Gunawan

unread,
Jan 18, 2017, 3:52:38 PM1/18/17
to job-dsl-plugin
Using the credential's description does not work.  It becomes null after the job is created by seed job.

The Credential plugin version is : 2.5.1
The DSL plugin version is : 1.51
The Jenkins version is : 1.625.3 

I ended up supplying the credential id generated by Jenkins to credentials().

I wish I do not need to do this.

Victor Martinez

unread,
Jan 18, 2017, 3:57:26 PM1/18/17
to job-dsl-plugin
It has been deprecated since version 1.31 so please look at the below page:

Cheers

Indra Gunawan

unread,
Jan 18, 2017, 3:58:38 PM1/18/17
to job-dsl-plugin
using jm.getCredentialsId breaks the seed job:

ERROR: (exampleJobPRThrottleRally.groovy, line 43) No signature of method: javaposse.jobdsl.plugin.InterruptibleJobManagement.getCredentialsId() is applicable for argument types: (java.lang.String) values: [SPB Jenkins Generic User]
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
Finished: FAILURE

Victor Martinez

unread,
Jan 18, 2017, 4:48:10 PM1/18/17
to job-dsl-plugin
It has been deprecated since long time ago. If you see my previous comment you can find in the same url some references:
https://github.com/jenkinsci/job-dsl-plugin/blob/master/docs/Migration.md#jobmanagement-3

It's also worth looking at the release notes:

joujou

unread,
Jan 23, 2017, 12:00:38 PM1/23/17
to job-dsl-plugin
Hello,
what to use instead to get credential value?

Victor Martinez

unread,
Jan 23, 2017, 1:00:34 PM1/23/17
to job-dsl-plugin
As I already mentioned in one of the previous comments:
Reply all
Reply to author
Forward
0 new messages