JulianBPL
JulianBPL

Reputation: 193

Compare Package Versions in Github Actions

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

Upvotes: 2

Views: 1636

Answers (1)

JulianBPL
JulianBPL

Reputation: 193

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         

Upvotes: 0

Related Questions