Search code examples
if-statementgithub-actions

Unexpected symbol: 'are' for if then else condition check


2 environment variables need to do a equally check before carry on the next sep in my workflow

  - name: version check
    env:
      version_in_code : "v$(poetry version -s)" 
      version_in_tag : $(git describe --exact-match --tags $(git log -n1 --pretty='%h')) 
      version_is_equal: ${{ env.version_in_code != env.version_in_tag }}
    run: |
      if [ ${{ env.version_is_equal }} ]; #here comes the error
      then
      echo " ${{ env.version_in_tag }} will be released"
      else
      git push -d origin ${{ env.version_in_tag }}
      echo "⛔️ Tag-version: ${{ env.version_in_tag }} and Code-version: ${{ env.version_in_code are NOT equivalent"
      exit 1
      fi

Using $env.version_is_equal is not allowed inside the if condition check, and the error message is very confusing: Unexpected symbol: 'are' how can I solve this issue?


Solution

  • There is the syntax issue pointed out by Gui, but there is also a Bash problem: this

    [ ${{ env.version_is_equal }} ]
    

    is either [ true ] or [ false ]. This will always evaluate to true, because the construct checks if it contains a non-empty string; it doesn't care if the string is true or false.

    Then, you can replace ${{ env.version_is_equal }} with $version_is_equal; doesn't make much of a difference, but it's more convenient.

    To fix the comparison, you either have to compare to a string:

    if [ "$version_is_equal" = 'true' ]; then
    

    or run it as a command (without the [ ... ]), which is shorter, but a bit more magic:

    if "$version_is_equal"; then
    

    This works because true and false are commands returning a success/failure exit status.


    Notice that this

          version_in_tag : $(git describe --exact-match --tags $(git log -n1 --pretty='%h')) 
    

    is evaluated only when it is used, and not when it is assigned. Until it is used, it's just a string. This may or may not be the intention, but it's in my opinion more clear to make the assignment part of the script itself:

    version_in_tag=$(git describe --exact-match --tags $(git log -n1 --pretty='%h'))