How to use conditions in .gitlab-ci.yml variables?

It is possible:

Add your logic into the variable section:

  VERSION_LOGIC: '(if [ "$${CI_COMMIT_TAG}" == "" ]; then echo "1.3.5.$$CI_PIPELINE_IID"; else echo "$${CI_COMMIT_TAG}"; fi);'

Now you are able to use this logic in a script secion of a job:

  stage: versioning
    - echo "The current version is set to ${VERSION}."

This is expected behavior.

CI_COMMIT_TAG is only set to a value in a GitLab job. From

CI_COMMIT_TAG - The commit tag name. Present only when building tags.

Therefore in the variables section CI_COMMIT_TAG is not defined, hence equals to "".

So if you want to use CI_COMMIT_TAG use in job where tags are defined. See