From c0eaaac6200d38f56a090614da7aa85a98cd8a3f Mon Sep 17 00:00:00 2001 From: Oleg Tarasov Date: Fri, 28 Feb 2020 15:07:28 +0300 Subject: [PATCH] Added regex params, renamed env var --- README.md | 18 +++++++++++++----- action.yml | 2 +- lib/main.js | 17 +++++++++++++++-- src/main.ts | 17 +++++++++++++++-- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c21e767c..dfea9719 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # 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 @@ -9,9 +10,16 @@ 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 + ``` diff --git a/action.yml b/action.yml index 2b18cef5..805bf720 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/lib/main.js b/lib/main.js index 6060d060..cdd93c1a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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); } } diff --git a/src/main.ts b/src/main.ts index 435eefc1..d90602cd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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) {