Set latest tag only if matches with a pattern

This commit is contained in:
CrazyMax 2020-10-26 17:51:00 +01:00
parent 4d7603f754
commit 0dac4059e9
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
6 changed files with 207 additions and 3 deletions

View file

@ -25,7 +25,7 @@ ___
* [Notes](#notes)
* [Latest tag](#latest-tag)
* [Coerces Git tag](#coerces-git-tag)
* [Templates available for schedule tag](#templates-available-for-schedule-tag)
* [Schedule tag](#schedule-tag)
* [Keep up-to-date with GitHub Dependabot](#keep-up-to-date-with-github-dependabot)
* [How can I help?](#how-can-i-help)
* [License](#license)
@ -117,6 +117,7 @@ Following inputs can be used as `step.with` keys
| `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-coerce-tag` | String | Coerces Git tag to semver if possible using [Handlebars template](https://handlebarsjs.com/guide/) |
| `tag-latest-match` | String | Set `latest` tag only if [matches with a pattern](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) |
| `tag-schedule` | String | [Handlebars template](https://handlebarsjs.com/guide/) to apply to schedule tag (default `nightly`) |
| `sep-tags` | String | Separator to use for tags output (default `\n`) |
| `sep-labels` | String | Separator to use for labels output (default `\n`) |
@ -137,11 +138,13 @@ Following outputs are available
### Latest tag
`latest` tag is created only on `push tag` event and resolves one of the following conditions:
`latest` tag is created with the following conditions:
* Git tag is a valid [semver](https://semver.org/)
* Provided `tag-coerce-tag` is valid
If `tag-latest-match` is filled, then it has priority over the creation of the tag.
### Coerces Git tag
Provides a very forgiving translation of a non-semver tag to semver. For more information see
@ -156,7 +159,7 @@ Provides a very forgiving translation of a non-semver tag to semver. For more in
| `{{minor}}` | `v1.2.3` | `2` |
| `{{patch}}` | `v1.2.3` | `3` |
### Templates available for schedule tag
### Schedule tag
`tag-schedule` is specially crafted input to support [Handlebars template](https://handlebarsjs.com/guide/) with
the following expressions:

View file

@ -601,6 +601,191 @@ describe('push tag', () => {
])('given %p event ', tagsLabelsTest);
});
describe('latest', () => {
// prettier-ignore
test.each([
[
'event_tag_release1.env',
{
images: ['user/app'],
tagLatestMatch: `^release`,
} as Inputs,
{
version: 'release1',
latest: true,
} as Version,
[
'user/app:release1',
'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.git",
"org.opencontainers.image.version=release1",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_tag_20200110-RC2.env',
{
images: ['user/app'],
tagLatestMatch: `^\\d`,
} as Inputs,
{
version: '20200110-RC2',
latest: true
} as Version,
[
'user/app:20200110-RC2',
'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.git",
"org.opencontainers.image.version=20200110-RC2",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_tag_20200110-RC2.env',
{
images: ['user/app'],
tagCoerceTag: '{{major}}',
tagLatestMatch: `\\d`,
} as Inputs,
{
version: '20200110',
latest: true
} as Version,
[
'user/app:20200110',
'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.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',
{
images: ['user/app'],
tagLatestMatch: `\\d{1,3}.\\d{1,3}.\\d{1,3}`,
} as Inputs,
{
version: '1.1.1',
latest: true
} as Version,
[
'user/app:1.1.1',
'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.git",
"org.opencontainers.image.version=1.1.1",
"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',
{
images: ['org/app', 'ghcr.io/user/app'],
} as Inputs,
{
version: '1.1.1',
latest: true
} as Version,
[
'org/app:1.1.1',
'org/app:latest',
'ghcr.io/user/app:1.1.1',
'ghcr.io/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.git",
"org.opencontainers.image.version=1.1.1",
"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',
{
images: ['org/app', 'ghcr.io/user/app'],
tagLatestMatch: `2.\\d{1,3}.\\d{1,3}`,
} as Inputs,
{
version: '1.1.1',
latest: false
} as Version,
[
'org/app:1.1.1',
'ghcr.io/user/app:1.1.1',
],
[
"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=1.1.1",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
[
'event_tag_v2.0.8-beta.67.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tagLatestMatch: `^\\d{1,3}.\\d{1,3}.\\d{1,3}`,
} as Inputs,
{
version: '2.0.8-beta.67',
latest: true
} as Version,
[
'org/app:2.0.8-beta.67',
'org/app:latest',
'ghcr.io/user/app:2.0.8-beta.67',
'ghcr.io/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.git",
"org.opencontainers.image.version=2.0.8-beta.67",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT"
]
],
])('given %p event ', tagsLabelsTest);
});
describe('pull_request', () => {
// prettier-ignore
test.each([

View file

@ -24,6 +24,9 @@ inputs:
tag-coerce-tag:
description: 'Coerces Git tag to semver if possible using Handlebars template'
required: false
tag-latest-match:
description: 'Set latest tag only if matches with a pattern'
required: false
tag-schedule:
description: 'Handlebars template to apply to schedule tag'
default: 'nightly'

6
dist/index.js generated vendored
View file

@ -26,6 +26,7 @@ function getInputs() {
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
tagEdgeBranch: core.getInput('tag-edge-branch'),
tagCoerceTag: core.getInput('tag-coerce-tag'),
tagLatestMatch: core.getInput('tag-latest-match'),
tagSchedule: core.getInput('tag-schedule') || 'nightly',
sepTags: core.getInput('sep-tags') || `\n`,
sepLabels: core.getInput('sep-labels') || `\n`,
@ -179,6 +180,7 @@ class Meta {
this.date = new Date();
}
version() {
var _a;
const currentDate = this.date;
const version = {
version: undefined,
@ -225,6 +227,10 @@ class Meta {
else if (/^refs\/pull\//.test(this.context.ref)) {
version.version = `pr-${this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '')}`;
}
if (this.inputs.tagLatestMatch) {
const match = (_a = version.version) === null || _a === void 0 ? void 0 : _a.match(new RegExp(this.inputs.tagLatestMatch));
version.latest = match !== null;
}
return version;
}
tags() {

View file

@ -6,6 +6,7 @@ export interface Inputs {
tagEdge: boolean;
tagEdgeBranch: string;
tagCoerceTag: string;
tagLatestMatch: string;
tagSchedule: string;
sepTags: string;
sepLabels: string;
@ -19,6 +20,7 @@ export function getInputs(): Inputs {
tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
tagEdgeBranch: core.getInput('tag-edge-branch'),
tagCoerceTag: core.getInput('tag-coerce-tag'),
tagLatestMatch: core.getInput('tag-latest-match'),
tagSchedule: core.getInput('tag-schedule') || 'nightly',
sepTags: core.getInput('sep-tags') || `\n`,
sepLabels: core.getInput('sep-labels') || `\n`,

View file

@ -68,6 +68,11 @@ export class Meta {
version.version = `pr-${this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, '')}`;
}
if (this.inputs.tagLatestMatch) {
const match = version.version?.match(new RegExp(this.inputs.tagLatestMatch));
version.latest = match !== null;
}
return version;
}