0

I want to compare my current deployed version against the version mentioned in my 'package.json'. If the version number in the json is higher than the current one (mentioned in my pushed Tags (Release)) I want to deploy it with GitHub Actions.

Im not able to compare the two numbers/Integers in the Github Actions yaml file.

Currently I'm doing this:

- name: Clean Versions
  run: |
        echo "TAG_VERSION=$(echo ${{env.TAG}} | tr -dc '0-9')" >> $GITHUB_ENV
        echo "PACKAGE_VERSION=$(echo ${{env.PACKAGE}} | tr -dc '0-9')" >> $GITHUB_ENV

- name: Compare Versions
  run: |
    if [ $(( ${{env.TAG_VERSION}} )) >> $(( ${{env.PACKAGE_VERSION}} )) ]; then
        echo "PUSH=true" >> "$GITHUB_ENV"

    elif [ ${{env.TAG_VERSION}} == ${{env.PACKAGE_VERSION_NUMBER}} ]; then
        echo "PUSH=false" >> "$GITHUB_ENV"

    else
        echo "PUSH=false" >> "$GITHUB_ENV"
    fi 

Its not delivering the result I want:

enter image description here

Its looking good at first sight, but delivers:

PUSH: true

Is there a nice and native way to compare those version numbers in the if condition?

Ive also tried it to convert the numbers like this, but same output:

- name: Clean Versions
  run: |
        echo "TAG_VERSION=$(echo ${{env.TAG}} | tr -dc '0-9')" >> $GITHUB_ENV
        echo "PACKAGE_VERSION=$(echo ${{env.PACKAGE}} | tr -dc '0-9')" >> $GITHUB_ENV

- name: Get Numbers
  run: |
    echo "TAG_NUMBER=$echo$((${{env.TAG_VERSION}} - 0 ))" >> $GITHUB_ENV
    echo "PACKAGE_NUMBER=$echo$((${{env.PACKAGE_VERSION}} - 0 ))" >> $GITHUB_ENV

- name: Compare Versions
  run: |
    if [ ${{env.TAG_NUMBER}} >>  ${{env.PACKAGE_NUMBER}} ]; then
        echo "PUSH=true" >> "$GITHUB_ENV"

    elif [ ${{env.TAG_NUMBER}} == ${{env.PACKAGE_NUMBER}} ]; then
        echo "PUSH=false" >> "$GITHUB_ENV"

    else
        echo "PUSH=false" >> "$GITHUB_ENV"
    fi     

Output:

enter image description here enter image description here

JulianBPL
  • 149
  • 1
  • 10

1 Answers1

1

Just changed the if bracktes from "[ ... ]" to "(( ... ))" and it worked perfect:

- name: Clean Versions
  run: |
        echo "TAG_VERSION=$(echo ${{env.TAG}} | tr -dc '0-9')" >> $GITHUB_ENV
        echo "PACKAGE_VERSION=$(echo ${{env.PACKAGE}} | tr -dc '0-9')" >> $GITHUB_ENV

- name: 
  run: |
    echo "TAG_NUMBER=$echo$((${{env.TAG_VERSION}} - 0 ))" >> $GITHUB_ENV
    echo "PACKAGE_NUMBER=$echo$((${{env.PACKAGE_VERSION}} - 0 ))" >> $GITHUB_ENV

- name: Compare Versions
  run: |
    if (( ${{env.PACKAGE_NUMBER}} > ${{env.TAG_NUMBER}} )); then
        echo "PUSH=true" >> "$GITHUB_ENV"

    elif (( ${{env.PACKAGE_NUMBER}} = ${{env.TAG_NUMBER}} )); then
        echo "PUSH=false" >> "$GITHUB_ENV"

    else
        echo "PUSH=false" >> "$GITHUB_ENV"
    fi         
JulianBPL
  • 149
  • 1
  • 10
  • 1
    One note - when you compare numbers in Bash it's better to use `-eq` `-gt` `-lt` rather than `=`, `>`, `<` - those will make numerical comparations. `=` should be only used for strings in bash. And you could go with `[[` - they are more natural for Bash. – Grzegorz Krukowski Feb 06 '22 at 13:32
  • Thanks! Good advice – JulianBPL Feb 08 '22 at 14:29