Variables
Variables are fundamental to creating dynamic and interconnected Bosun workflows. They allow you to store and reference data throughout your tasks, enabling complex logic and data flow between different steps and agents.
Bosun provides several types of variables, with two common ones being:
outputs: Used to access the results or output of a previous step.for_each.value: Used within afor_eachloop to refer to the current item being processed.
Accessing Step Outputs (outputs)
Each step in a Bosun workflow can produce an output. You can reference these outputs in subsequent steps using the outputs.<step_index> syntax, where <step_index> is the zero-based index of the step in the workflow.
Example from Workflow:
- name: Get source files
run: find src -name "*.ts" # This is step 0 (0-indexed)
- name: Analyze files
for_each:
from: '{{ outputs.0 | split(pat="\n") | slice(end=5) | json_encode() }}'
# ...
In this snippet:
- The
run: find src -name "*.ts"command is the first step in this example (index 0). It outputs a list of TypeScript file paths. - The
for_eachstep then usesoutputs.0to retrieve this list. The templating expression{{ outputs.0 | split(pat="\n") | slice(end=5) | json_encode() }}processes this output to prepare it for the loop.
Referencing Outputs by Step ID
When you need more descriptive references than numeric indexes, add an id field to a step:
- id: fetch_sources
name: Get source files
run: find src -name "*.ts"
- name: Analyze files
for_each:
from: '{{ outputs.fetch_sources | split(pat="\n") | filter(value) | json_encode() }}'
# ...
IDs must start with a letter or underscore and can contain alphanumeric characters, underscores, and hyphens (kebab case).
Iterating with for_each.value
When you use a for_each loop, Bosun automatically makes the current item of the iteration available as for_each.value. This allows the agent or run command within the loop to operate on each item individually.
Example from Workflow:
- name: Analyze files
for_each:
from: '{{ outputs.fetch_sources | split(pat="\n") | slice(end=5) | json_encode() }}'
agent:
extends: Coding
instructions: "Refactor the code in file {{ for_each.value }} to improve readability."
# ...
Here:
- The
for_eachloop iterates over the list of file paths. - For each iteration,
{{ for_each.value }}holds the current file path (e.g.,src/utils/helper.ts). - This value is then injected into the
instructionsfor theCodingagent, allowing it to specifically refactor that particular file.