KBEC-00301 - Search for objects and customize output

Article ID:360033190811
2 minute readKnowledge base

Summary

CloudBees CD (CloudBees Flow)'s web UI search function is powerful, but the output table can’t be customized to display specific properties. This makes some searches (such as getting all the job step IDs of running jobs) tedious to conduct. The UI search is also not very helpful if search results are needed for some automated task.

Solution

Use ec-perl’s findObjects API to search for jobSteps that match the criteria, then parse the XML response object for relevant values to either print to log or save for other use. With the right parameters and filters, this method can also be expanded to search for objects other than job steps and criteria other than status.

Examples

The following script searches for all job steps that are waiting on resources. It then prints the job ID, job name, job step ID, step name, and status message of each of those step.

use strict;
use ElectricCommander;
my $ec = new ElectricCommander();


# search for job steps with status 'waiting for resource'
my $result = $ec->findObjects("jobStep",{
    filter => [{
        propertyName => 'status',
        operator => 'equals',
        operand1 => 'runnable'}]
});

# split into objects
my @nodes = $result->findnodes("//object");

# iterate through each object and print values
foreach my $node (@nodes) {
    printf "jobId: ".$node->findvalue('jobStep/jobId')."\n";
    printf "jobName: ".$node->findvalue('jobStep/jobName')."\n";
    printf "jobStepId: ".$node->findvalue('jobStep/jobStepId')."\n";
    printf "stepName: ".$node->findvalue('jobStep/stepName')."\n";
    printf "status: ".$node->findvalue('jobStep/combinedStatus/message')."\n";
    printf "\n";
}

To see what the return object for findObjects looks like and what properties are accessible by default for each object, add this line to print it:

printf $result->findnodes_as_string("/");

Please consult CloudBees CD (CloudBees Flow) documentation for more details and examples on the findObjects API.

Applies to

  • Product versions: all

  • OS versions: all