Allow custom value for semver and match type

This commit is contained in:
CrazyMax 2021-03-27 21:46:47 +01:00
parent b171e9c8ea
commit e59258b94d
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
7 changed files with 179 additions and 20 deletions

View File

@ -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:
-

View File

@ -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
@ -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`
@ -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 |
|-------------------------|-------------------------------|---------|------------------------|
@ -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`

View File

@ -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);
});

View File

@ -33,7 +33,8 @@ describe('transform', () => {
"enable": "true",
"prefix": "",
"suffix": "",
"pattern": "{{version}}"
"pattern": "{{version}}",
"value": ""
}
},
{
@ -44,7 +45,8 @@ describe('transform', () => {
"prefix": "",
"suffix": "",
"pattern": "\\d{1,3}.\\d{1,3}.\\d{1,3}",
"group": "0"
"group": "0",
"value": ""
}
},
{
@ -149,7 +151,8 @@ describe('parse', () => {
"enable": "true",
"prefix": "",
"suffix": "",
"pattern": "{{version}}"
"pattern": "{{version}}",
"value": ""
}
} as Tag,
false
@ -163,7 +166,23 @@ describe('parse', () => {
"enable": "true",
"prefix": "",
"suffix": "",
"pattern": "{{version}}"
"pattern": "{{version}}",
"value": ""
}
} 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
@ -178,7 +197,8 @@ describe('parse', () => {
"prefix": "",
"suffix": "",
"pattern": "v(.*)",
"group": "1"
"group": "1",
"value": ""
}
} as Tag,
false
@ -193,7 +213,8 @@ describe('parse', () => {
"prefix": "",
"suffix": "",
"pattern": "^v(\\d{1,3}.\\d{1,3}.\\d{1,3})$",
"group": "1"
"group": "1",
"value": ""
}
} as Tag,
false
@ -208,7 +229,24 @@ describe('parse', () => {
"prefix": "",
"suffix": "",
"pattern": "v(.*)",
"group": "1"
"group": "1",
"value": ""
}
} 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

26
dist/index.js generated vendored
View File

@ -418,10 +418,16 @@ class Meta {
return version;
}
procSemver(version, tag) {
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;
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;
@ -455,10 +461,16 @@ class Meta {
return version;
}
procMatch(version, tag) {
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;
if (tag.attrs['value'].length > 0) {
vraw = tag.attrs['value'];
}
else {
vraw = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
}
let latest = false;
let tmatch;
const isRegEx = tag.attrs['pattern'].match(/^\/(.+)\/(.*)$/);
@ -766,6 +778,9 @@ function Parse(s) {
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: {
@ -778,6 +793,9 @@ function Parse(s) {
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: {

View File

@ -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;
@ -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;

View File

@ -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: {
@ -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: {