使用Groovy在Jenkins中初始化审计试用插件

时间:2018-05-28 09:41:04

标签: jenkins jenkins-plugins jenkins-pipeline

我的方法:

import hudson.plugins.audit_trail.AuditTrailPlugin
import hudson.plugins.jobConfigHistory.JobConfigHistory
import net.sf.json.JSONObject

def auditTrialPlugin = Jenkins.getInstance().getPlugin(AuditTrailPlugin.class)

// println(auditTrialPlugin.getConfigXml().asString())

println("Going to configure...")

def logger = new JSONObject()
logger.put("log", "Vibin")
logger.put("limit", "1")
logger.put("count", "2")
logger.put("stapler-class", "hudson.plugins.audit_trail.LogFileAuditLogger")
logger.put("\$class", "hudson.plugins.audit_trail.LogFileAuditLogger")

def plugin = new JSONObject()
plugin.put("name", "audit-trail")
plugin.put("pattern", "")
plugin.put("logBuildCause", true)
plugin.put("loggers", logger)

auditTrialPlugin.configure(null, plugin)

Error happening:

java.lang.NoSuchMethodException: hudson.plugins.audit_trail.LogFileAuditLogger.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
Caused: java.lang.InstantiationException: hudson.plugins.audit_trail.LogFileAuditLogger
    at java.lang.Class.newInstance(Class.java:427)
    at hudson.model.Descriptor.newInstance(Descriptor.java:578)
Caused: java.lang.Error: Failed to instantiate class hudson.plugins.audit_trail.LogFileAuditLogger from {"log":"Vibin","limit":"1","count":"2","stapler-class":"hudson.plugins.audit_trail.LogFileAuditLogger","$class":"hudson.plugins.audit_trail.LogFileAuditLogger"}
    at hudson.model.Descriptor.newInstance(Descriptor.java:600)
    at hudson.model.Descriptor.newInstancesFromHeteroList(Descriptor.java:1055)
    at hudson.model.Descriptor.newInstancesFromHeteroList(Descriptor.java:1017)
    at hudson.plugins.audit_trail.AuditTrailPlugin.configure(AuditTrailPlugin.java:78)
    at hudson.plugins.audit_trail.AuditTrailPlugin$configure$1.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at Script1.run(Script1.groovy:24)

我试着按照我在GitHub上找到的插件的代码库。有一个&#39; configure&#39;我尝试使用的插件的功能,它不起作用。

插件的代码库:GitHub link

1 个答案:

答案 0 :(得分:0)

尝试一下:

import jenkins.model.*;
import hudson.plugins.audit_trail.AuditTrailPlugin;
import hudson.plugins.audit_trail.LogFileAuditLogger;

def log   = "Vibin"
def limit = 1
def count = 2

LogFileAuditLogger logFileAuditLogger = new LogFileAuditLogger(log, limit, count)

Jenkins j = Jenkins.getInstance();
AuditTrailPlugin plugin = j.getPlugin(AuditTrailPlugin.class);
plugin.loggers.clear()
plugin.loggers.add(logFileAuditLogger)
plugin.pattern = "" // empty pattern?

plugin.save()
plugin.start()

我认为使用LogFileAuditLogger class比'configure'功能更清楚。