Skip to content

Commit e0542a6

Browse files
authored
Merge pull request #536 from crazy-max/semver-match
allow to match part of the git tag or value for semver type
2 parents de11195 + b7facdf commit e0542a6

File tree

7 files changed

+135
-33
lines changed

7 files changed

+135
-33
lines changed

README.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ tags: |
495495
type=semver,pattern={{version}}
496496
# use custom value instead of git tag
497497
type=semver,pattern={{version}},value=v1.0.0
498+
# use custom value and match part of it
499+
type=semver,pattern={{version}},value=p1/v1.0.0,match=v(\d.\d.\d)$
498500
```
499501

500502
Will be used on a [push tag event](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#push)
@@ -510,18 +512,19 @@ with the following expressions:
510512
* `minor` ; minor version identifier
511513
* `patch` ; patch version identifier
512514

513-
| Git tag | Pattern | Output |
514-
|--------------------|-----------------------|----------------------|
515-
| `v1.2.3` | `{{raw}}` | `v1.2.3` |
516-
| `v1.2.3` | `{{version}}` | `1.2.3` |
517-
| `v1.2.3` | `{{major}}.{{minor}}` | `1.2` |
518-
| `v1.2.3` | `v{{major}}` | `v1` |
519-
| `v1.2.3` | `{{minor}}` | `2` |
520-
| `v1.2.3` | `{{patch}}` | `3` |
521-
| `v2.0.8-beta.67` | `{{raw}}` | `v2.0.8-beta.67` |
522-
| `v2.0.8-beta.67` | `{{version}}` | `2.0.8-beta.67` |
523-
| `v2.0.8-beta.67` | `{{major}}` | `2.0.8-beta.67`* |
524-
| `v2.0.8-beta.67` | `{{major}}.{{minor}}` | `2.0.8-beta.67`* |
515+
| Git tag | Pattern | Match | Output |
516+
|------------------|-----------------------|----------------|------------------|
517+
| `v1.2.3` | `{{raw}}` | | `v1.2.3` |
518+
| `v1.2.3` | `{{version}}` | | `1.2.3` |
519+
| `v1.2.3` | `{{major}}.{{minor}}` | | `1.2` |
520+
| `v1.2.3` | `v{{major}}` | | `v1` |
521+
| `v1.2.3` | `{{minor}}` | | `2` |
522+
| `v1.2.3` | `{{patch}}` | | `3` |
523+
| `p1/v1.2.3` | `{{version}}` | `v(\d.\d.\d)$` | `1.2.3` |
524+
| `v2.0.8-beta.67` | `{{raw}}` | | `v2.0.8-beta.67` |
525+
| `v2.0.8-beta.67` | `{{version}}` | | `2.0.8-beta.67` |
526+
| `v2.0.8-beta.67` | `{{major}}` | | `2.0.8-beta.67`* |
527+
| `v2.0.8-beta.67` | `{{major}}.{{minor}}` | | `2.0.8-beta.67`* |
525528

526529
> [!IMPORTANT]
527530
> *Pre-release (rc, beta, alpha) will only extend `{{version}}` (or `{{raw}}`
@@ -533,7 +536,7 @@ Extended attributes and default values:
533536

534537
```yaml
535538
tags: |
536-
type=semver,enable=true,priority=900,prefix=,suffix=,pattern=,value=
539+
type=semver,enable=true,priority=900,prefix=,suffix=,pattern=,value=,match=
537540
```
538541

539542
### `type=pep440`
@@ -544,6 +547,8 @@ tags: |
544547
type=pep440,pattern={{version}}
545548
# use custom value instead of git tag
546549
type=pep440,pattern={{version}},value=1.0.0
550+
# use custom value and match part of it
551+
type=pep440,pattern={{version}},value=p1/v1.0.0,match=v(\d.\d.\d)$
547552
```
548553

549554
Will be used on a [push tag event](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#push)
@@ -559,19 +564,20 @@ with the following expressions:
559564
* `minor` ; minor version identifier
560565
* `patch` ; patch version identifier
561566

562-
| Git tag | Pattern | Output |
563-
|--------------------|----------------------------------------------------------|----------------------|
564-
| `1.2.3` | `{{raw}}` | `1.2.3` |
565-
| `1.2.3` | `{{version}}` | `1.2.3` |
566-
| `v1.2.3` | `{{version}}` | `1.2.3` |
567-
| `1.2.3` | `{{major}}.{{minor}}` | `1.2` |
568-
| `1.2.3` | `v{{major}}` | `v1` |
569-
| `v1.2.3rc2` | `{{raw}}` | `v1.2.3rc2` |
570-
| `1.2.3rc2` | `{{version}}` | `1.2.3rc2` |
571-
| `1.2.3rc2` | `{{major}}.{{minor}}` | `1.2.3rc2`* |
572-
| `1.2.3post1` | `{{major}}.{{minor}}` | `1.2.3.post1`* |
573-
| `1.2.3beta2` | `{{major}}.{{minor}}` | `1.2.3b2`* |
574-
| `1.0dev4` | `{{major}}.{{minor}}` | `1.0.dev4`* |
567+
| Git tag | Pattern | Match | Output |
568+
|--------------|-----------------------|----------------|----------------|
569+
| `1.2.3` | `{{raw}}` | | `1.2.3` |
570+
| `1.2.3` | `{{version}}` | | `1.2.3` |
571+
| `v1.2.3` | `{{version}}` | | `1.2.3` |
572+
| `1.2.3` | `{{major}}.{{minor}}` | | `1.2` |
573+
| `1.2.3` | `v{{major}}` | | `v1` |
574+
| `v1.2.3rc2` | `{{raw}}` | | `v1.2.3rc2` |
575+
| `1.2.3rc2` | `{{version}}` | | `1.2.3rc2` |
576+
| `p1/v1.2.3` | `{{version}}` | `v(\d.\d.\d)$` | `1.2.3` |
577+
| `1.2.3rc2` | `{{major}}.{{minor}}` | | `1.2.3rc2`* |
578+
| `1.2.3post1` | `{{major}}.{{minor}}` | | `1.2.3.post1`* |
579+
| `1.2.3beta2` | `{{major}}.{{minor}}` | | `1.2.3b2`* |
580+
| `1.0dev4` | `{{major}}.{{minor}}` | | `1.0.dev4`* |
575581

576582
> [!IMPORTANT]
577583
> *dev/pre/post release will only extend `{{version}}` (or `{{raw}}` if

__tests__/meta.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,69 @@ describe('tag', () => {
19711971
"org.opencontainers.image.version=1.1.1"
19721972
],
19731973
undefined
1974+
],
1975+
[
1976+
'tag34',
1977+
'event_tag_p1-v1.0.0.env',
1978+
{
1979+
images: ['org/app'],
1980+
tags: [
1981+
`type=semver,pattern={{version}},"match=v(\\d.\\d.\\d)$"`,
1982+
]
1983+
} as Inputs,
1984+
{
1985+
main: '1.0.0',
1986+
partial: [],
1987+
latest: true
1988+
} as Version,
1989+
[
1990+
'org/app:1.0.0',
1991+
'org/app:latest'
1992+
],
1993+
[
1994+
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
1995+
"org.opencontainers.image.description=This your first repo!",
1996+
"org.opencontainers.image.licenses=MIT",
1997+
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
1998+
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
1999+
"org.opencontainers.image.title=Hello-World",
2000+
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
2001+
"org.opencontainers.image.version=1.0.0"
2002+
],
2003+
undefined
2004+
],
2005+
[
2006+
'push35',
2007+
'event_push_master.env',
2008+
{
2009+
images: ['user/app'],
2010+
tags: [
2011+
`type=semver,pattern={{version}},value=p1/v1.2.3,"match=v(\\d.\\d.\\d)$"`,
2012+
`type=pep440,pattern={{version}},value=p1/v1.2.3,"match=v(\\d.\\d.\\d)$"`,
2013+
`type=edge`
2014+
],
2015+
} as Inputs,
2016+
{
2017+
main: '1.2.3',
2018+
partial: ['edge'],
2019+
latest: true
2020+
} as Version,
2021+
[
2022+
'user/app:1.2.3',
2023+
'user/app:edge',
2024+
'user/app:latest'
2025+
],
2026+
[
2027+
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
2028+
"org.opencontainers.image.description=This your first repo!",
2029+
"org.opencontainers.image.licenses=MIT",
2030+
"org.opencontainers.image.revision=266574110acf203503badf966df2ea24b5d732d7",
2031+
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
2032+
"org.opencontainers.image.title=Hello-World",
2033+
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
2034+
"org.opencontainers.image.version=1.2.3"
2035+
],
2036+
undefined
19742037
]
19752038
])('given %p with %p event', tagsLabelsTest);
19762039
});

__tests__/tag.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ describe('transform', () => {
3232
"priority": DefaultPriorities[Type.Semver],
3333
"enable": "true",
3434
"pattern": "{{version}}",
35-
"value": ""
35+
"value": "",
36+
"match": ""
3637
}
3738
},
3839
{
@@ -147,7 +148,8 @@ describe('parse', () => {
147148
"priority": DefaultPriorities[Type.Semver],
148149
"enable": "true",
149150
"pattern": "{{version}}",
150-
"value": ""
151+
"value": "",
152+
"match": ""
151153
}
152154
} as Tag,
153155
false
@@ -160,7 +162,8 @@ describe('parse', () => {
160162
"priority": "1",
161163
"enable": "true",
162164
"pattern": "{{version}}",
163-
"value": ""
165+
"value": "",
166+
"match": ""
164167
}
165168
} as Tag,
166169
false
@@ -173,7 +176,22 @@ describe('parse', () => {
173176
"priority": "1",
174177
"enable": "true",
175178
"pattern": "{{version}}",
176-
"value": "v1.0.0"
179+
"value": "v1.0.0",
180+
"match": ""
181+
}
182+
} as Tag,
183+
false
184+
],
185+
[
186+
`type=semver,priority=1,enable=true,pattern={{version}},value=p1/v1.0.0,"match=v(\\d.\\d.\\d)$"`,
187+
{
188+
type: Type.Semver,
189+
attrs: {
190+
"priority": "1",
191+
"enable": "true",
192+
"pattern": "{{version}}",
193+
"value": "p1/v1.0.0",
194+
"match": "v(\\d.\\d.\\d)$"
177195
}
178196
} as Tag,
179197
false

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/meta.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,20 @@ export class Meta {
161161
if (tag.attrs['value'].length > 0) {
162162
vraw = this.setGlobalExp(tag.attrs['value']);
163163
} else {
164-
vraw = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
164+
vraw = this.context.ref.replace(/^refs\/tags\//g, '');
165+
}
166+
167+
if (tag.attrs['match'].length > 0) {
168+
const tmatch = vraw.match(tag.attrs['match']);
169+
if (!tmatch) {
170+
core.warning(`${tag.attrs['match']} does not match ${vraw}.`);
171+
} else {
172+
vraw = this.setValue(tmatch[1], tag);
173+
}
165174
}
175+
176+
vraw = vraw.replace(/\//g, '-');
177+
166178
if (!semver.valid(vraw)) {
167179
core.warning(`${vraw} is not a valid semver. More info: https://semver.org/`);
168180
return version;

src/tag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ export function Parse(s: string): Tag {
137137
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'value')) {
138138
tag.attrs['value'] = '';
139139
}
140+
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'match')) {
141+
tag.attrs['match'] = '';
142+
}
140143
break;
141144
}
142145
case Type.Match: {

0 commit comments

Comments
 (0)