Variable Expressions
This tutorial will walk you through creating and using variables in Harness CD.
This tutorial builds upon Deploy using Kubernetes Manifest - CD Pipeline. Make sure you have successfully deployed the Guestbook app from that tutorial before proceeding further.
What are variable expressions?
Variable expressions help you paramaterize data and configuration settings in Harness. The types of variables and their scopes are summarized below.
Variable Types | Scopes | Supported Values | ||
---|---|---|---|---|
Harness built-in | :: | Organization | :: | Fixed value |
User-created | :: | Account | :: | Runtime input |
:: | Project | :: | Expression | |
:: | Entities (e.g. pipeline, service) | :: |
Variables are renderd by Harness using an expression language called JEXL.
Before you begin
Verify you have the following:
- A Harness CD account. You can sign up for free if you don't have one.
- A successfull Guestbook app deployment from the Kubernetes Manifest - CD Pipeline tutorial.
Reference built-in variables
- Log into app.harness.io if you have not already done so.
- Navigate to Deployments > Pipelines, and click into your guestbook_canary_pipeline.
- Select Edit Pipeline from the top of the page to enter the Pipeline Studio.
- Select the deploy-guestbook stage tile, then select Execution.
- Select Add Step > Add Step.
- Scroll to the Utilities section, then select Shell Script.
- In the Name field, type
Successful Deployment Message
. - In the Script field, paste the following:
echo "<+pipeline.triggeredBy.name> successfully deployed <+service.name> to <+env.name>."
- Select Apply Changes, then Save.
- Select Run and then Run Pipeline
- Wait for the pipeline to complete, then select the Successful Deployment Message step.
- Expand Step Details if needed, and verify the step log shows the script message with the variable names appropriately rendered.
Create an account-level variable
- Navigate to Account Settings > Account Resources. Select Variables.
- Select New Variable.
- Name the variable account_alias and set its Fixed Value to your first name or nickname.
- Click Save.
You'll notice that variables created at the account, org, or project level support fixed values only. Variables created at the entity level (such as pipelines, stages, and services), support dynamic runtime inputs and expressions.
- Navigate to Deployments > Pipelines > guestbook_canary_pipeline. Select YAML > Edit YAML.
- In the Successful Deployment Message step, on the script: line, replace
<+pipeline.triggeredBy.name>
with<+variable.account.account_alias>
. - Select Save and then Run > Run Pipeline.
- After the pipeline completes, verify that the step log message references the value you set for your account level variable.
Create a project entity-level variable as a runtime input
- In Deployments > Pipelines, select your guestbook_canary_pipeline.
- Select Edit Pipeline, then select Variables from the right sidebar.
- Under Pipeline > Custom Variables, select Add Variable.
- Name the variable region, leave the value blank, then select Set variable as required during runtime.
- Select Save, then Apply Changes.
- In the Pipeline Studio, select YAML.
- At the bottom of the YAML, in the variables block, replace
value: ""
withvalue: <+input>
. - In the Successful Deployment Message step, modify the script line to include the new variable:
echo "<+variable.account.account_alias> successfully deployed <+service.name> to <+env.name> in <+pipeline.variables.region>."
- Select Save.
- Select Run. Note the prompt for a value for the region variable. Enter a value of your choosing (e.g. eu, india, usa, etc.).
- Select Run Pipeline.
- After the pipeline completes, verify that the step log message references the value you set for your account level variable.
Congratulations!🎉
You've just learned how to create and reference variables in a CD pipeline.
What's Next?
- Learn about pipeline triggers.
- Visit the Harness Developer Hub for more tutorials and resources.