Tutorial - Using Conditionals
Overview
A Conditional step in a Pipeline Definition is similar to an "if" in many programming languages with out the "else if" branches. The function will evaluate an expression for a True or False result and perform one action or another.
Setup of Jenkins Build
In the Using Pipeline Variables Tutorial, we explored using variables during the Pipeline Instance run. Edit the Pipeline Definition Stage that you created previously. On the Jenkins job step, change the setting Inherit Job Status
to "false".
Next select Flow, Utility - Conditional
from the plugin menu on the left. Place it below the Jenkins step on the right. We will add two "sub-steps" to the Conditional step for the If True
and Else
actions. For each, select Click to add a plugin
then drag Utility - Log
into each.
Edit each "Log" by clicking the edit button on the right and add the following text into each respectively after the edit box pops up.
True:
Job completed successfully
False:
Job failed
In the "Expression" box, paste the following line:
"[$ phasedata['__THIS_PHASE']['__THIS_STAGE']['0']['details']['status'] $]" == "SUCCESS"
This will look in the "phasedata" section of the Data document, drill into data for this phase, this stage, step 0 (first step), then in the details section and finally in the status field. The syntax will substitute status found and compare it to the string "SUCCESS" to determine whether to do the true or false condition.
Perform a commit, then find the Pipeline Instance associated with the commit. Check the Data tab on the Pipeline Instance detail page and expand the phasedata section all the way to the status field.
In the "Progress" link in the upper right, you should see either the true or false log message.