Pass Variables between Workflows
Harness variables can be used as parameters in many settings, and you can create and output variables using different Workflow steps. Once created, you can reference variables using expressions in steps in the same Workflow or across Workflows in a Pipeline.
Let's look at some examples:
- Passing Jenkins environment variables from a Build Workflow to populate configuration values in a subsequent Harness Workflow.
- Pass AWS resource attributes such as ARNs into a CloudFormation template.
- Pass the outputs of a Terraform provisioning Workflow into a JIRA ticket, and update the ticket throughout deployment stages.
Before You Begin
To obtain an understanding of Harness variable expressions and the Workflow steps that use them, ensure you're read the following:
- Variables and Expressions in Harness
- Add Service Config Variables
- Set Workflow Variables
- Passing Variables into Workflows and Pipelines from Triggers
- Jira Integration
- Using the HTTP Command
- Using the Jenkins Command
- Use Terraform Outputs in Workflow Steps
- Using CloudFormation Outputs in Workflow Steps
Prerequisites
This topic uses an existing Harness setup to explain passing variables. To follow along, you can set up the following in your account:
- 2 Harness Application Workflows
- 1 Harness Application Pipeline
Step 1: Publish Outputs
To demonstrate publishing outputs, we'll create a Workflow and use the Shell Script step to create and publish variables.
- Create a Workflow, or use an existing workflow in your Application.
- In your Workflow, add a Shell Script step.
- In the Script section, declare some variables. For example:
name=nora
export name
last=smith
export last
In Script Output, list out all the environment variables that you want to reference later on. We'll use the variable names
name, last
.Enable Publish output in the context.
In Publish Variable Name, enter a unique parent name for all of the output variables. This is the parent variable that you will use to call the
name
andlast
variables elsewhere.
We'll use the nameinfo
.Do not use reserved words in Publish Variable Name, such asvar
. See Variable Expression Limitations and Restrictions.In Scope, select Pipeline so we can reference these variables in the second Workflow in the Pipeline. When you're done, the step will look like this:
Click Submit.
The Workflow should look something like this:
Step 2: Echo Variables
Next, we'll create another Workflow that echos the variables you published in the first Workflow using the context variable you created.
Create a second Workflow and add a Shell Script step.
In Script, reference the outputs from the first Workflow Shell Script step by entering the following:
```
echo {context.info.name} echo {context.info.last}
```We use the
context
variable to reference variables in context.context
is a built-in Harness variable. Theinfo
variable is what you have assigned to reference the env variablesname
andlast
.The Shell Script step will look something like this:
Click Submit.
You Workflow should look something like this:
Step 3: Pass the Variables in a Pipeline
Now that we have a Workflow pushing variables and a Workflow echoing them, we can create a Pipeline using the Workflows.
Create a Pipeline with two stages. See Create a Pipeline.
For Stage 1, select the first Workflow you created, where the variables are published.
For Stage 2, select the second Workflow, where you echoed the variables. Your Pipeline will look something like this:
Deploy the Pipeline and review the execution logs. You'll see the Shell Script step in the first Workflow publish the variables:
Next, in stage 2 of the Pipeline, you'll see the Shell Script step in the second Workflow echo the variables.
Review: Passing Variables between Workflows
Using this simple method of publishing variables, teams can tackle a wide variety of use cases.
If you want to share some ideas, join the Harness Community.