Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"rule if invalid expression syntax" using boolean inputs interpolation

Using this GitLab CI component code:

# component.yml
spec:
  inputs:
    job1-enabled:
      default: true
      type: boolean
    job2-enabled:
      default: true
      type: boolean

---

job1:
  script:
    - echo $[[ inputs.job1-enabled ]]
  rules:
    - if: $[[ inputs.job1-enabled ]]  # jobs:job1:rules:rule if invalid expression syntax

job2:
  script:
    - echo $[[ inputs.job2-enabled ]]
  rules:
    - if: $[[ inputs.job2-enabled ]]

and this .gitlab-ci.yml:

include:
  - project: $CI_PROJECT_PATH
    ref: $CI_COMMIT_REF_NAME
    file: component.yml
    inputs:
      job2-enabled: false

I get this error:

Unable to create pipeline
    jobs:job1:rules:rule if invalid expression syntax

I noticed this issue: GitLab CI "if invalid expression syntax" when using CI/CD inputs Unfortunately, the fix suggested does not apply here since the datatype is not a string, but a boolean, making the quoting of the input interpolation irrelevant.

like image 625
Dorian Turba Avatar asked Sep 12 '26 12:09

Dorian Turba


1 Answers

I tried three methods:

  • using variables
  • using variables and string comparison
  • stringify the input and string comparison

The first one removed the error but led to a strange result: both jobs were running when the second job should have been disabled.

Then I found this GitLab issue. In a nutshell, it explains that even if inputs are explicitly typed as boolean, this is of the type string.

That's when I remembered this solution: since the input is interpreted as a string and that the interpolation in variable creates this if: - if: true", which is always true. Using a variable WITH a string condition seems to solve the issue: - if: $ENABLED == "true" worked but then, I realized that this variable fix was a convoluted way to stringify the input.

That's when I moved to the third solution, the same as the one of this question, but that's a coincidence caused by input interpolation:

# component.yml
spec:
  inputs:
    job1-enabled:
      default: "true"
    job2-enabled:
      default: "true"

---

job1:
  script:
    - echo $[[ inputs.job1-enabled ]]
  rules:
    - if: '"$[[ inputs.job1-enabled ]]" == "true"'

job2:
  script:
    - echo $[[ inputs.job2-enabled ]]
  rules:
    - if: '"$[[ inputs.job2-enabled ]]" == "true"'
# .gitlab-ci.yml
include:
  - project: $CI_PROJECT_PATH
    ref: $CI_COMMIT_REF_NAME
    file: component.yml
    inputs:
      job2-enabled: "false"

Pipeline result: passed.

Edit: I now found a place in gitlab documentation where this is explained: https://docs.gitlab.com/ci/inputs/examples/#use-cicd-inputs-in-variable-expressions

like image 160
Dorian Turba Avatar answered Sep 15 '26 02:09

Dorian Turba