Skip to content

Commit

Permalink
Add tag-match-group input to choose group to get if tag-match matches
Browse files Browse the repository at this point in the history
Check tag-match is a valid regex
  • Loading branch information
crazy-max committed Oct 27, 2020
1 parent ad83daa commit 6fe5b3f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 16 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`) |
Expand All @@ -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

Expand Down
26 changes: 26 additions & 0 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
{
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 10 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Inputs {
tagEdge: boolean;
tagEdgeBranch: string;
tagMatch: string;
tagMatchGroup: number;
tagMatchLatest: boolean;
tagSchedule: string;
sepTags: string;
Expand All @@ -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`,
Expand Down
10 changes: 8 additions & 2 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 6fe5b3f

Please sign in to comment.