we're using an old groovy-script to generate a pdf for a build-report on our Jenkins-server. The Job did not run for some months but now we reveive the following error altough nothing was changed on the job and at server-side
import hudson.model.*
import hudson.FilePath
import groovy.xml.MarkupBuilder
import java.text.SimpleDateFormat
// make variable bindings available in this script
binding.variables
// Timestamp format for output
def tsformat = "yyyy-MM-dd HH:mm:ss"
def to_ts_format = new SimpleDateFormat(tsformat)
// Integrity time format
def from_integrity_format = new SimpleDateFormat ("MMM dd, yyyy hh:mm:ss aa")
def today = Calendar.getInstance().getTime()
// get current build
def thr = Thread.currentThread()
def build = thr?.executable
// Get parameters
// reportname must be given as parameter binding - s. Jenkins groovy script extended ("erweitert") settings
String reportname = "${build.envVars.WORKSPACE}" "\\" "${build.envVars.par_reportname}"
// Get job name from environment
String srcjobname = "${build.envVars.PAR_JOB}"
// Get from and to
int to = "${build.envVars.PAR_TO}".toInteger ()
int last_to = "${build.envVars.PAR_FROM}".toInteger ()
int from = last_to 1
// End get parameters
// Get the job object ...
def job = Hudson.instance.getJob (srcjobname)
println ("\nCreating report for '" job.name "' builds: " from " to " to "\n")
// Get job time preceding earliest job in range
def frompredecessorbuild = job.getNearestOldBuild (last_to)
def predectime
if (frompredecessorbuild != null) {
println ("\nGot predecessor build: " frompredecessorbuild.number ".\n")
predectime = frompredecessorbuild.getTime ()
} else {
println ("\nDidn't get predecessor time, no cut off!\n")
predectime = to_ts_format.parse ("1900-01-01 00:00:00")
}
// Get builds from jobs (alternative way, because getBuildByNumber () seems not to work reliably)
def builds = job.builds
def freport = new FilePath (build.workspace, reportname)
def stream = freport.write()
//
// Create the xml ...
//
println ("Now create xml.")
try {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.changereport (job:srcjobname, time:today.format (tsformat), predecessortime:predectime.format (tsformat)) {
// for (def i in to .. from) {
for (def b in builds) {
// def b = job.getBuildByNumber (i)
/* if (b == null) {
println ("Skipped build: " i)
continue
} */
b_no = b.getNumber ()
if (b_no < from || b_no > to) {
println ("Skipped build: " b_no)
continue
} else {
println ("Processing build: " b_no)
}
buildinfo (number:b.number, time:b.getTime ().format (tsformat)) {
for (cs in b.changeSet) {
for (ci in cs) {
citime = from_integrity_format.parse (ci.date)
if (citime > predectime) {
// Add only entries whose date is bigger than the cut-off date
changeinfo (time:to_ts_format.format (citime), stime:ci.date, action:ci.action, author:ci.getAuthor().getDisplayName()) {
changedfile (fname:ci.file.toString (), "rev":ci.rev, ci.msg)
} // changeinfo
} // if
} // for ci ...
} // for cs ...
} // buildinfo
} // for i ...
} // changereport
} // try
finally {
// Need an encoding ... due to umlauts ...
stream.write ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>".getBytes ())
stream.write (writer.toString ().getBytes ())
stream.flush ()
stream.close ()
}
Following error:
groovy.lang.MissingPropertyException: No such property: writer for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
at org.kohsuke.groovy.sandbox.impl.Checker$checkedGetProperty.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:230)
at Script1.run(Script1.groovy:123)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runScript(GroovySandbox.java:162)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:370)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:314)
at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
at hudson.model.Build$BuildExecution.build(Build.java:206)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1894)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Build step 'Execute system Groovy script' marked build as failure
Something went wrong with the write-object. But we don't know why. Do anybody has a clue for solving the problem? Thanks!
CodePudding user response:
You should be declaring your writer
before the try block if you want to access it in the finally
block.
def writer = null
try {
writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.changereport (job:srcjobname, time:today.format (tsformat), predecessortime:predectime.format (tsformat)) {
// for (def i in to .. from) {
for (def b in builds) {
// def b = job.getBuildByNumber (i)
/* if (b == null) {
println ("Skipped build: " i)
continue
} */
b_no = b.getNumber ()
if (b_no < from || b_no > to) {
println ("Skipped build: " b_no)
continue
} else {
println ("Processing build: " b_no)
}
buildinfo (number:b.number, time:b.getTime ().format (tsformat)) {
for (cs in b.changeSet) {
for (ci in cs) {
citime = from_integrity_format.parse (ci.date)
if (citime > predectime) {
// Add only entries whose date is bigger than the cut-off date
changeinfo (time:to_ts_format.format (citime), stime:ci.date, action:ci.action, author:ci.getAuthor().getDisplayName()) {
changedfile (fname:ci.file.toString (), "rev":ci.rev, ci.msg)
} // changeinfo
} // if
} // for ci ...
} // for cs ...
} // buildinfo
} // for i ...
} // changereport
} // try
finally {
// Need an encoding ... due to umlauts ...
stream.write ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>".getBytes ())
stream.write (writer.toString ().getBytes ())
stream.flush ()
stream.close ()
}