Çağrı BIYIK
Çağrı BIYIK

Reputation: 96

How to compare two strings in GitHub Actions

I am trying to compare two strings and run some commands according to the results.

Here is my script:

- name: Get Version of Release Branch
  run: echo "branch_version=$(echo $branch_name | sed 's/release\///g')" >> $GITHUB_ENV
      
- name: Get Version of Project
  run: echo "build_version=$(cat notification-abstractions.csproj | grep PackageVersion | sed -e 's/PackageVersion//g' -e 's/<//g' -e 's/>//g' -e 's/\///g' -e 's/^[[:space:]]*//')" >> $GITHUB_ENV


- name: Versions
  run: | 
    echo build version=$build_version
    echo branch version=$branch_version

- name: Fail PR if versions not matching
  if: ${{ '${{ env.branch_version }}' != '${{ env.build_version }}' }}
  run: exit 1

enter image description here

build and branch version is the same but somehow expression returns true.

I have tried:

but none of them works properly.

Upvotes: 2

Views: 5760

Answers (1)

Azeem
Azeem

Reputation: 14617

The version strings should be comparable with the != operator.

I have tested this:

if: ${{ env.build_version != env.branch_version }}

and, it works fine.

Please make sure that the version strings don't have any preceding or following spaces, or newlines.

  • Use tr -d '\n' to remove newline characters if there are any.
  • echo also adds a trailing newline. Use echo -n to suppress that.
  • And, grep can directly take a file as an argument, you don't have to cat and then grep.

Upvotes: 2

Related Questions