From 0dac4059e9e691fcc25839f8758730ae0e48f51e Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Mon, 26 Oct 2020 17:51:00 +0100 Subject: [PATCH] Set latest tag only if matches with a pattern --- README.md | 9 +- __tests__/meta.test.ts | 185 +++++++++++++++++++++++++++++++++++++++++ action.yml | 3 + dist/index.js | 6 ++ src/context.ts | 2 + src/meta.ts | 5 ++ 6 files changed, 207 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7efa436..8062ad3 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index 6ea0f5b..cb6e377 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -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([ diff --git a/action.yml b/action.yml index 973f8ba..75b98e8 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/index.js b/dist/index.js index 2b34e4a..eb2b608 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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() { diff --git a/src/context.ts b/src/context.ts index fcc39f4..6988520 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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`, diff --git a/src/meta.ts b/src/meta.ts index c8bb127..9288369 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -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; }