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.version returns a valid version only if the passed parameter refers to a released version
  • component.version returns null for 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:

  1. Testing unreleased component versions during development
  2. 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.version has a value (not null), use that value
  • If component.version is null, use the value of component.sha instead

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): uses image:v1.2.3
  • When including @feature-branch (unreleased): uses image:<sha>
  • When including @abc123def (SHA): uses image:abc123def

Implementation Notes

  1. This is a follow-up to the component context implementation in #438275
  2. The default function should work with any interpolation expression, not just component context
  3. 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 🤖