KBEC-00080 - Using Perl and xPath to find a preceding sibling step relative to the current step

Article ID:360032831832
2 minute readKnowledge base

Description

You are in an CloudBees CD (CloudBees Flow) step and you want to access properties (like outcome) stored in a previous step. You want to find a previous step to reference that property.

Solution

  1. Use getJobDetails to create an XML object containing all job steps in the current job.

  2. Use xPath to locate the current job step - the one that your code is running in.

  3. Use the xPath keyword, preceding-sibling to find the preceding step.

Example perl

This example uses xPath to find the current job step and its preceding sibling, and prints a property from that step.

use strict;
use ElectricCommander ();
my $ec = new ElectricCommander->new();
my $xPath = $ec->getJobDetails($ENV{COMMANDER_JOBID});

#  Find the name of the first job step and then the job step ID of that first step
my $firstStepName = $xPath->findvalue(qq#//jobStep[jobStepId="$ENV{COMMANDER_JOBSTEPID}"]/../jobStep[1]/stepName#);
my $firstJobStepId = $xPath->findvalue(qq#//jobStep[jobStepId="$ENV{COMMANDER_JOBSTEPID}"]/../jobStep[stepName="$firstStepName"]/jobStepId#);
print "Id of first job step $firstStepName is $firstJobStepId\n";

#  Find a sibling job step by its position relative to the current job step
my $siblingJobStepId = $xPath->findvalue(qq#//jobStep[jobStepId="$ENV{COMMANDER_JOBSTEPID}"]/preceding-sibling::jobStep[1]/jobStepId#);
print "Id of preceding job step is $siblingJobStepId\n";

#  Find the two steps back sibling step by its position relative to the current job step and show a two job step back property
my $twoBackJobStepId = $xPath->findvalue(qq#//jobStep[jobStepId="$siblingJobStepId"]/preceding-sibling::jobStep[1]/jobStepId#);
my $whichResource = $xPath->findvalue(qq#//jobStep[jobStepId="$siblingJobStepId"]/resourceName#);
print "Id of two job steps back is $twoBackJobStepId using resource $whichResource\n";

Example step parent Javascript

After Commander 3.5, you no longer need to use Perl to find the previous step. Given steps A and B in parallel, step B can refer to step A’s status and outcome as:

echo Status - $[/myParent/jobSteps/A/status]
echo Outcome - $[/myParent/jobSteps/A/outcome]

Example hierarchical Javascript step property retrieval

By at least Commander 3.8 and possibly before, you can use javascript to find arbitrary parent properties.

$[/javascript
function fullStepName(jobStep) {
var result = "";
var parentStep = getProperty(jobStep ,"/myParent");

// Walk up the chain if the parent is not the root step
if (parentStep.stepName != null) { result += fullStepName(parentStep) + "/"; }
result += jobStep.stepName;
return (result);
}
fullStepName(myJobStep);
]

Given procedure A which calls subprocedure B which calls subprocedure C, the above code would produce output:

A/B/C

To retrieve the step name of procedure A from a step in procedure C, use the javascript code:

$[/javascript getProperty(getProperty(myJobStep ,"/myParent") ,"/myParent").stepName;]
javascript will error if the above code does not executed from a subprocedure.

Example Javascript workflow property retrieval

The following code will return the parent’s workflow name if the code is called from a workflow:

$[/javascript if (myJob.callingState != null) {
myJob.callingState.workflow.callingState.workflow.workflowName //logical form
myState.workflow.callingState.workflow.workflowName //alternate form
}
]