Job DSL Extracting into Groovy Script

54 views
Skip to first unread message

Gergely Brautigam

unread,
Oct 13, 2015, 9:37:43 AM10/13/15
to Jenkins Users
Hi guys.

I'm trying to extract some common stuff from a JOB DSL file into some scripts which I can then use to augment my jobs to insert some common stuff.

So I'm doing it like the example showed it =>

package utilities

class MyUtilities {
    static void addMyFeature(def job) {
        job.with {
            description('Arbitrary feature')
        }
    }
}

import utilities.MyUtilities def myJob = job('example') MyUtilities.addMyFeature(myJob)
Which is fine and it does work. The problem comes in when my Groovy script looks like this =>

package utilities

public class JobCommonTemplate {
   
public static void addEmailTemplate(def job) {
        job
.with {
            publishers
{
               
String emailScript = readFileInWorkspace("packagename/EmailExt_TestResultSummary.groovy")
                wsCleanup
()
                extendedEmail
('asdf', '$DEFAULT_SUBJECT', '$DEFAULT_CONTENT') {
                    configure
{ node ->
                        node
/ presendScript << emailScript
                        node
/ replyTo << '$DEFAULT_REPLYTO'
                        node
/ contentType << 'default'
                   
}
                    trigger
(triggerName: 'StillUnstable', subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT', replyTo: '$DEFAULT_REPLYTO', sendToDevelopers: true, sendToRecipientList: true)
                    trigger
(triggerName: 'Fixed', subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT', replyTo: '$DEFAULT_REPLYTO', sendToDevelopers: true, sendToRecipientList: true)
                    trigger
(triggerName: 'Failure', subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT', replyTo: '$DEFAULT_REPLYTO', sendToDevelopers: true, sendToRecipientList: true)
               
}

           
}
       
}
   
}
}

This is all fine, though now that I see it, the static is not needed, I think. BUT! readFileInWorkspace or FromWorkspace is not part of the class PublisherContext.

I'm trying to get it to be used from any other implementation of the interface which has it, but I don't want to have to implement shitloads of other stuff that comes with the JobManagers.

If I override it and use new File('bla').text, I'm in another world of hurt since trying to access a local file, is a very bad no - no.

So, what's the proper way to do this guys?

THANKS! Any help is very much welcome!!

Gergely.

milki milk

unread,
Oct 13, 2015, 12:34:59 PM10/13/15
to Jenkins Users
This is more appropriate for the job-dsl-plugin mailing list.

readFileFromWorkspace is part of DslFactory. You can pass in a DslFactory through the call to addEmailTemplate or maybe import DslFactory and use it directly.

Gergely Brautigam

unread,
Oct 14, 2015, 1:14:57 AM10/14/15
to Jenkins Users
Thanks!

Sorry, didn't knew there was such a list. I'll take it there as well.

Thank you! I'll try your suggestion once I'm in.

Reply all
Reply to author
Forward
0 new messages