Using Variables in Script Code
Trickest allows users to enhance their workflows with custom scripts, and variables play an essential role in this process. This page covers how to use variables within scripts, whether for a Bash shell or Python code.
Managing Variables in Your Workflows
In Trickest, variables are used to create workflows by dynamically referencing data. Our platform supports two types of variables:
-
Global Variables: These are universally accessible within your Trickest account and can be used across multiple workflows. They are ideal for storing data that remains constant across different projects.
-
Space Variables: These variables are specific to each workspace. They are useful for project-specific data and can override Global Variables in the same workspace.
Utilizing Variables in Bash Scripts
In bash scripts within Trickest Editor, variables can be referenced directly in the code using the syntax echo ${{vars.VARIABLE_NAME}}
. This allows you to dynamically insert the value of a variable into your scripts.
Bash Script Example
Let's say we want to download the latest version of mksub through the script by referencing the URL for binary in a Space Variable.
Let's call it mksub_binary
, which will use ${{vars.mksub_binary}}
variable syntax in the Editor.
Now, with the variable created, we can create a simple wget
to download the binary to the out
directory.
- Using custom-script
We will drag & drop the custom script to the Editor.
- Writing the script using variables
Now, let's execute the workflow.
Python Script Example
With Python as an example, we want to send a request with a custom Header being used inside a variable.
Let's create a variable custom_header
with value of foo
Now, let's drag & drop the Python script.
We can use a simple Python script for sending requests with custom headers.
After running the script, we can see we've got the request with the header value from our space variable.
Best Practices
-
Descriptive Naming: Choose variable names that clearly describe their purpose.
-
Error Handling: Implement error checking around variable usage to gracefully handle unexpected or missing values.
-
Consistency: Maintain a consistent variable naming convention and usage pattern across all scripts within a project.$
By harnessing the power of variables within code, Trickest workflows become significantly more powerful, adaptable, and maintainable. Variables enable scripts to operate in various contexts, reducing the need for duplicate code and enhancing the security and manageability of the automated processes.
Go back to Using variables for string inputs