Unlocking workflow flexibility with variable-sharing between parent and child pipelines
We’re excited to release our first major set of enhancements to the recently released Parent/Child Pipelines feature for Bitbucket Pipelines.
Bitbucket Pipelines currently supports enabling users to orchestrate workflows by invoking child pipelines from a parent pipeline step. However, until now, there was no way to pass information from a parent step to a child pipeline step. With this feature, you can now share variables efficiently.
This new capability allows you to define variables in the parent step and make them accessible in child pipelines. The child pipeline will pass these variables as custom pipeline variables.
How it works
In your pipeline YAML file, start by adding an input-variables section within the parent step and pass variables from the parent to the child pipeline. Here is an example:
pipelines:
custom:
a-parent-pipeline:
- variables:
- name: parent_pipeline_variable
default: "value_1"
allowed-values:
- "value_1"
- "value_2"
- step:
name: 'Child pipeline'
type: pipeline
custom: a-child-pipeline
input-variables:
var_1: $parent_pipeline_variable
var_2: $repository_variable_name
var_3: $workspace_variable_name
var_4: "static-value-1"
a-child-pipeline:
- step:
name: 'Child Pipeline step'
script:
- echo "Parent Pipeline Step Variable -" $var_1
- echo "Parent Pipeline Step Variable -" $var_2
In this example, a new input-variables section containing 4 variables is defined in the step within the parent pipeline that calls the child. Dynamic variables are defined with $ sign. In this example, a custom pipeline variable, a repository variable, a workspace variable, and a static variable are defined in the input-variables section. The values of these variables will be resolved and passed as custom pipeline variables to the child pipelines.
Static strings are wrapped in double quotes, dynamic variables use the $variable_name syntax.
You can restrict the values that can be provided to a child pipeline by declaring a named pipeline variable in the .yaml, and specifying a list of allowed-values. Here is the example
a-parent-pipeline:
- variables:
- name: parent_pipeline_variable
default: "value_1"
allowed-values:
- "value_1"
- "value_2"
- step:
name: 'Child pipeline'
type: pipeline
custom: a-child-pipeline
input-variables:
var_1: $parent_pipeline_variable
var_2: $repository_variable_name
var_3: $workspace_variable_name
var_4: "static-value-1"
a-child-pipeline:
- variables:
- name: var_4
default: "static-value-1"
allowed-values:
- "static-value-1"
- "static-value-2"
- step:
name: 'Child Pipeline step'
script:
- echo "Parent Pipeline Step Variable -" $var_4
Limitations
- Security: This feature is not intended for sharing secrets. Doing so would involve a wide range of security risks, including the fact that all shared variables will be logged in plain text in the build logs.
- Limits: You can define a max of
20input-variables in a parent step to be passed to a child pipeline. - Validation: Existing validation rules apply to variable key names. They must be alphanumeric, can contain only underscores, and should not begin with a number. Variable values should be a string or a dynamic variable starting with
$sign.
Feedback
We’re always keen to hear your thoughts and feedback – if you have any questions or suggestions on this feature, feel free to share via the Pipelines Community Space.