'Unable to set Variable in gitlab-ci.yml

In this abridged version of a gitlab-ci.yaml file. The global variable APP_VERSION can either be filled or left blank. In the case of it being left blank, I want to set the variable to the version value that exists in version_cache/version.txt. If APP_VERSION already contains a version, then it will use that.

variable:
  APP_VERSION: ""

build_image:
  stage: build_image
  before_script:
    - APP_VERSION=if [ -z $APP_VERSION ]; then echo $(cat version_cache/version.txt); else echo $APP_VERSION; fi;

When the runner runs this, I get

bash: eval: line 128: syntax error near unexpected token `then'

The runner uses zsh. I saw an example on SO where a variable is set this way, yet I'm unable to understand the missing syntax.



Solution 1:[1]

After checking with shellcheck, try:

$(if [ -z "$APP_VERSION" ]; then cat version_cache/version.txt; else echo "$APP_VERSION"; fi;)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1