JobDSL triggers deprecated, pipelineTriggers closure problem - jenkins

I'm using jobdsl 1.76 and trying to migrate to 1.77: from triggers to pipelineTriggers:
pipelineJob('testPipelineTriggers'){
properties {
pipelineTriggers {
triggers {
cron{
spec("* * * * *")
}
}
}
}
}
This simple code giving me below error which I couldnt understand:
No signature of method: javaposse.jobdsl.dsl.helpers.properties.PropertiesContext.pipelineTriggers() is applicable for argument types: (XXX$__clinit__closure1$_closure2$_closure7$_closure9) values: [XXX$__clinit__closure1$_closure2$_closure7$_closure9#3c818ac4]
note: I replaced our groovy filename with 'XXX'

Related

Jenkins triggers gets depreciated

I have the below piece of code in groovy to schedule the job at 12 am IST.
I am using Job DSL plugin to seed the job.
Initial code-
triggers{
cron{
spec("TZ=Asia/Calcutta\n0 0 * * *")
}
}
For the same even though it works, I get depreciation warnings.
Warning: (jobName.groovy, line 18) triggers is deprecated
Second code-
void nightly(String schedule = 'H 0 * * *') {
job.properties {
pipelineTriggers {
triggers{
cron{
spec("TZ=Asia/Calcutta\nH 0 * * *")
}
}
}
}
}
The second one got failed with the below error message.
JobScriptsSpec > test script fr_oms_core_unit_perf_sanity_job.groovy FAILED
org.spockframework.runtime.UnallowedExceptionThrownError at JobScriptsSpec.groovy:24
Caused by: javaposse.jobdsl.dsl.DslException at JobScriptsSpec.groovy:21
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException at JobScriptsSpec.groovy:21
How can I avoid the same?
Am I using the correct format?
Thanks in advance.
The new syntax uses the pipelineTriggers directive under the properties directive instead of the deprecated triggers directive:
pipelineJob('MyPipelineJob') {
properties {
pipelineTriggers {
triggers {
cron{
spec("TZ=Asia/Calcutta\n0 0 * * *")
}
}
}
}
}
The documentation for the pipelineTriggers is available in your own Jenkins server in the following URL:
https://your.jenkins.domain/plugin/job-dsl/api-viewer/index.html#path/javaposse.jobdsl.dsl.DslFactory.pipelineJob-properties-pipelineTriggers

Jenkins Job DSL: unknown return statement

some days ago I bumped into a code snippet used to override the default configuration of the Jenkins plugin "GitHub SCM Source" (unknown author):
Closure configOverride(String repo, int id, String cred) {
return {
it / sources / 'data' / 'jenkins.branch.BranchSource' << {
source(class: 'org.jenkinsci.plugins.github_branch_source.GitHubSCMSource') {
id(id)
scanCredentialsId(cred)
checkoutCredentialsId('SAME')
repoOwner('owner')
repository(repo)
includes('*')
buildOriginBranch('true')
buildOriginBranchWithPR('true')
buildOriginPRMerge('false')
buildOriginPRHead('false')
buildForkPRMerge('true')
buildForkPRHead('false')
}
}
}
}
All it's good except that I can't understand the following line:
it / sources / 'data' / 'jenkins.branch.BranchSource' << { ... }
I tried to find some explanation about the use of '/' in groovy but no luck. Maybe I don't know what exactly to search.
Could someone help me please with a link to the docs or a short explanation.
This is overloading of
operators
Groovy allows you to overload the various operators so that they can be used with your own classes. Consider this simple class:
class Bucket {
int size
Bucket(int size) { this.size = size }
Bucket plus(Bucket other) {
return new Bucket(this.size + other.size)
}
}
Just by implementing the plus() method, the Bucket class can now be used with the + operator like so:
def b1 = new Bucket(4)
def b2 = new Bucket(11)
assert (b1 + b2).size == 15
For / one would override T div(T x)
This code snippet is used in Jenkins DSL for the "multibranchPipelineJob". The closure configOverride is used to generate an XML object which replace the default configuration in config.xml on the following path "sources/data/jenkins.branch.BranchSource".

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 ?
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.

jenkins job dsl - No signature of method: java.lang.String.call()

I am unable to run this piece of code:
buildPath = 'applications'
buildJob(['java', 'nodejs'])
def buildJob(def jobList){
for(job in jobList){
def jobName = "${job}_seed"
def jobDescription = "Jenkins DSL seed for ${job}"
def jobScriptPath = "resources/dsl/${jobName}.groovy"
job("${buildPath}/${jobName}")
}
}
So, I am getting this error:
Processing provided DSL script
ERROR: (script, line 12) No signature of method: java.lang.String.call() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [applications/java_seed]
Possible solutions: wait(), any(), wait(long), take(int), each(groovy.lang.Closure), any(groovy.lang.Closure)
Finished: FAILURE
I do not see where or what is causing this error. I created a single job outside of the buildJob(def jobList) function and it is working but I need to do the loop to automation the jobs creation.
Any ideas?
Posting a similar issue i have run into. Isn't much around the web on this issue.
No signature of method: java.lang.String.call() is applicable for argument types: (java.lang.String) values: [some-value]
Say we are implementing a job dsl plugin (https://github.com/jenkinsci/multibranch-build-strategy-extension-plugin) like:
includeRegionBranchBuildStrategy {
includedRegions(String value)
}
And we have code like:
def includedRegions = r ? String.join("\n", r) : null
branchSources {
branchSource {
buildStrategies {
if(includedRegions){
includeRegionBranchBuildStrategy {
includedRegions(includedRegions)
}
}
}
}
}
Need to rename your variable to get it to work! e.g the method can't have the same name as a var defined above.
def regions = r ? String.join("\n", r) : null
branchSources {
branchSource {
buildStrategies {
if(regions){
includeRegionBranchBuildStrategy {
includedRegions(regions)
}
}
}
}
}
you are iterating string array in the following line:
for(job in jobList){
and using variable job for this.
then you try to invoke method call on this variable:
job("${buildPath}/${jobName}")

DSL for triggering cron with a parameter. I have defined the parameter in the job above but unable to pass it in the cron using dsl scripts

I have created the parameter but i am unable to pass that variable while creating the cron job.
job("dev_testing")
{
parameters
{
booleanParam('security_scan', true)
choiceParam('OPTION', ['false (default)', 'true',])
}
triggers
{
cron('H 23 * * 6 %security_scan; true')
}
}
Following is the error:
ERROR: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object (javaposse.jobdsl.dsl.helpers.triggers.TriggerContext parameterizedTimerTrigger script$_run_closure1$_closure2$_closure3)
I dont know which plugins you have installed, but the Parameterized Scheduler plugin should help you with your use case.
Per their documentation, the below should work:
triggers {
parameterizedCron('''H 23 * * 6 %security_scan=true''')
}
This also worked for me:
triggers {
parameterizedTimerTrigger {
parameterizedSpecification('H 23 * * 6 %security_scan=true')
}
}
I know this is an old thread - but I just ran into this lately and couldn't get multiple triggers to work for the life of me. Finally I got it working so:
triggers {
parameterizedTimerTrigger {
parameterizedSpecification('''H 21 * * 0-4 %APPLICATION=php
H 23 * * 0-4 %APPLICATION=java''')
}
}

Resources