Skip to content

Commit 01e4ccb

Browse files
feat(semver-compare): add new Github action (#146)
This adds a new Github Action that semantically compares two versions, like 2.1.1 and 2.3.0 giving information about whether or the version is "higher" or "lower" than another version. The action exposes an output called `result` which will match the return type of the PHP [version_compare](https://www.php.net/manual/en/function.version-compare.php) function. Currently, this action compares `version` against `compare_against` and returns: - `-1` - if `version` is lower than `compare_against` - `0` - if `version` is equal to `compare_against` - `1` - if `version` is greater than `compare_against` Co-authored-by: Vitaliy Golomoziy <[email protected]>
1 parent bc840e1 commit 01e4ccb

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Test semvar-compare action
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- ".github/workflows/_internal-semver-compare.yaml"
9+
- "semver-compare/**"
10+
- "!(**/*.md)"
11+
pull_request:
12+
branches:
13+
- main
14+
paths:
15+
- ".github/workflows/_internal-semver-compare.yaml"
16+
- "semver-compare/**"
17+
- "!(**/*.md)"
18+
19+
jobs:
20+
semver-compare:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v3
24+
25+
- uses: ./semver-compare
26+
id: version-compare-1
27+
with:
28+
version: 2.2.3
29+
compare_against: 2.3
30+
31+
- uses: ./semver-compare
32+
id: version-compare-2
33+
with:
34+
version: 2
35+
compare_against: 2.0.1
36+
37+
- uses: ./semver-compare
38+
id: version-compare-3
39+
with:
40+
version: 2.2.1
41+
compare_against: 2.2
42+
43+
44+
- uses: ./semver-compare
45+
id: version-compare-4
46+
with:
47+
version: 2.2.0
48+
compare_against: 2.2.0
49+
50+
- name: Fail if 2.3 is not higher than 2.2.3
51+
if: steps.version-compare-1.outputs.result != -1
52+
shell: bash
53+
run: echo "FAIL because 2.3 must be higher than 2.2.3 Compare 2.2.3 to 2.3 renders ${{ steps.version-compare-1.outputs.result }}" && exit 1
54+
55+
56+
- name: Fail if 2.0.1 is not higher than 2
57+
if: steps.version-compare-2.outputs.result != -1
58+
shell: bash
59+
run: echo "FAIL because 2.0.1 must be higher than 2 Compare 2 to 2.0.1 renders ${{ steps.version-compare-2.outputs.result }}" && exit 1
60+
61+
- name: Fail if 2.2.1 is not higher than 2.2
62+
if: steps.version-compare-3.outputs.result != 1
63+
shell: bash
64+
run: echo "FAIL because 2.2.1 must be higher than 2.2 Compare 2.2.1 to 2.2 renders ${{ steps.version-compare-3.outputs.result }}" && exit 1
65+
66+
- name: Fail if 2.2.0 is not equals to 2.2.0
67+
if: steps.version-compare-4.outputs.result != 0
68+
shell: bash
69+
run: echo "FAIL because 2.2.0 must be equal to 2.2.0 Compare 2.2.0 to 2.2.0 renders ${{ steps.version-compare-4.outputs.result }}" && exit 1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ Opinionated Github Actions and Workflows to make building, testing, and maintain
3131
| [Setup Magento](./setup-magento/README.md) | A Github Action that sets up Magento before `composer install` for an extension or store. |
3232
| [Get Magento Version](./get-magento-version/README.md) | A Github Action that computes the installed Magento version. |
3333
| [Installation Test](./installation-test/README.md) | A Github Action that tests the installability of a Magento Package |
34+
| [Semver Compare](./semver-compare/README.md) | A Github Action that semantically compares two versions |
3435
| [Supported Version](./supported-version/README.md) | A Github Action that computes the currently supported Github Actions Matrix for Magento 2 |

semver-compare/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# "Semver Compare" Action
2+
3+
A Github Action that semantically compares two versions, like 2.1.1 and 2.3.0 giving information about whether or the version is "higher" or "lower" than another version. The action exposes an output called `result` which will match the return type of the PHP [version_compare](https://www.php.net/manual/en/function.version-compare.php) function.
4+
5+
Currently, this action compares `version` against `compare_against` and returns:
6+
7+
- `-1` - if `version` is lower than `compare_against`
8+
- `0` - if `version` is equal to `compare_against`
9+
- `1` - if `version` is greater than `compare_against`
10+
11+
## Inputs
12+
13+
See the [action.yml](./action.yml)
14+
15+
## Usage
16+
17+
```yml
18+
name: Semver Compare
19+
20+
on:
21+
push:
22+
branches:
23+
- main
24+
pull_request:
25+
branches:
26+
- main
27+
28+
jobs:
29+
version:
30+
runs-on: ubuntu-latest
31+
name: A job to semantically compare two versions
32+
steps:
33+
- uses: actions/checkout@v3
34+
- uses: mage-os/github-actions/semver-compare@main
35+
with:
36+
version: 2.1.0
37+
compare_against: 2.2.3
38+
id: semver-compare
39+
- run: echo version ${{ steps.semver-compare.outputs.result }}
40+
shell: bash
41+
```

semver-compare/action.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "Semver Compare"
2+
author: "Graycore"
3+
description: "A Github Action that compares two versions, semantically"
4+
5+
inputs:
6+
version:
7+
required: true
8+
description: "Original version"
9+
10+
compare_against:
11+
required: true
12+
description: "The version to compare against"
13+
14+
outputs:
15+
result: # id of output
16+
description: "The result of comparison. By default, this returns -1 if the first version is lower than the second, 0 if they are equal, and 1 if the second is lower."
17+
value: ${{ steps.semver-compare.outputs.result }}
18+
19+
runs:
20+
using: "composite"
21+
steps:
22+
- name: Semantically compare two versions
23+
run: php -r "echo 'result=' . version_compare('${{ inputs.version }}', '${{ inputs.compare_against }}');" >> $GITHUB_OUTPUT
24+
shell: bash
25+
id: semver-compare
26+
27+
branding:
28+
icon: "code"
29+
color: "green"

0 commit comments

Comments
 (0)