Add default() interpolation function for CI/CD inputs

What does this MR do and why?

Adds a default interpolation function. It replaces an empty value with a fallback, so a component template can use its own version and still work when it is included at a branch or commit SHA:

spec:
  component: [version, sha]
---
build:
  image: registry.example.com/my-component:$[[ component.version | default(component.sha) ]]

component.version is nil unless the component is included at a released version, which breaks templates (and their tests) that include the component at $CI_COMMIT_SHA.

The function is behind the ci_interpolation_default_function beta feature flag, disabled by default. Rollout issue: #638368

Proposed semantics (open for the group to confirm)

The issue has no agreed design yet, so this MR proposes one. Happy to change any of these:

  • Syntax: default('<literal>') or default(<context>.<field>), with parentheses like truncate(0,7) and split(','). The issue description writes default component.sha, which would be new grammar for functions.
  • What counts as empty: nil and "". false, 0 and [] are real typed values and pass through. Only nil is not enough, because string inputs are never nil: Ci::Inputs::StringInput#coerced_value turns nil into "". A nil-only default would not help the cases from the issue comments, for example $[[ inputs.image_tag | default(component.version) ]] with image_tag: { default: "" }.

Alignment with the existing functions

  • The literal argument is single-quoted with no escape support, exactly like split('<separator>').
  • No whitespace inside the parentheses, like split, the other function with a string argument. truncate allows whitespace around its integer arguments, but inside a quoted literal whitespace is part of the value, so the stricter split form is less ambiguous.
  • ^...$ anchors and #{name} in the pattern, and invalid argument: ... errors, as in split.

Deviation: an access as an argument

No existing function takes a context access as an argument. default(component.sha) resolves its argument with the same Interpolation::Access and context as the block itself, so it accepts the same forms (inputs.name, component.sha, inputs.list[0]) and returns the same errors (for example unknown interpolation provided), prefixed with error in \default` function: invalid argument:. The argument is resolved even when the value is present, so a typo like default(component.shaa)` fails for every ref, not only for consumers who include the component at a SHA.

Other behaviour

  • Chaining works with the existing stack: $[[ inputs.tag | default(component.version) | default(component.sha) ]], within the limit of 3 functions per block.
  • default must come before string functions like truncate, which return an error for nil. This is documented.
  • The fallback value is not interpolated again, the same as any input value.
  • A literal cannot contain |, because Block splits the block on |. Same limitation as split('|'); documented.
  • The feature flag is checked only when an expression matches default, so pipelines that do not use it do not evaluate the flag. This also avoids stubbing the flag across the external file specs, which the split() rollout needed.
  • Security::CiConfiguration::BaseCreateService now loads the project's .gitlab-ci.yml inside Gitlab::Ci::Config::FeatureFlags.with_actor(project), like CloudSeed::GoogleCloud::GeneratePipelineService already does. Without it, a .gitlab-ci.yml with a spec:inputs header that uses default() checks the flag with no actor, so a project-scoped enablement reads as disabled and creating the SAST or Secret Detection configuration MR fails. All other callers that interpolate a project's CI config already set an actor.

Possible follow-ups

Out of scope for this MR, error messages stay as they are:

  • A hint for the bare default <access> form, which the issue proposes. Today default component.sha only gets no function matching, with no pointer to default(component.sha).
  • A clearer error for | inside function arguments, shared with split. Today default('a|b') reports the fragments default('a and b').
  • On an unknown component.* field, tell users to add the field to spec:component.

References

Screenshots or screen recordings

Not applicable, no UI change.

How to set up and validate locally

  1. Enable the feature flag in the Rails console:

    Feature.enable(:ci_interpolation_default_function)
  2. In a project, add a component at templates/my-component/template.yml:

    spec:
      component: [version, sha]
    ---
    my-job:
      image: alpine:$[[ component.version | default('latest') ]]
      script: echo "$[[ component.version | default(component.sha) ]]"
  3. In another project, include it at a commit SHA and at a released version with include: component: <host>/<path>/my-component@<ref>.

  4. Check the pipeline: at a SHA the job uses alpine:latest and echoes the SHA. At a released version it uses the version.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Timo Furrer

Merge request reports

Loading
Loading