Skip to content

Commit

Permalink
Added regex params, renamed env var
Browse files Browse the repository at this point in the history
  • Loading branch information
olegtarasov committed Feb 28, 2020
1 parent 068d01c commit c0eaaac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
# Get tag name

This simple action gets tag name from commit that triggered the action and puts it into an environment variable GITHUB_TAG_NAME. It will also export is as an output
named "tag".
This action gets tag name from commit that triggered the action and puts it into an environment variable GIT_TAG_NAME. It will also export is as an output named "tag".

You can also use optional parameters `tagRegex` and `tagRegexGroup` to extract a part from tag string.

## Usage

Dead simple:

```yaml
steps:
- uses: olegtarasov/get-tag@v1
- uses: olegtarasov/get-tag@v2
id: tagName
- name: Some other step
with:
tagname: ${{ steps.tagName.outputs.tag }}
tagRegex: "foobar-(.*)" # Optional. Returns specified group text as tag name. Full tag string is returned if regex is not defined.
tagRegexGroup: 1 # Optional. Default is 1.
- name: Some other step # Output usage example
with:
tagname: ${{ steps.tagName.outputs.tag }}
- name: Yet another step # Environment variabl usage example
run: |
docker build . --file Dockerfile --tag docker.pkg.github.com/someimage:$GIT_TAG_NAME
```
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Get tag name'
description: 'Gets current tag name and puts it into an environment variable GITHUB_TAG_NAME.'
description: 'Gets current tag name and puts it into an environment variable GIT_TAG_NAME.'
author: 'Oleg Tarasov'
runs:
using: 'node12'
Expand Down
17 changes: 15 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ function run() {
const ref = github.context.ref;
const tagPath = "refs/tags/";
if (ref && ref.startsWith(tagPath)) {
const tag = ref.substr(tagPath.length, ref.length);
core.exportVariable("GITHUB_TAG_NAME", tag);
var tag = ref.substr(tagPath.length, ref.length);
const regexStr = core.getInput("tagRegex");
if (regexStr) {
const regex = new RegExp(regexStr);
const groupIdx = parseInt(core.getInput("tagRegexGroup") || "1");
const result = regex.exec(tag);
if (result && result.length > groupIdx) {
tag = result[groupIdx];
}
else {
core.warning(`Failed to match regex '${regexStr}' in tag string '${tag}'. Result is '${result}'`);
return;
}
}
core.exportVariable("GIT_TAG_NAME", tag);
core.setOutput('tag', tag);
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ async function run() {
const ref = github.context.ref
const tagPath = "refs/tags/"
if (ref && ref.startsWith(tagPath)) {
const tag = ref.substr(tagPath.length, ref.length);
core.exportVariable("GITHUB_TAG_NAME", tag);
var tag = ref.substr(tagPath.length, ref.length);
const regexStr = core.getInput("tagRegex")
if (regexStr) {
const regex = new RegExp(regexStr)
const groupIdx = parseInt(core.getInput("tagRegexGroup") || "1")
const result = regex.exec(tag);
if (result && result.length > groupIdx) {
tag = result[groupIdx]
}
else {
core.warning(`Failed to match regex '${regexStr}' in tag string '${tag}'. Result is '${result}'`)
return
}
}
core.exportVariable("GIT_TAG_NAME", tag);
core.setOutput('tag', tag);
}
} catch (error) {
Expand Down

0 comments on commit c0eaaac

Please # to comment.