Overview
CircleCI orbs do not support list parameters directly, but you can achieve similar functionality by using a delimited string. This guide demonstrates how to pass a colon-delimited string to an orb and iterate over its values, enabling you to handle dynamic data effectively in your workflows.
Use case
Suppose you want to provide multiple values to an orb command and process them individually. Instead of defining a list parameter type (which isn't supported), you can use a string parameter with colon-delimited values. The orb can then parse and iterate over these values during execution.
Configuration example
Below is a complete CircleCI configuration demonstrating how to pass a delimited string to an orb and iterate over its values.
version: 2.1
orbs:
example-orb:
executors:
default:
docker:
- image: cimg/base:stable
commands:
process_list:
parameters:
delimited_list:
type: string
steps:
- run:
name: Process Delimited List
command: |
DELIM_LIST=<< parameters.delimited_list >>
IFS=":"
for item in ${DELIM_LIST}; do
echo "Item: $item"
done
jobs:
hello-world:
executor: example-orb/default
steps:
- checkout
- example-orb/process_list:
delimited_list: "value1:value2:value3"
workflows:
build-and-test:
jobs:
- hello-world
How it works
-
String Parameter:
The orb defines a string parameter `delimited_list` to accept a colon-delimited string. -
Iterating Over the Values:
Inside the `process_list` command, the `IFS` (Internal Field Separator) is set to `":"` to split the string by colons. A `for` loop is used to iterate over the individual values and print each one. -
Workflow Execution:
The `hello-world` job calls the `process_list` command, passing a sample delimited string (`"value1:value2:value3"`). The workflow ensures the command is executed during the pipeline.
Key benefits
- Dynamic Inputs: Allows passing multiple values as a single parameter.
- Reusability: The orb command can be reused with any colon-delimited string.
Extending the example
You can adapt this configuration to:
- Process other types of delimited strings (e.g., `,` or `;` as delimiters).
- Perform additional operations on each value (e.g., passing them to another command or API).
Conclusion
While CircleCI orbs lack native support for list parameters, using a delimited string and iterating over it provides a simple and effective workaround. This approach ensures compatibility with CircleCI’s parameter types and enables flexible data handling in your pipelines.
References
- Docs: Reusable Config Reference Guide - Parameter Types
- Discuss: Circleci orb with array parameters and for loop
Comments
Please sign in to leave a comment.