Creating post-processing scripts
Post-processing scripts are used in job steps to determine the success or failure of a step. Success is determined by the exit code and log of the step.
About this task
Procedure
- On the left navigational panel, click in the Script Library area.
- Click Create New Script.
- On the New Post-Processing Script window, type a name for the script in the Name field.
-
Enter the script in the Script box.
The script must be written in JavaScript.™ The step output properties are in a
java.util.Propertiesvariable namedproperties. TheexitCodeproperty contains the exit code of the process. TheStatusproperty contains the final status. AnyStatusvalue other than Success results in the step failing. Thescannervariable can be used to scan the output log of the step. Thescannervariable has several public methods:register(String regex, function call)registers a new function to be called whenever the regular expression is matched.addLOI(Integer lineNumber)adds a specific line to the lines of interest list. The lines are displayed in the LogViewer after the process finishes. This is implicitly called anytimescan()matches a line.getLinesOfInterest()returns ajava.util.Listlist that contains the lines of interest. This method can be used to remove lines if necessary.
After all regular expressions are registered, use
scan.scan()to scan the log. - Click Save.
Example
commandOut.println("This is a test command output line! This can be used as a means of logging messages!");
var prop1Value = "";
scanner.register("^prop1=", function(lineNumber, line) {
commandOut.println("Inside callback for line of interest: " + line);
prop1Value = line.substr(line.indexOf('=') + 1, line.length - line.indexOf('='));
properties.put("buildlife/prop1", prop1Value);
});
scanner.scan();
commandOut.println("Exit code is: " + properties.get("exitCode"));
commandOut.println("Value for property 1 is: " + prop1Value);
properties.put("Status", "Success");
The example includes a couple of statements with the commandOut command. You can use commandOut command to print messages that can be used for tracing flow and debugging.
In regards to setting Status, while there is nothing wrong with explicitly setting a Failure status, there is no need to. Lack of a Status property with a value of Success is equivalent to Failure. Basically, anything other than Success is considered Failure.
