Add tag-match-group input to choose group to get if tag-match matches

Check tag-match is a valid regex
This commit is contained in:
CrazyMax 2020-10-27 02:32:26 +01:00
parent ad83daa929
commit 6fe5b3f6bb
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
8 changed files with 74 additions and 16 deletions

View File

@ -58,9 +58,13 @@ jobs:
strategy:
fail-fast: false
matrix:
tag-match:
- \\d{1,3}.\\d{1,3}.\\d{1,3}
- \\d{1,3}.\\d{1,3}
include:
- tag-match: '\d{1,3}.\d{1,3}.\d{1,3}'
tag-match-group: '0'
- tag-match: '\d{1,3}.\d{1,3}'
tag-match-group: '0'
- tag-match: 'v(.*)'
tag-match-group: '1'
steps:
-
name: Checkout
@ -74,6 +78,7 @@ jobs:
ghcr.io/name/app
tag-sha: true
tag-match: ${{ matrix.tag-match }}
tag-match-group: ${{ matrix.tag-match-group }}
docker-push:
runs-on: ubuntu-latest

View File

@ -1,5 +1,10 @@
# Changelog
## 1.5.0 (2020/10/27)
* Add `tag-match-group` input to choose group to get if `tag-match` matches
* Check `tag-match` is a valid regex
## 1.4.0 (2020/10/27)
* Use RegExp to match against a Git tag instead of coerce

View File

@ -76,7 +76,8 @@ jobs:
with:
images: name/app
tag-sha: true
tag-match: \\d{1,3}.\\d{1,3}.\\d{1,3}
tag-match: v(.*)
tag-match-group: 1
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
@ -114,7 +115,8 @@ Following inputs can be used as `step.with` keys
| `tag-sha` | Bool | Add git short SHA as Docker tag (default `false`) |
| `tag-edge` | Bool | Enable edge branch tagging (default `false`) |
| `tag-edge-branch` | String | Branch that will be tagged as edge (default `repo.default_branch`) |
| `tag-match` | String | RegExp to match against a Git tag and use match group as Docker tag |
| `tag-match` | String | RegExp to match against a Git tag and use first match as Docker tag |
| `tag-match-group` | Number | Group to get if `tag-match` matches (default `0`) |
| `tag-match-latest` | Bool | Set `latest` Docker tag if `tag-match` matches (default `true`) |
| `tag-schedule` | String | [Template](#schedule-tag) to apply to schedule tag (default `nightly`) |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
@ -136,13 +138,13 @@ Following outputs are available
### `tag-match` examples
| Git tag | `tag-match` | Docker tag
|-------------------------|--------------------------------|-------------------|
| `v1.2.3` | `\\d{1,3}.\\d{1,3}.\\d{1,3}` | `1.2.3` |
| `v2.0.8-beta.67` | `\\d{1,3}.\\d{1,3}.\\d{1,3}.*` | `2.0.8-beta.67` |
| `v2.0.8-beta.67` | `\\d{1,3}.\\d{1,3}` | `2.0` |
| `release1` | `\\d{1,3}.\\d{1,3}` | `release1` |
| `20200110-RC2` | `\\d+` | `20200110` |
| Git tag | `tag-match` | `tag-match-group` | Docker tag
|-------------------------|------------------------------------|-------------------|------------------|
| `v1.2.3` | `\\d{1,3}.\\d{1,3}.\\d{1,3}` | `0` | `1.2.3` |
| `v2.0.8-beta.67` | `v(.*)` | `1` | `2.0.8-beta.67` |
| `v2.0.8-beta.67` | `v(\\d.\\d)` | `1` | `2.0` |
| `release1` | `\\d{1,3}.\\d{1,3}` | `0` | `release1` |
| `20200110-RC2` | `\\d+` | `0` | `20200110` |
### Schedule tag

View File

@ -441,6 +441,32 @@ describe('push tag', () => {
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_tag_20200110-RC2.env',
{
images: ['user/app'],
tagMatch: `(.*)-RC`,
tagMatchGroup: 1,
tagMatchLatest: false,
} as Inputs,
{
version: '20200110',
latest: false
} as Version,
[
'user/app:20200110'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World.git",
"org.opencontainers.image.version=20200110",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_tag_v1.1.1.env',
{

View File

@ -24,6 +24,10 @@ inputs:
tag-match:
description: 'RegExp to match against a Git tag and use match group as Docker tag'
required: false
tag-match-group:
description: 'Group to get if tag-match matches (default 0)'
default: '0'
required: false
tag-match-latest:
description: 'Set latest Docker tag if tag-match matches'
default: 'true'

12
dist/index.js generated vendored
View File

@ -26,6 +26,7 @@ function getInputs() {
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
tagEdgeBranch: core.getInput('tag-edge-branch'),
tagMatch: core.getInput('tag-match'),
tagMatchGroup: Number(core.getInput('tag-match-group')) || 0,
tagMatchLatest: /true/i.test(core.getInput('tag-match-latest') || 'true'),
tagSchedule: core.getInput('tag-schedule') || 'nightly',
sepTags: core.getInput('sep-tags') || `\n`,
@ -194,9 +195,16 @@ class Meta {
else if (/^refs\/tags\//.test(this.context.ref)) {
version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
if (this.inputs.tagMatch) {
const tagMatch = version.version.match(this.inputs.tagMatch);
let tagMatch;
const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
if (isRegEx) {
tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
}
else {
tagMatch = version.version.match(this.inputs.tagMatch);
}
if (tagMatch) {
version.version = tagMatch[0];
version.version = tagMatch[this.inputs.tagMatchGroup];
version.latest = this.inputs.tagMatchLatest;
}
}

View File

@ -6,6 +6,7 @@ export interface Inputs {
tagEdge: boolean;
tagEdgeBranch: string;
tagMatch: string;
tagMatchGroup: number;
tagMatchLatest: boolean;
tagSchedule: string;
sepTags: string;
@ -20,6 +21,7 @@ export function getInputs(): Inputs {
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
tagEdgeBranch: core.getInput('tag-edge-branch'),
tagMatch: core.getInput('tag-match'),
tagMatchGroup: Number(core.getInput('tag-match-group')) || 0,
tagMatchLatest: /true/i.test(core.getInput('tag-match-latest') || 'true'),
tagSchedule: core.getInput('tag-schedule') || 'nightly',
sepTags: core.getInput('sep-tags') || `\n`,

View File

@ -41,9 +41,15 @@ export class Meta {
} else if (/^refs\/tags\//.test(this.context.ref)) {
version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
if (this.inputs.tagMatch) {
const tagMatch = version.version.match(this.inputs.tagMatch);
let tagMatch;
const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
if (isRegEx) {
tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
} else {
tagMatch = version.version.match(this.inputs.tagMatch);
}
if (tagMatch) {
version.version = tagMatch[0];
version.version = tagMatch[this.inputs.tagMatchGroup];
version.latest = this.inputs.tagMatchLatest;
}
}