From e4f548552dbf80127b6e8e3f601739db39477c6c Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 19 Aug 2021 19:58:11 +0200 Subject: [PATCH] Add global expression "date" Signed-off-by: CrazyMax --- README.md | 1 + __tests__/meta.test.ts | 4 +++- dist/index.js | 4 ++++ src/meta.ts | 4 ++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 419820d..220b7b0 100644 --- a/README.md +++ b/README.md @@ -612,6 +612,7 @@ attributes are available: | `{{branch}}` | `master` | | `{{tag}}` | `v1.2.3` | | `{{sha}}` | `90dd603` | +| `{{date 'YYYYMMDD'}}` | `20210326` | ```yaml tags: | diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index c907526..f69d547 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -585,16 +585,18 @@ describe('push', () => { images: ['user/app'], tags: [ `type=raw,value=mytag-{{branch}}`, + `type=raw,value=mytag-{{date 'YYYYMMDD'}}`, `type=raw,value=mytag-{{tag}}` ], } as Inputs, { main: 'mytag-master', - partial: ['mytag-'], + partial: ['mytag-20200110', 'mytag-'], latest: false } as Version, [ 'user/app:mytag-master', + 'user/app:mytag-20200110', 'user/app:mytag-' ], [ diff --git a/dist/index.js b/dist/index.js index 916aace..3eb91a0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -682,6 +682,7 @@ class Meta { } setGlobalExp(val) { const ctx = this.context; + const currentDate = this.date; return handlebars.compile(val)({ branch: function () { if (!/^refs\/heads\//.test(ctx.ref)) { @@ -697,6 +698,9 @@ class Meta { }, sha: function () { return ctx.sha.substr(0, 7); + }, + date: function (format) { + return moment_1.default(currentDate).utc().format(format); } }); } diff --git a/src/meta.ts b/src/meta.ts index 5746d14..ff29c65 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -323,6 +323,7 @@ export class Meta { private setGlobalExp(val): string { const ctx = this.context; + const currentDate = this.date; return handlebars.compile(val)({ branch: function () { if (!/^refs\/heads\//.test(ctx.ref)) { @@ -338,6 +339,9 @@ export class Meta { }, sha: function () { return ctx.sha.substr(0, 7); + }, + date: function (format) { + return moment(currentDate).utc().format(format); } }); }