Executing partial job updates

22 views
Skip to first unread message

Ian Duffy

unread,
Nov 22, 2016, 12:03:41 PM11/22/16
to job-dsl-plugin
Hi All,

I'm wanting to build up static helper functions that given a job they overlay some functionality.

I've started of at looking at using with example:

def job = job('some-name')
setGitRepoURL(job, "http://github.com/lalala.git")
setGitRepoBranch(job, "master")

def setGitRepoUrl(def job, def gitUrl) {
    job.with {
        scm {
            git {
                remote {
                    url(gitUrl)
                }
            }
        }
    }
}

def setGitRepoBranch(def job, def branchName) {
    job.with {
        scm {
            git {
                remote {
                    branch(branchName)
                }
            }
        }
    }
}

The result is setGitRepoBranch changes get in but setGitRepoURL changes do not. Is there any way to do a merge of the two?

Thanks,
Ian. 

Kamil Demecki

unread,
Nov 24, 2016, 8:11:30 AM11/24/16
to job-dsl-plugin
I understand that you want to limit duplication of those two methods - if so you can use 'delegate'


            ...
            publishers
           
{
                addAuthorDescActions
().each { action -> action(delegate) }
               
...
           
}

   
public List addAuthorDescActions()
   
{
       
[
           
{
                context
-> context.with
               
{
                    groovyPostBuild
                   
{
                        sandbox
(true)
                        script
(addAuthorDesc())
                   
}
               
}
           
}
       
]
   
}

Kamil Demecki

unread,
Nov 24, 2016, 8:20:36 AM11/24/16
to job-dsl-plugin

Other way to limit duplication is groovy Map passed as arguments.

I'm not sure if you mean the same by 'overlay' functionality - I found useful to code complex pipelines with delegate + traits + Map and normal object programming. Complexity comes from number of projects and reporting we get from Jenkins - we put Jenkins not-related stuff into Ansible and call it from Jenkins anyway.

def setGit(Map args) {
    args
.job.with {
        scm
{
            git
{
                remote
{
                   
if (args.branchName)
                   
{
                        branch
(args.branchName)
                   
}
                   
if (args.gitUrl)
                   
{
                        url
(args.gitUrl)
                   
}                  
               
}
           
}
       
}
   
}
}
setGit
(job: job, branchName: 'master')

Daniel Spilker

unread,
Dec 3, 2016, 9:22:30 AM12/3/16
to job-dsl...@googlegroups.com
The scm configuration is always replaced as a whole and can not be modified. You can only have one helper method to build the while scm config at once. Or you can to use a configure block to amend an existing config.

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-plugin+unsubscribe@googlegroups.com.
To post to this group, send email to job-dsl-plugin@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/119952ed-88cc-492f-8cd5-accb054e0ad5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rahul Mishra

unread,
Dec 3, 2016, 9:42:17 AM12/3/16
to job-dsl...@googlegroups.com
You can solve this problem by using a different approach 

Define a function called getGitRepository()
which returns an object with two parameters 
url and branch 

Your scm section will be as follows 

scm {
            git {
                remote {
                    url(getRepository().url)
                       branch(getRepository().branch)
                }
            }
        }
    }

Regards,
Z
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/CAKqW32DxFyCtwrZHRjUF5Fdc8hgiLk1hxkPLK51%2BF_YeYh0A_A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages