JuanD
JuanD

Reputation: 463

Jenkins: read an existing job's plugin config via Groovy

We are in the early phases of using Jenkins DSL. One challenge we have come across is being able to read in an existing jobs plugin settings so that we retain it before running the DSL. This allows us the ability to give the Jenkins users the option to keep some of their settings. We have successfully retained the schedule settings for our jobs but the newest challenge is being able to retain a plugin setting. Specifically a setting in the "ExtendedEmailPublisher" plugin. We would like to retain the value:

enter image description here

In the config.xml file for this job in the ExtendedEmailPublisher tags we see the following:

<publishers>
    <hudson.plugins.emailext.ExtendedEmailPublisher>
        <recipientList>Our_Team@Our_Team.com</recipientList>
        <configuredTriggers>
            <hudson.plugins.emailext.plugins.trigger.FailureTrigger>
                <email>
                    <recipientList/>
                    <subject>$PROJECT_DEFAULT_SUBJECT</subject>
                    <body>$PROJECT_DEFAULT_CONTENT</body>
                    <recipientProviders>
                        <hudson.plugins.emailext.plugins.recipients.ListRecipientProvider/>
                    </recipientProviders>
                    <attachmentsPattern/>
                    <attachBuildLog>false</attachBuildLog>
                    <compressBuildLog>false</compressBuildLog>
                    <replyTo>$PROJECT_DEFAULT_REPLYTO</replyTo>
                    <contentType>project</contentType>
                </email>
            </hudson.plugins.emailext.plugins.trigger.FailureTrigger>
        </configuredTriggers>
        <contentType>default</contentType>
        <defaultSubject>$DEFAULT_SUBJECT</defaultSubject>
        <defaultContent>$DEFAULT_CONTENT</defaultContent>
        <attachmentsPattern/>
        <presendScript>$DEFAULT_PRESEND_SCRIPT</presendScript>
        <classpath/>
        <attachBuildLog>false</attachBuildLog>
        <compressBuildLog>false</compressBuildLog>
        <replyTo>$DEFAULT_REPLYTO</replyTo>
        <saveOutput>false</saveOutput>
        <disabled>false</disabled>
    </hudson.plugins.emailext.ExtendedEmailPublisher>
</publishers>

The value we would like to extract/preserve from this XML is:

<disabled>false</disabled>

We have tried getting the existing values using groovy but cant seem to find the right code. Our first idea was to try to read the value from the config.xml using the XmlSlurper. We ran this from the Jenkins Script Console:

def projectXml = new XmlSlurper().parseText("curl http://Server_Name:8100/job/Job_Name/api/xml".execute().text);
*we use 8100 for our Jenkins port

Unfortunately while this does return some config info it does not return plugin info.

Then, we also tried running the following to read/replace the existing settings:

def oldJob = hudson.model.Hudson.instance.getItem("Job_Name")
def isDisabled = false // Default Value

for(publisher in oldJob.publishersList) {
  if (publisher instanceof hudson.plugins.emailext.ExtendedEmailPublisher) {
    isDisabled = publisher.disabled
  }
}

And while this works if executed from the Jenkins Script Console, when we try to use it in a DSL Job we get the message:

Processing provided DSL script
ERROR: startup failed:
script: 25: unable to resolve class 
hudson.plugins.emailext.ExtendedEmailPublisher 
 @ line 25, column 37.
   if (publisher instanceof hudson.plugins.emailext.ExtendedEmailPublisher) 
{
1 error

Finished: FAILURE

SOLUTION UPDATE:

Using url @aflat's URL suggestion for getting the raw XML config info, I was able to use the XML Slurper and then use the getProperty method to assign the property I wanted to a variable.

def projectXml = new XmlSlurper().parseText("curl http://Server_Name:8100/job/Job_Name/config.xml".execute().text);
def emailDisabled = projectXml.publishers."hudson.plugins.emailext.ExtendedEmailPublisher".getProperty("disabled");

Upvotes: 4

Views: 4034

Answers (2)

aflat
aflat

Reputation: 4319

If you want to parse the config.xml, use

def projectXml = new XmlSlurper().parseText("curl http://Server_Name:8100/job/Job_Name/config.xml");

That should return your raw config.xml data

Upvotes: 3

xone47
xone47

Reputation: 1

Under "Manage Jenkins->Configure Global Security" did you try disabling "Enable script security for Job DSL scripts"?

Upvotes: 0

Related Questions