Skip to content

Commit

Permalink
feat(action-git): add extract-version-from-tag action
Browse files Browse the repository at this point in the history
  • Loading branch information
JoffreyPlouvier committed Mar 24, 2023
1 parent 3f7fe35 commit 8a78dad
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
33 changes: 33 additions & 0 deletions actions/git/extract-version-from-tag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Git: extract-version-from-tag

## Behavior

Extract version number from semver tag.
WARNING: Must be run on tag reference.

## Usage

```yaml
jobs:
extract-version:
runs-on: ubuntu-latest
steps:
- name: "Checkout Code"
uses: "actions/checkout@v3"

- name: "extract version"
uses: "meero-com/github-actions-shared-workflows/actions/git/extract-version-from-tag@main"
with:
PREFIX: "DEV-v"
```
Will outputs
```yaml
version_number: 1.2.3 # if ran on tag DEV-v1.2.3
```
Beware of using a `@ref` (`@main` in the example above) which suits your stability requirements in your workflow:

* Use `@main` if you always want to use the latest version of the workflow.
* Use `@<tag>` if you wan to use a specific frozen version of the workflow.
29 changes: 29 additions & 0 deletions actions/git/extract-version-from-tag/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: "Extract version from tag"
description: "Extract version number from semver tag. Must be run on tag reference."

inputs:
PREFIX:
description: "Prefix of the tag (example 'prefix-v': 'prefix-v1.0.0' -> '1.0.0')"
required: false
default: 'v'

outputs:
version_number:
description: "version number (ex: 1.0.0)"
value: ${{ steps.extract-version.outputs.version }}

runs:
using: "composite"
steps:
- name: "extract version from refname"
id: extract-version
uses: actions/github-script@v6
with:
script: |
const getVersion = function (ref_name) {
var rx = /${{ inputs.PREFIX }}([0-9]+\.[0-9]+\.[0-9]+)/g;
var arr = rx.exec(ref_name);
return arr[1];
}
core.setOutput('version', getVersion('${{ github.ref_name }}'))

0 comments on commit 8a78dad

Please sign in to comment.