Manage post-processing scripts

This topic explains how Deployment Automation handles post-processing in component processes. You can create and use your own post-processing scripts.

Guidelines for post-processing scripts

Component processes perform post-processing whenever a plugin step finishes running. Typically, post-processing scripts help ensure that expected results occurred.

When configuring a process step, you can use your custom script created in JavaScript.

When a process step finishes, the agent performing the step runs your script:

  1. The agent loads the server log file and finds the exit code property of the target step using regular expressions from the script. For details, see View the output log.

  2. Before processing the next step, the agent applies any actions defined in the script.

Recommendations for creating a post-processing script:

  • Wrap the script in a CDATA element.

  • Use the following JavaScript objects and functions

    Copy code
    Function
    Object
    Error
    decodeURI
    decodeURIComponent
    encodeURI
    encodeURIComponent
    escape
    isNaN
    unescape
    NaN
    isFinite
    parseFloat
    parseInt
    Infinity
    undefined
    EvalError
    RangeError
    ReferenceError
    SyntaxError
    TypeError
    URIError
    Array
    String
    Boolean
    Number
    Date
    Math
    JSON
    RegExp

For examples of scripts, see Post-processing script examples.

For details about component processes, see Create and design component processes.

Back to top

Create a post-processing script

To add a post-processing script, you need a Web UI role with the Settings permission. Administrators have this permission by default.

For details about Web UI roles, see Web UI roles and UI security.

To create a post-processing script:

  1. In Deployment Automation, go to Administration > Automation.

  2. Select the Post Processing Scripts tab.

  3. Click Create.

  4. Specify the post-processing script details:

    Field Description
    Name Enter the name of the script. The name must match the one you specified in the process step's properties.
    Script Body

    Enter or paste your script.

    Tip: To view information about the properties and variables available for user-defined scripts, hover over the field name.

  5. Click Save.

Note: Alternatively, you can create a post-processing script in the process designer when you set the properties for a plugin step.

Back to top

Post-processing script examples

Use these examples of post-processing scripts to create your own scripts.

Example 1: Specify a step's status in the post-processing script.

To specify the Status property with the value of Success, use the following script:

Copy code
properties.put("Status","Success");

 

Example 2: Inspect the step's output and run actions based on the output.

In this example, the post-processing script evaluates the exit codes specified in the script, and triggers actions based on exit code values.

In the post-processing script, include the scanner object:

  1. Use scanner.register() to register the strings "Exception" and "Process finished with value = " with a regular expression engine, and then run commands if those strings are found.

  2. Use scanner.scan() to scan each line of the step's output log after the strings are registered.

Copy code
properties.put("Status", "Success");

// Evaluate the built-in exitCode property, which indicates the exit code of the script called by the step.
// Typically, if the value of the exitCode property is not a zero, the step failed.

if (properties.get("exitCode") == 0) {
    properties.put("Status", "Success");
} else {
    // properties.put("Status", "Failure");

    // Use the scanner object to search the log for the string "Exception" (not case-sensitive).
    // The first argument is a regular expression.
    //
    // The second argument is an inline function, and it is invoked once for every line in the log output that matches the pattern.
    // The "number" variable contains the line number where the match was found, and the "fullLine" variable is the full text of the line.

    scanner.register("(?i)Exception", function(number, fullLine) {
        properties.put("LineString", fullLine);
        properties.put("Line", new java.lang.String(number));
        properties.put("Status", "Failure");
    });

    // You can register multiple searches with the scanner. Add another search to look for a string to set as an output property.

    scanner.register("Process finished with value = ", function(number, fullLine) {
        var value = fullLine.replace("Process finished with value = ", "");
        properties.put("ProcessValue", value);
    });

    scanner.scan();
}

 

Example 3: Pass a property to a later step.

This example demonstrates how to pass a property from an earlier step to any of the later steps in the same process.

Assume you have a process with two steps, Prepare Environment (earlier step) and Perform Deployment (later step). Perform Deployment is a Shell step.

To pass a property:

  1. Add the following post-processing script to the Prepare Environment step to create two output properties, Status and isDone:

    Copy code
    if (properties.get("exitCode") == 0) {
        properties.put("Status", "Success");
        properties.put("isDone", "true");
    } else {
        properties.put("Status", "Failure");
        properties.put("isDone", "false");
    }
  2. Set the contents of the Perform Deployment step to enable the step to access the property from the Prepare Environment step:

    Copy code
    echo Custom properties of the Prepare Environment step:
    echo "Prepare Environment/isDone = ${p:Prepare Environment/isDone}"

The Perform Deployment step can also use the properties of the Prepare Environment step in its post-processing script.

For example, to specify the property isDone of the Prepare Environment step in the property PrepareEnvironmentDone for the Perform Deployment step, use the following post-processing script:

Copy code
if (properties.get("exitCode") == 0) {
    properties.put("Status", "Success");
} else {
    properties.put("Status", "Failure");
}

properties.put("PrepareEnvironmentDone", "${p:Prepare Environment/isDone}");

 

Example 4: Pass a value to a later step.

This example demonstrates how to pass a value from an earlier step to any of the later steps in the same process.

Assume you have a process with two steps, Prepare Environment (earlier step) and Perform Deployment (later step). Prepare Environment is a Shell step.

To pass a value:

  1. In the Prepare Environment step, write the needed value to the step's output log, where the post-processing script can access the value.

    For example, use an echo command to add a record to the log:

    Copy code
    echo "Preparing environment..." 
    echo "Status of environment preparation = SUCCESS"
  2. In the post-processing script for the Prepare Environment step, use the scanner object to scan the log for the string "Status of environment preparation = ". After that, assign the value to an output property, for example, PrepareEnvironmentStatus.

    Copy code
    if (properties.get("exitCode") == 0) {
        properties.put("Status", "Success");
    } else {
        properties.put("Status", "Failure");
    }

    scanner.register("Status of environment preparation = ", function(lineNumber, line) {
        var value = line.replace("Status of environment preparation = ", "");
        properties.put("PrepareEnvironmentStatus", value);
    });

    scanner.scan();
  3. To pass the value to a later step, reference the output property PrepareEnvironmentStatus in that step.

    For example, pass the value to the Perform Deployment step:

    Copy code
    echo "Status of environment preparation is ${p:Prepare Environment/PrepareEnvironmentStatus}"

Back to top

See also: