0

I'm comparing the current version which is in package.json and the latest tag.

The expected behavior is that whenever:

  1. The version from the package.json file is less than or equal to the latest tag(tag version) the PR should be rejected rejected
  2. The version from the package.json file is greater than the latest tag(tag version) the PR should pass the check.

but the result is different than I expected enter image description here where CURRENT_VERSION refers to the version from the package.json file and LATEST_TAG is the latest tag number.

Here is my action:

steps:
  - uses: actions/checkout@v2
    with:
      fetch-depth: 0
  - name: Get version and tag
    run: |
      version=$(node -p "require('./package.json').version")
      echo "CURRENT_VERSION=$version" >> $GITHUB_ENV
      echo "LATEST_TAG=$(git describe --tag --abbrev=0 | cut -c 2-)" >> $GITHUB_ENV

  - name: PR version is invalid
    if: ${{ env.CURRENT_VERSION <= env.LATEST_TAG }}
    run: |
      echo "Error: Same version found. Please update version in package.json"
      exit 1 

  - name: PR version is valid
    if: ${{ env.CURRENT_VERSION != env.LATEST_TAG }}
    run: |
      echo "New taga has been published"

If someone helps me to understand what is happening I'll be grateful.

niranjanshk
  • 138
  • 1
  • 2
  • 11

1 Answers1

1

<= operator is used in comparing numbers: see operators

To compare symver, you may want other methods (or simply php -r 'version_compare("${{ some expr }}", "${{ some expr }}");')

I may use something like

steps:
  - name: Check PR version
    shell: bash
    run: |
      # you can also use node, python, php things
      pr="$(jq -r '.version' < ./package.json)"
      # get my latest version
      # modify this with your own style
      latest="$(git describe --tag --abbrev=0)"
      php -r 'if(version_compare("'"$pr"'","'"$latest"'",">")){exit(0);};exit(1);'
  - name: Step that wont run if not valid
    shell: bash
    run: |
      echo balabala

or

steps:
  - name: Check PR version
    shell: bash
    # set step name for futher use
    id: check
    run: |
      # you can also use node, python, php things
      pr="$(jq -r '.version' < ./package.json)"
      # get my latest version
      # modify this with your own style
      latest="$(git describe --tag --abbrev=0)"
      php -r 'echo"::set-output name=valid::".(version_compare("'"$pr"'","'"$latest"'",">")?"valid":"invalid").PHP_EOL;'
  - name: Step that always run
    shell: bash
    run: |
      echo "pr is ${{ steps.check.outputs.valid }}"
      if [ "${{ steps.check.outputs.valid }}" = "valid" ]
      then
        echo "im valid"
      else
        echo "im invalid"
      fi
  - name: Step that run at valid
    if: steps.check.outputs.valid == 'valid'
    shell: bash
    run: |
      echo "pr is ${{ steps.check.outputs.valid }}"
  - name: Step that run at invalid
    if: steps.check.outputs.valid != 'valid'
    shell: bash
    run: |
      echo "pr is ${{ steps.check.outputs.valid }}"
Yun Dou
  • 13
  • 4