Skip to content

Commit

Permalink
Allow custom value for semver and match type
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Mar 27, 2021
1 parent b171e9c commit b9f3800
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 13 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -129,7 +129,24 @@ jobs:
flavor: |
latest=${{ matrix.flavor-latest }}
label-custom:
flavor:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Docker meta
uses: ./
with:
images: |
${{ env.DOCKER_IMAGE }}
ghcr.io/name/app
flavor: |
prefix=foo-
suffix=-bar
labels:
runs-on: ubuntu-latest
steps:
-
Expand Down
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -381,10 +381,13 @@ tags: |
tags: |
# minimal
type=semver,pattern={{version}}
# use custom value instead of git tag
type=semver,pattern={{version}},value=v1.0.0
```
Will be used on a [push tag event](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push)
and requires a valid Git tag [semver](https://semver.org/).
and requires a valid Git tag [semver](https://semver.org/) but you can also use a custom value through `value`
attribute.
`pattern` attribute supports [Handlebars template](https://handlebarsjs.com/guide/) with the following expressions:
* `raw` ; the actual semver
Expand Down Expand Up @@ -412,7 +415,7 @@ Extended attributes and default values:
```yaml
tags: |
type=semver,enable=true,priority=900,prefix=,suffix=,pattern=
type=semver,enable=true,priority=900,prefix=,suffix=,pattern=,value=
```
### `type=match`
Expand All @@ -425,10 +428,13 @@ tags: |
type=match,"pattern=\d{1,3}.\d{1,3}.\d{1,3}"
# define match group
type=match,pattern=v(.*),group=1
# use custom value instead of git tag
type=match,pattern=v(.*),group=1,value=v1.0.0
```
Can create a regular expression for matching Git tag with a pattern and capturing group. Will be used on a
[push tag event](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push).
[push tag event](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push) but you can also use
a custom value through `value` attribute.
| Git tag | Pattern | Group | Output |
|-------------------------|-------------------------------|---------|------------------------|
Expand All @@ -441,7 +447,7 @@ Extended attributes and default values:
```yaml
tags: |
type=group,enable=true,priority=800,prefix=,suffix=,pattern=,group=0
type=group,enable=true,priority=800,prefix=,suffix=,pattern=,group=0,value=
```
### `type=edge`
Expand Down
62 changes: 62 additions & 0 deletions __tests__/meta.test.ts
Expand Up @@ -496,6 +496,68 @@ describe('push', () => {
"org.opencontainers.image.licenses=MIT"
]
],
[
'push14',
'event_push_defbranch.env',
{
images: ['user/app'],
tags: [
`type=semver,pattern={{version}},value=v1.2.3`,
`type=edge`
],
} as Inputs,
{
main: '1.2.3',
partial: ['edge'],
latest: true
} as Version,
[
'user/app:1.2.3',
'user/app:edge',
'user/app:latest'
],
[
"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",
"org.opencontainers.image.version=1.2.3",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'push15',
'event_push_defbranch.env',
{
images: ['user/app'],
tags: [
`type=match,pattern=v(.*),group=1,value=v1.2.3`,
`type=edge`
],
} as Inputs,
{
main: '1.2.3',
partial: ['edge'],
latest: true
} as Version,
[
'user/app:1.2.3',
'user/app:edge',
'user/app:latest'
],
[
"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",
"org.opencontainers.image.version=1.2.3",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
]
])('given %p with %p event', tagsLabelsTest);
});

Expand Down
31 changes: 31 additions & 0 deletions __tests__/tag.test.ts
Expand Up @@ -168,6 +168,21 @@ describe('parse', () => {
} as Tag,
false
],
[
`type=semver,priority=1,enable=true,pattern={{version}},value=v1.0.0`,
{
type: Type.Semver,
attrs: {
"priority": "1",
"enable": "true",
"prefix": "",
"suffix": "",
"pattern": "{{version}}",
"value": "v1.0.0"
}
} as Tag,
false
],
[
`type=match,enable=true,pattern=v(.*),group=1`,
{
Expand Down Expand Up @@ -213,6 +228,22 @@ describe('parse', () => {
} as Tag,
false
],
[
`type=match,enable=true,pattern=v(.*),group=1,value=v1.2.3`,
{
type: Type.Match,
attrs: {
"priority": DefaultPriorities[Type.Match],
"enable": "true",
"prefix": "",
"suffix": "",
"pattern": "v(.*)",
"group": "1",
"value": "v1.2.3"
}
} as Tag,
false
],
[
`type=match,enable=true,pattern=v(.*),group=foo`,
{} as Tag,
Expand Down
26 changes: 22 additions & 4 deletions dist/index.js

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

20 changes: 16 additions & 4 deletions src/meta.ts
Expand Up @@ -115,10 +115,16 @@ export class Meta {
}

private procSemver(version: Version, tag: tcl.Tag): Version {
if (!/^refs\/tags\//.test(this.context.ref)) {
if (!/^refs\/tags\//.test(this.context.ref) && tag.attrs['value'].length == 0) {
return version;
}
let vraw = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');

let vraw: string;
if (tag.attrs['value'].length > 0) {
vraw = tag.attrs['value'];
} else {
vraw = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
}
if (!semver.valid(vraw)) {
core.warning(`${vraw} is not a valid semver. More info: https://semver.org/`);
return version;
Expand Down Expand Up @@ -152,10 +158,16 @@ export class Meta {
}

private procMatch(version: Version, tag: tcl.Tag): Version {
if (!/^refs\/tags\//.test(this.context.ref)) {
if (!/^refs\/tags\//.test(this.context.ref) && tag.attrs['value'].length == 0) {
return version;
}
let vraw = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');

let vraw: string;
if (tag.attrs['value'].length > 0) {
vraw = tag.attrs['value'];
} else {
vraw = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
}

let latest: boolean = false;
let tmatch;
Expand Down
6 changes: 6 additions & 0 deletions src/tag.ts
Expand Up @@ -104,6 +104,9 @@ export function Parse(s: string): Tag {
if (!tag.attrs.hasOwnProperty('pattern')) {
throw new Error(`Missing pattern attribute for ${s}`);
}
if (!tag.attrs.hasOwnProperty('value')) {
tag.attrs['value'] = '';
}
break;
}
case Type.Match: {
Expand All @@ -116,6 +119,9 @@ export function Parse(s: string): Tag {
if (isNaN(+tag.attrs['group'])) {
throw new Error(`Invalid match group for ${s}`);
}
if (!tag.attrs.hasOwnProperty('value')) {
tag.attrs['value'] = '';
}
break;
}
case Type.Edge: {
Expand Down

0 comments on commit b9f3800

Please sign in to comment.