CI YAML default interpolation function for fallback values
Summary
Implement default as an interpolation function to allow fallback values when the primary interpolation resolves to null.
Problem
In issue #438275, we're implementing $[[ component.version ]] and $[[ component.sha ]] to provide component context data. However:
-
component.versionreturns a valid version only if the passed parameter refers to a released version -
component.versionreturnsnullfor unreleased components (e.g., when using a branch name or SHA for testing)
Users need a way to gracefully handle scenarios where component.version is null, particularly when:
- Testing unreleased component versions during development
- Referencing container images that should match the component version in production, but fall back to SHA during testing.
Proposal
Implement a default interpolation function that provides fallback behavior:
$[[ component.version | default component.sha ]]
Behavior:
- If
component.versionhas a value (notnull), use that value - If
component.versionisnull, use the value ofcomponent.shainstead
Example use case:
spec:
component: [version, sha]
inputs:
---
my-job:
image: registry.gitlab.com/my-component/image:$[[ component.version | default component.sha ]]
script:
- echo "Running component"
Result:
- When including
@v1.2.3(released version): usesimage:v1.2.3 - When including
@feature-branch(unreleased): usesimage:<sha> - When including
@abc123def(SHA): usesimage:abc123def
Implementation Notes
- This is a follow-up to the component context implementation in #438275
- The
defaultfunction should work with any interpolation expression, not just component context - Should support chaining:
$[[ a | default b | default c ]]???
Benefits
- Enables seamless testing of unreleased components without requiring separate logic
- Simplifies component templates by removing the need for workarounds
- Provides a consistent pattern for handling optional/nullable values in interpolation
Edited by 🤖 GitLab Bot 🤖