KBEC-00138 - Useful tips on setting properties with postp

Article ID:360032831192
2 minute readKnowledge base

1. Resetting the outcome of a job or a step

Problem

I would like to reset the outcome of a job or a step based on my own criteria.
For instance, it contains warnings and/or errors, yet there are circumstances when I still want to show it as "green".
Is it possible?

Solution

1A. Provide your own version of postpEndHook subroutine

sub postpEndHook() {
    # ...your logic here...
    setProperty ("/myJob/outcome", "success");
}

For instance, you can set a custom property in your matcher, then check it in postpEndHook, then adjust the outcome accordingly.

Example

Set the job outcome to "skipped" upon seeing "NotCompiled" pattern one or more times.

my @JobOutcome=({
    id =>"NotCompiled",
    pattern =>q{NotCompiled},
    action => q{$::joboutcome=1;},
},);

push (@::gMatchers,@JobOutcome);

sub postpEndHook() {
    if (defined $::joboutcome) {
        setProperty ("/myJob/outcome", "skipped");
    }
}


More reading

  • Online help

    • About Postprocessors -> postp functions

1B. Provide your own version of updateOutcome subroutine

sub updateOutcome() {
    #...your logic here
    setProperty("outcome", "error");
}

(yes, you can redefine any function, not just postpEndHook)

Take a look at the standard updateOutcome sub in the postp.pl source to see what it does by default.

Example

Show the overall job status as "success if postp finds warnings, rather than show it as warning.

sub updateOutcome() {
    if (defined($::gProperties{errors}) && ($::gProperties{errors} > 0)) {
        setProperty("outcome", "error");
    }
}

Keep in mind that the first approach is certainly more generic as you can create any custom actions with it.

2. Setting a Property when no postp match is found

Description

Normally, you think of postp as taking a direct action when some pattern is matched. However, there are also cases where you would like to take some action based on a more "global" condition. For example, you might want to set a property when some condition is not met.

Solution

The postp code has a call to a special subprocedure (or "hook") that it calls when the log file has been completely processed. The subprocedure is called postpEndHook . The default implementation of postpEndHook does nothing. You can redefine it as part of your postp extensions, using --load or --loadProperty to take special actions at the end of the step.

Example

Use a normal matcher to look for a certain error pattern, then use the postpEndHook to set another property if the error pattern never occurred.

push (@::gMatchers,
    {
        id => "SynergyError",
        pattern => q{SynergyError},
        action => q{incValue("warnings");
                    incValue("SynergyError");
                   },
    },
);

sub postpEndHook()
{
    if (! defined $::gProperties{"SynergyError"}) {
        setProperty  ("/myJob/SynergySuccessful", "$[stepName]");
    }
}

Note that in the example, there is a call to setProperty in the postpEndHook . After calling postpEndHook , postp will call the subprocedure updateServer to make sure that the properties are sent to the CloudBees CD (CloudBees Flow) server.