Dynamic Formation YAML Multiline String (Multiple Lines) Blocks (Edited)
I am creating a universal CI/CD pipeline that should use a variable group whose name depends on the pipeline definition’s name. To achieve this, I attempted to set the group name dynamically.
For example:
group: $(Pipeline.DefName).Prod
Or another variant using template syntax:
group: {{ variables.pipelineDefName }}.Prod
However, neither of these approaches works.
The only method that succeeded was using a parameter:
group: ${{ parameters.pipelineID }}.Prod
But it is inconvenient to pass the parameter each time the pipeline is run.
At the same time, my YAML file contains long string blocks for which preserving formatting (line breaks, indentation, etc.) is important. For example, a block like:
description: | Line A Line B Line C
Here the literal style preserves the multiple lines and newlines exactly as written—including every newline and keeping the indent intact. Moreover, it is necessary to control both the leading and trailing line breaks (using block chomping operators “|+” or “|-”). This way, you can control the end of the string and maintain overall readability. Such an approach ensures that if the text should not fold into a single paragraph, the YAML parser will interpret the block to indicate exactly how each newline (or even a folded style option, where each newline is replaced by a space character) should behave.
Question:
How can the problem of dynamically forming a variable group name (without explicitly specifying the parameter at every run) be solved, while simultaneously ensuring the correct preservation of multi-line string formatting in YAML?
Answers
Adam Kowalski
5 months ago
Rating
Solution for Dynamically Forming the Variable Group Name (string in yaml over multiple lines)
Here is my perspective on the problem.
Using Predefined Variables of the CI/CD Platform (literal style, block scalar)
In certain systems, such as Azure DevOps, there are predefined variables that contain the pipeline’s name. For example, in Azure DevOps:
Why It Works:
Build.DefinitionName is a predefined variable that automatically contains the pipeline definition’s name.
The syntax $${{ ... }} is processed at the YAML compilation stage, enabling dynamic formation of the group name. This method ensures that the proper string value is interpreted and denoted over multiple lines.
Combining with Default Parameters (multi-line strings)
If the predefined variable is insufficient, you can define a parameter with a default value:
Why It Works:
The pipelineID parameter automatically takes the value of Build.DefinitionName if it isn’t explicitly provided. This ensures that the YAML interpreter correctly reads the default and denotes the intended string spanning several lines in the configuration.
Multiline YAML Block Formatting (folded style, multiple lines, indentation indicator)
Use block scalar operators to format multiline strings:
Why It Works:
The operators |+, |-, and | control how newlines are handled in accordance with the YAML standard. You can also choose a folded style (>) when you want to fold the multiline string into a single line by replacing each newline with a space character. This flexibility helps break a string in yaml while preserving the intended indentation and overall format.
Final Example (long string format with proper break a string in yaml):
In this final example, the YAML is written as a long string over multiple lines. The use of block scalars ensures that the multiline string maintains its indentation and correctly breaks a string in yaml as needed.
Advantages of This Approach (ensuring readability)
Does not require manual parameter passing (uses a default value).
Works at the YAML compilation stage, ensuring predictability.
Applicable in systems that support this syntax (e.g., Azure DevOps).
The method guarantees that the string in yaml over multiple lines is handled correctly, maintaining the natural clarity of the file.
Recommendations (maintaining format and indentation)
Verify your CI/CD system’s documentation to confirm the names of predefined variables.
For runtime logic, use scripts (e.g., PowerShell/Bash) to generate dynamic values.
Stephan Rainer
5 months ago
1 comment
Rating
I can offer an alternative approach – isolating part of the pipeline into a template, where both the dynamic variable group and the multi-line text formatting are defined through parameters. This allows for centralized management of variables and avoids having to pass them in with every run.
Example Template (deploy-template.yml):
Explanation:
This approach minimizes duplication and centralizes parameter management while guaranteeing that multi-line values are correctly preserved or folded as desired.
Bruno Cesar
5 months ago
Rating
This approach is excellent for scaling and reducing code duplication. The use of templates means that multi-line and dynamic values are managed centrally, which helps the YAML parser to parse and indicate variables automatically.
Pros:
Scaling Simplification:
Templates offer centralized control over pipeline logic and reduce code duplication, especially when handling multi-line YAML blocks with proper indent and preservation of newlines.
Parameters with Default Values:
Defining a parameter (e.g., environment) with a default value allows pipelines to run without manual intervention, ensuring that the parser correctly indicates and manages the multiple lines without unwanted folding—unless a folded style is explicitly desired.
Cons:
Extra Setup for Simple Cases:
In simpler pipelines, configuring and maintaining templates may be seen as unnecessarily complex, potentially impacting clarity.
Parameter Automation:
The example does not show how to pass parameter values automatically (for instance, via environment variables or pipeline settings), which is important when you need the system to parse and fold text automatically under different circumstances.
Emmerich Lothar
5 months ago
1 comment
Rating
Solution:
The issue is that directly using pipeline variables in the variable group definition is not supported. One approach is to use a predefined variable that is accessible via the variables object with the correct notation. For example, if the pipeline already has a variable named Pipeline.Definition (which can be set in the pipeline settings), you can do as follows:
In this case, the platform will substitute the variable’s value, and the suffix “.Prod” will be appended during YAML compilation. The YAML system will evaluate the expression and indicate the proper value from the predefined variable.
Multiline Formatting:
To preserve line breaks in YAML for long descriptions or configuration fragments, use the literal style. For example, consider the following YAML:
The “|-” symbol ensures that extra empty lines at the end of the string are removed, while the newlines inside the block are preserved exactly, keeping the multi-line format and indent intact for optimal readability. Alternatively, if you need to fold the lines, you can use the folded style (using >), where each newline is replaced by spaces — this approach merges the multiple lines into one, though it may affect visual clarity when the output is later interpreted.
Bruno Cesar
5 months ago
Rating
I appreciate that the syntax $${{ variables[...] }} is used correctly to access the necessary values and that the explanation of how YAML blocks work with “|” and “|-” is clear. The solution explains how to learn how to break the block, as the YAML parser interprets it and indicates the correct newlines and indent properties.
Pros:
Correct Use of Syntax:
Using the $${{ variables[...] }} construct is indeed the proper way to reference predefined variables at the YAML compilation stage. The system will evaluate the expression and indicate the intended value, ensuring that the variable group name is generated dynamically.
Clear Explanation of YAML Block Behavior:
The description of the literal style shows how it preserves multiple lines and newlines while removing extra empty lines at the end of the string. This is critical to keep the multi-line configuration blocks correctly formatted and with proper indent.
Cons:
Dependence on a Specific Variable:
The proposed approach relies on the presence of the variable Pipeline.Definition (or its equivalent, such as Build.DefinitionName in Azure DevOps). If such a variable is not defined or a different naming convention is used, the solution becomes inapplicable. This can be a limitation if the YAML parser does not parse a variable that indicate the expected value.
Compilation Stage Only:
This solution works only at the YAML compilation stage, not at runtime. If dynamic logic is needed during execution, this method may not allow you to fold or adjust multiple lines dynamically.