Did you have issues using output parameter in CloudBees CD/RO or didn’t find much documentation about it? Then you are at the right place. I will present and guide you through an example of how to use the output parameter.

Example

The example is the following.

A procedure in CloudBees CD/RO needs to output his result, internally and externally. For the example, the output will be a boolean.

This output is used to know if we should do or skip processes.

In the procedure, the output will be used to condition one step.

In a pipeline, the procedure’s output will be used to condition other tasks.

Adding the output parameter

First, add the formal output parameter which will be called is_empty to the procedure.

Either through CloudBees CD/RO domain-specific language (DSL):

procedure 'example-output-procedure' {
  formaloutputParameter 'is_empty'

Or through the UI:

Setting the value of the output parameter

Let’s create the following procedure.

Use the command line interface ectool to interact with CloudBees CD/RO server, using your own condition to set the value of output parameter.

For my example, in the first step, I create a new directory called “test_directory” and check if it is empty.

mkdir test_directory

if [ -z "$(ls -A test_directory)" ]; then
  ectool setOutputParameter is_empty true
else
  ectool setOutputParameter is_empty false
fi

In the next procedure, the value of the output parameter will be available at the following path “/myjob/outputParameters/<name of the output parameter>”

echo "$[/myjob/outputParameters/is_empty]"

Add the condition

As the parameter is a boolean, you can directly add the following line in the condition:

$[/myjob/outputParameters/is_empty]

Run the procedure

To test it out, let’s run the procedure.

As the directory does not exist initially, the output parameter will be set to true and it will go to the second step, echoing “true”.

Pipeline – Creation

Let’s create the following pipeline. It will be composed of two stages.

The first stage running the procedure created before followed by another task based on a condition.

The second stage task will run based on a condition.

Add in the first stage, the first task which is running the procedure created earlier.

The second task called “Not Empty” only does the following.

echo "The directory was not empty"

Add in the second stage, the following task.

echo "The directory was empty or the Stage 1 didn't run"

Pipeline – Add the condition

For the condition on “Not Empty”, as the precedent task always runs first, I will only check the value of the output parameter.

For the condition on “Empty”, as it is in another stage and it is possible to only run the second stage, I will check if the output parameter exists or is set to “true”.

In CloudBees CD/RO, it is quite hard to know what the path is for a specific variable. To help us, CloudBees offers a “pick up property” through the UI.

Open the task “Not Empty” and in the conditions, click here:

It automatically inserts the path of the variable. Now we can write our condition using the Output Parameter path.

$[/javascript mystageRuntime.tasks["Run procedure"].job.outputParameters.is_empty == false]

Do the exact same step for the task “Empty” in the second stage, to get the correct path of the output parameter. As you can see, the paths are different.

$[/javascript myPipelineRuntime.stages["Stage 1"].tasks ["Run procedure"].job.outputParameters.is_empty == null] or $[/javascript myPipelineRuntime.stages["Stage 1"].tasks ["Run procedure"].job.outputParameters.is_empty == true]

Pipeline – Run

Let’s run both stages, the second task of the first stage should be skipped, and the task of the second stage should run.

Let’s run only the second stage. As it checks if it is null, the task should also run.

If you remove the null condition, the task will not run, it will also not throw any errors.

Conclusion

Now, it should be clearer how to create an output parameter, and how to use it to condition steps, tasks, stages, etc…

It should be noted, that if the parameter is not used outside of the procedure, you should probably use properties instead.


Learn more on DevOps: https://www.dbi-services.com/blog/category/devops/ 

Introduction to CloudBees CD/RO: https://docs.cloudbees.com/docs/cloudbees-cd/latest/intro/