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.
I tried three methods:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With