KBEC-00095 - Using Run Condition with examples

Article ID:360032831592
2 minute readKnowledge base


Run Condition controls whether a step is skipped or run.

By default, if the Run Condition field is blank, the step is run. This field accepts "fixed text" or text embedding property references that are evaluated into a logical TRUE (run the step) or FALSE (skip the step). A "" (empty string), a "0" (zero) or "false" (FALSE), is interpreted as FALSE (skip the step). Any other result string is interpreted as TRUE (run the step).

Because JavaScript expressions can be part of a Run Condition, you can conditionally run a step. CloudBees CD (CloudBees Flow) properties give Run Conditions a lot of power.

Simple Solution

If the nightly build errors property is greater that 5, do not run the step.

Use the following code in the Run Condition

$[/javascript myJob.nightlyBuildErrors < 5 // more than 5 failed builds, don't run the step]

Previous Steps Outcome Solution

Check if a previous specific step has worked or not.

Use the following code in the Run Condition

$[/javascript "$[/myParent/jobSteps/step1/outcome]" == "success" // use the parent reference and the previous step name]

Academic Solution

What if you want to randomly skip a step? Great for testing your job flow.

Use the following code in the Run Condition

$[/javascript Math.floor(Math.random()*2) > 0.0 // this JavaScript run condition will randomly skip a step ]

Preflight Snapshot Solution

When running prefights, only one snapshot step should run, depending on whether a production or preflight build is invoked. To enable the appropriate step:

  1. Create a checkbox-style parameter to the build procedure to determine whether or not the build is a preflight.

  2. Use that parameter in the Run Condition field on the two snapshot steps.

In the following example, the parameter is named "preflight", with an unchecked value of "false" and a checked value of "true".

  1. For the preflight snapshot step, set the Run Condition to

  2. For the production snapshot step, set the Run Condition to

     $[/javascript myJobStep.preflight == "false" // skip if this is not the production build ]


The run condition is evaluated when the step leaves the pending state — if it evaluates to false the outcome is set to skipped and the step completes.

This all happens before the step becomes runnable, which is when it would be assigned a resource.

This results in javascript references to /myResource in runCondition not being very useful.

Debugging tips

Oftentimes it is not obvious what is going on in the Run Condition. One of the things you can do is bring the run Condition into the command step. Then use either "echo" or perl 'heredoc' (set shell to ec-perl) to see what it evaluates to. The latter also lets you handle multiple lines.

echo $[/javascript

var somevalue = "";

somevalue += "something";




print <
  1. Javascript block evaluates to the last value, i.e. it will be the value of "somevalue" variable in this case.

  2. To have a better idea what your javascript evaluates to, you may take a look at the expanded command after the job has finished: go to:

    1. the Job Details page

    2. click on the step

    3. check the "Command:" field there.

Once the test javascript is working, move it back to the Run Condition.