1.1.1 Passing data between actions

 

 

1. What is the purpose of passing data between actions in a pipeline?

The main reason why we need to pass data between actions in the workflow is for creating more interaction between them, if one action depends on another action or if one uses data from another. The most common case is when the output or the partial output from one action is used as the input for another action.

 

 

 

 

2. What kind of information can you pass?

When we talk about passing data between actions, usually it’s for passing the output (the final result) of an action but passing other information about the action may be useful while constructing the logic of your workflow.
Examples:

  • Action Results: actions.<Action-ID>.result (result may be a string, numeric, boolean or JSON value)
  • Action Status: actions.<Action-ID>.status Helps you to check whether the action succeeded or not. Available statuses include: error, success, skipped, running, and stopped.
  • Information from Configurations: Kaholo.pipeline.configurations.<your_config_value>
  • Any Variable, or Function result from the code layer. You can also process results using a function, before passing it to the next action

To see more possible information that you can access, see this documentation: KAHOLO SDK TABLE

 

 

3. Checking data types

In most cases just a quick look at the execution results allows you to evaluate if your output is a string, numeric, boolean, or JSON value. But there could be cases were you end up dealing with a data type you weren’t expecting.
Most common cases:

  • A curl command response should normally be a JSON but it may also include response headers or additional text. Then the output would be a string. To avoid this use curl -s
  • A numeric value that looks like an integer or a float but is really a string. Use function Number() to convert the data. You can also use parseInt(),or parseFloat()
  • A returned value of true/false that is a string instead of boolean. You can transform it with JSON.parse(). This function converts it into an object corresponding to the given text.


 let boolValue = JSON.parse(stringValue);  // returns boolean true 

 

To evaluate the datatype of a returned value you can use this function in the main code tab (change <Action-ID> to the ID of your action):

 

function checkType(){
type = typeof(actions.<Action-ID>.result);
return `echo ${type}`;}; 

 

And using the command line action, switch the command field to code and call the function

 checkType()

After executing the pipeline, the returned information in Execution Results the data types.

 

 

4. Example of passing Strings, Numbers, or Boolean results

If you want to pass the full result that is a string, numeric or boolean result to the next action as a parameter input, switch the parameter code toggle on and use the Kaholo SDK as follows:

Action 1
In our example, we’ll set the ID of Action 1 to textfile. We’ll then reference this id when calling this action’s result data in the next action’s parameter input.

Action 2
Add a second action and toggle the code switch on a parameter and type:

   actions.textfile.result   

 

 

5. Example of passing a value from a JSON result

If you want to pass one value from the JSON result from the first action you can use the JavaScript dot and bracket notations as in the following example:

In this example, we have a pipeline that has two actions. Action 1 creates an EC2 machine, and Action 2 uses only a partial result from Action 1 and prints it to Slack. After execution, Action 1 returns a JSON value (picture below). As you can see, data is sorted and displayed like a tree. Important is to understand that if you need to access a value that is one level below, you need to call each of them after a dot (.).

Action 1
In this example, we’ll set the ID of Action 1 to amazonec1

Action 2
Add a second action and toggle the code switch of a parameter and type:

   actions.amazonec1.result.Instances[0].ImageId     

 

In this example, we’re trying to access the state of an instance, but the results returned an array of instances so we’re using the bracket notation to specifiy which element of the array to get into.

So if we want to access the monitoring state, we should write:

  actions.amazonec1.result.Instances[0].Monitoring.State   

In this example, it will return the string: disabled

 

 

6. Passing Data with JS Functions

If you need to process or transform the output from Actions, you can do it with JavaScript functions that can be defined in any code field, but typically you will want to use the main code layer to keep things cleaner. There, you can use JavaScript functions to call results from Actions and return the values. Inside the function, you may do any transformation that you need like converting data types, preprocessing (preparing raw data for another procedure), etc.)
Here’s a simple example of a function that returns the output from an Action – and prepares the information to send to Slack:

function getSlackMessage(){
return `New Instance ID: ${actions.createec2.result.runInstances.Instances[0].InstanceId}`}; 

To get the result of this function, you can call it by turning on the toggle switch above a parameter field and insert the following value:
 getSlackMessage() 

 

You can also access Vault items using functions. You can do this by using the async function.

async function getCmd(){
return `echo ${await kaholo.vault.getValueByKey("Vault_name")}`}; 

To get the result of this function, you can call it by turning on the toggle switch above a parameter field and inserting the following value:
 getCmd() 

Learn more about functions in Kaholo

 

While working with JSON objects you can get output like: [Object object]. This happens when you try to print the whole JSON object directly in the shell. With bash it is not possible to print datatype Object, before converting it to string. You can use function JSON.Stringify() if you need whole content as a String.

 

Summary:
Kaholo enables you to build advanced pipelines involving passing and manipulating data from one action to another. If want to explore passing data further, you can use our pre-built templates under the Basic Operations category in the template gallery.