DEV Community

madhead
madhead

Posted on • Originally published at madhead.me

One-stop shop for working with semantic versions in your GitHub Actions workflows

GitHub Actions are so cool!

Check out our recording about them @ Ministry of Testing Abu Dhabi if you want a quick sneak peek, but today I want to self-advertise one of the reusable Actions I made for you. And it wasn’t hard at all, thanks to the way they work.

You have heard about Semantic Versioning, haven’t you? If you are a developer you probably know a lot of libraries using Semantic Versioning. Chances are, you even use it for your projects.

Sometimes during CI/CD in such projects, you need to take simple actions with versions: compare and parse them. E.g. you may want to check that a PR to the main branch increases the version ($new_version > $old_version) or you may want to create or update a v$major.$minor / v$major tag whenever you release your project.

This is what madhead/semver-utils is for! Just drop it into your workflow and use it for various operations with versions:

- uses: madhead/semver-utils@latest
  id: version
  with:
    # A version to work with
    version: 1.2.3+42.24

    # A version to compare against
    compare-to: 2.1.0

    # A range to check agains
    satisfies: 1.x
- run: |
    echo "${{ steps.version.outputs.major }}"             # 1
    echo "${{ steps.version.outputs.minor }}"             # 2
    echo "${{ steps.version.outputs.patch }}"             # 3
    echo "${{ steps.version.outputs.build }}"             # 42.24
    echo "${{ steps.version.outputs.build-parts }}"       # 2
    echo "${{ steps.version.outputs.build-0 }}"           # 42
    echo "${{ steps.version.outputs.build-1 }}"           # 24
    echo "${{ steps.version.outputs.comparison-result }}" # <
    echo "${{ steps.version.outputs.satisfies }}"         # true
    echo "${{ steps.version.outputs.inc-major }}"         # 2.0.0
    echo "${{ steps.version.outputs.inc-premajor }}"      # 2.0.0-0
    echo "${{ steps.version.outputs.inc-minor }}"         # 1.3.0
    echo "${{ steps.version.outputs.inc-preminor }}"      # 1.3.0-0
    echo "${{ steps.version.outputs.inc-patch }}"         # 1.2.4
    echo "${{ steps.version.outputs.inc-prepatch }}"      # 1.2.4-0
    echo "${{ steps.version.outputs.inc-prerelease }}"    # 1.2.4-0
Enter fullscreen mode Exit fullscreen mode

Yeah, it's basically a wapper around semver package, so the outputs may look familiar to you. But if you need more in your workflows — feel free to open an issue with a feature you’re missing.

I want this action to become a one-stop shop for working with semantic versions in your GitHub Actions workflows one day!

Top comments (0)