Skip to content

Support variables in CI inputs with predefined function `expand_vars`

Leaminn Ma requested to merge ci-interpolation-expand-vars into master

What does this MR do and why?

This MR adds support for a new predefined function expand_vars to be optionally used in a CI interpolation block. This function:

  • Expands the variables within an input value.
  • Variables are only expanded one-level; nested variables are not supported. File type variable refs are expanded.
  • Raises a validation error if a masked variable is detected.

Resolves #387632 (closed)

How to set up and validate locally

  1. Set up your Project with the following CI/CD variables in your Project Settings.
  • key: MY_VAR, value: 123456789, protect: false, mask: false, expand: false
  • key: NESTED_VAR, value: $MY_VAR, protect: false, mask: false, expand: true
  • key: MASKED_VAR, value: ABCDEFGH, protect: false, mask: true, expand: false
  1. In your Project root, create a templates directory. Inside this directory, create a file component1.yml with the following contents:
spec:
  inputs:
    my_var:
      default: 'TEST-${MY_VAR}'
    nested_var:
      default: $NESTED_VAR
---
1:$[[ inputs.my_var ]]:
  script: echo $[[ inputs.my_var ]]

2:expand:$[[ inputs.my_var | expand_vars ]]:
  script: echo $[[ inputs.my_var | expand_vars ]]

3:truncate:$[[ inputs.my_var | truncate(0,9) ]]:
  script: echo

4:expand-and-truncate:$[[ inputs.my_var | expand_vars | truncate(0,9) ]]:
  script: echo $[[ inputs.my_var | expand_vars | truncate(0,9) ]]

5:nested:$[[ inputs.nested_var ]]:
  script: echo $[[ inputs.nested_var ]]

6:nested:expand:$[[ inputs.nested_var | expand_vars ]]:
  script: echo $[[ inputs.nested_var | expand_vars ]]

7:multiple:$[[ inputs.my_var | expand_vars | truncate(0,9) ]]--nested:$[[ inputs.nested_var | expand_vars | truncate(0,3) ]]:
  script: echo $[[ inputs.my_var | expand_vars | truncate(0,9) ]]--$[[ inputs.nested_var | expand_vars | truncate(0,3) ]]
  1. Go to the Pipeline editor. Update your config file with the following (adjust the component path as needed):
include:
  - component: gdk.test:3000/group-c/project-6/component1@main
  1. Confirm in the "Full configuration" tab that the functions applied correctly. Observe that the inner variable of NESTED_VAR is not expanded.

Screenshot_2023-09-13_at_6.59.33_PM

  1. Now update the config so that a masked variable is passed into the input:
include:
  - component: gdk.test:3000/group-c/project-6/component1@main
    inputs:
      my_var: '$MASKED_VAR'
  1. Observe that a validation error occurs:

Screenshot_2023-09-18_at_8.07.40_PM

  1. (Optional) Change the project variable MY_VAR into a File type variable. Go to the Pipeline editor and update your config file as in Step 3. The result should be the same as in Step 4.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #387632 (closed)

Edited by Leaminn Ma

Merge request reports