Skip to content

Commit 6fe5b3f

Browse files
committed
Add tag-match-group input to choose group to get if tag-match matches
Check tag-match is a valid regex
1 parent ad83daa commit 6fe5b3f

File tree

8 files changed

+74
-16
lines changed

8 files changed

+74
-16
lines changed

.github/workflows/ci.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ jobs:
5858
strategy:
5959
fail-fast: false
6060
matrix:
61-
tag-match:
62-
- \\d{1,3}.\\d{1,3}.\\d{1,3}
63-
- \\d{1,3}.\\d{1,3}
61+
include:
62+
- tag-match: '\d{1,3}.\d{1,3}.\d{1,3}'
63+
tag-match-group: '0'
64+
- tag-match: '\d{1,3}.\d{1,3}'
65+
tag-match-group: '0'
66+
- tag-match: 'v(.*)'
67+
tag-match-group: '1'
6468
steps:
6569
-
6670
name: Checkout
@@ -74,6 +78,7 @@ jobs:
7478
ghcr.io/name/app
7579
tag-sha: true
7680
tag-match: ${{ matrix.tag-match }}
81+
tag-match-group: ${{ matrix.tag-match-group }}
7782

7883
docker-push:
7984
runs-on: ubuntu-latest

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.5.0 (2020/10/27)
4+
5+
* Add `tag-match-group` input to choose group to get if `tag-match` matches
6+
* Check `tag-match` is a valid regex
7+
38
## 1.4.0 (2020/10/27)
49

510
* Use RegExp to match against a Git tag instead of coerce

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ jobs:
7676
with:
7777
images: name/app
7878
tag-sha: true
79-
tag-match: \\d{1,3}.\\d{1,3}.\\d{1,3}
79+
tag-match: v(.*)
80+
tag-match-group: 1
8081
-
8182
name: Set up QEMU
8283
uses: docker/setup-qemu-action@v1
@@ -114,7 +115,8 @@ Following inputs can be used as `step.with` keys
114115
| `tag-sha` | Bool | Add git short SHA as Docker tag (default `false`) |
115116
| `tag-edge` | Bool | Enable edge branch tagging (default `false`) |
116117
| `tag-edge-branch` | String | Branch that will be tagged as edge (default `repo.default_branch`) |
117-
| `tag-match` | String | RegExp to match against a Git tag and use match group as Docker tag |
118+
| `tag-match` | String | RegExp to match against a Git tag and use first match as Docker tag |
119+
| `tag-match-group` | Number | Group to get if `tag-match` matches (default `0`) |
118120
| `tag-match-latest` | Bool | Set `latest` Docker tag if `tag-match` matches (default `true`) |
119121
| `tag-schedule` | String | [Template](#schedule-tag) to apply to schedule tag (default `nightly`) |
120122
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
@@ -136,13 +138,13 @@ Following outputs are available
136138

137139
### `tag-match` examples
138140

139-
| Git tag | `tag-match` | Docker tag
140-
|-------------------------|--------------------------------|-------------------|
141-
| `v1.2.3` | `\\d{1,3}.\\d{1,3}.\\d{1,3}` | `1.2.3` |
142-
| `v2.0.8-beta.67` | `\\d{1,3}.\\d{1,3}.\\d{1,3}.*` | `2.0.8-beta.67` |
143-
| `v2.0.8-beta.67` | `\\d{1,3}.\\d{1,3}` | `2.0` |
144-
| `release1` | `\\d{1,3}.\\d{1,3}` | `release1` |
145-
| `20200110-RC2` | `\\d+` | `20200110` |
141+
| Git tag | `tag-match` | `tag-match-group` | Docker tag
142+
|-------------------------|------------------------------------|-------------------|------------------|
143+
| `v1.2.3` | `\\d{1,3}.\\d{1,3}.\\d{1,3}` | `0` | `1.2.3` |
144+
| `v2.0.8-beta.67` | `v(.*)` | `1` | `2.0.8-beta.67` |
145+
| `v2.0.8-beta.67` | `v(\\d.\\d)` | `1` | `2.0` |
146+
| `release1` | `\\d{1,3}.\\d{1,3}` | `0` | `release1` |
147+
| `20200110-RC2` | `\\d+` | `0` | `20200110` |
146148

147149
### Schedule tag
148150

__tests__/meta.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,32 @@ describe('push tag', () => {
441441
"org.opencontainers.image.licenses=MIT"
442442
]
443443
],
444+
[
445+
'event_tag_20200110-RC2.env',
446+
{
447+
images: ['user/app'],
448+
tagMatch: `(.*)-RC`,
449+
tagMatchGroup: 1,
450+
tagMatchLatest: false,
451+
} as Inputs,
452+
{
453+
version: '20200110',
454+
latest: false
455+
} as Version,
456+
[
457+
'user/app:20200110'
458+
],
459+
[
460+
"org.opencontainers.image.title=Hello-World",
461+
"org.opencontainers.image.description=This your first repo!",
462+
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
463+
"org.opencontainers.image.source=https://github.com/octocat/Hello-World.git",
464+
"org.opencontainers.image.version=20200110",
465+
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
466+
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
467+
"org.opencontainers.image.licenses=MIT"
468+
]
469+
],
444470
[
445471
'event_tag_v1.1.1.env',
446472
{

action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ inputs:
2424
tag-match:
2525
description: 'RegExp to match against a Git tag and use match group as Docker tag'
2626
required: false
27+
tag-match-group:
28+
description: 'Group to get if tag-match matches (default 0)'
29+
default: '0'
30+
required: false
2731
tag-match-latest:
2832
description: 'Set latest Docker tag if tag-match matches'
2933
default: 'true'

dist/index.js

+10-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Inputs {
66
tagEdge: boolean;
77
tagEdgeBranch: string;
88
tagMatch: string;
9+
tagMatchGroup: number;
910
tagMatchLatest: boolean;
1011
tagSchedule: string;
1112
sepTags: string;
@@ -20,6 +21,7 @@ export function getInputs(): Inputs {
2021
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
2122
tagEdgeBranch: core.getInput('tag-edge-branch'),
2223
tagMatch: core.getInput('tag-match'),
24+
tagMatchGroup: Number(core.getInput('tag-match-group')) || 0,
2325
tagMatchLatest: /true/i.test(core.getInput('tag-match-latest') || 'true'),
2426
tagSchedule: core.getInput('tag-schedule') || 'nightly',
2527
sepTags: core.getInput('sep-tags') || `\n`,

src/meta.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ export class Meta {
4141
} else if (/^refs\/tags\//.test(this.context.ref)) {
4242
version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
4343
if (this.inputs.tagMatch) {
44-
const tagMatch = version.version.match(this.inputs.tagMatch);
44+
let tagMatch;
45+
const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
46+
if (isRegEx) {
47+
tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
48+
} else {
49+
tagMatch = version.version.match(this.inputs.tagMatch);
50+
}
4551
if (tagMatch) {
46-
version.version = tagMatch[0];
52+
version.version = tagMatch[this.inputs.tagMatchGroup];
4753
version.latest = this.inputs.tagMatchLatest;
4854
}
4955
}

0 commit comments

Comments
 (0)