From 8841ef4bb7fa232d00628632c137f28a313367c7 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 11 May 2021 20:14:23 +0200 Subject: [PATCH] Add format attribute for type=sha Signed-off-by: CrazyMax --- README.md | 12 ++++++++++-- __tests__/meta.test.ts | 34 +++++++++++++++++++++++++++++++++- __tests__/tag.test.ts | 32 +++++++++++++++++++++++++++----- dist/index.js | 21 +++++++++++++++++++-- src/meta.ts | 8 +++++++- src/tag.ts | 15 +++++++++++++++ 6 files changed, 111 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 637e143..e0b7718 100644 --- a/README.md +++ b/README.md @@ -528,13 +528,21 @@ tags: | type=sha ``` -Output Git short commit as Docker tag like `sha-ad132f5`. +```yaml +tags: | + # minimal using short sha + type=sha + # full length sha + type=sha,format=long +``` + +Output Git short commit (or long if specified) as Docker tag like `sha-ad132f5`. Extended attributes and default values: ```yaml tags: | - type=sha,enable=true,priority=100,prefix=sha-,suffix= + type=sha,enable=true,priority=100,prefix=sha-,suffix=,format=short ``` ## Notes diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index c983807..7017459 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -617,7 +617,39 @@ describe('push', () => { "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", "org.opencontainers.image.licenses=MIT" ] - ] + ], + [ + 'push18', + 'event_push.env', + { + images: ['org/app', 'ghcr.io/user/app'], + tags: [ + `type=ref,event=branch`, + `type=sha,format=long` + ], + } as Inputs, + { + main: 'dev', + partial: ['sha-90dd6032fac8bda1b6c4436a2e65de27961ed071'], + latest: false + } as Version, + [ + 'org/app:dev', + 'org/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071', + 'ghcr.io/user/app:dev', + 'ghcr.io/user/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071' + ], + [ + "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=dev", + "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); }); diff --git a/__tests__/tag.test.ts b/__tests__/tag.test.ts index 6c83efd..467b9a3 100644 --- a/__tests__/tag.test.ts +++ b/__tests__/tag.test.ts @@ -1,4 +1,4 @@ -import {Transform, Parse, Tag, Type, RefEvent, DefaultPriorities} from '../src/tag'; +import {Transform, Parse, Tag, Type, RefEvent, ShaFormat, DefaultPriorities} from '../src/tag'; describe('transform', () => { // prettier-ignore @@ -89,7 +89,8 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "true", - "prefix": "sha-" + "prefix": "sha-", + "format": ShaFormat.Short } } ] as Tag[], @@ -355,7 +356,21 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "true", - "prefix": "sha-" + "prefix": "sha-", + "format": ShaFormat.Short + } + } as Tag, + false + ], + [ + `type=sha,format=long`, + { + type: Type.Sha, + attrs: { + "priority": DefaultPriorities[Type.Sha], + "enable": "true", + "prefix": "sha-", + "format": ShaFormat.Long } } as Tag, false @@ -367,7 +382,8 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "true", - "prefix": "" + "prefix": "", + "format": ShaFormat.Short } } as Tag, false @@ -379,7 +395,8 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "false", - "prefix": "sha-" + "prefix": "sha-", + "format": ShaFormat.Short } } as Tag, false @@ -403,6 +420,11 @@ describe('parse', () => { `type=sha,enable=foo`, {} as Tag, true + ], + [ + `type=sha,format=foo`, + {} as Tag, + true ] ])('given %p event ', async (s: string, expected: Tag, invalid: boolean) => { try { diff --git a/dist/index.js b/dist/index.js index 1166c4d..88f3d4e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -555,7 +555,11 @@ class Meta { if (!this.context.sha) { return version; } - const vraw = this.setValue(this.context.sha.substr(0, 7), tag); + let val = this.context.sha; + if (tag.attrs['format'] === tcl.ShaFormat.Short) { + val = this.context.sha.substr(0, 7); + } + const vraw = this.setValue(val, tag); return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true'); } static setVersion(version, val, latest) { @@ -698,7 +702,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.Parse = exports.Transform = exports.DefaultPriorities = exports.Tag = exports.RefEvent = exports.Type = void 0; +exports.Parse = exports.Transform = exports.DefaultPriorities = exports.Tag = exports.ShaFormat = exports.RefEvent = exports.Type = void 0; const sync_1 = __importDefault(__webpack_require__(8750)); const core = __importStar(__webpack_require__(2186)); var Type; @@ -717,6 +721,11 @@ var RefEvent; RefEvent["Tag"] = "tag"; RefEvent["PR"] = "pr"; })(RefEvent = exports.RefEvent || (exports.RefEvent = {})); +var ShaFormat; +(function (ShaFormat) { + ShaFormat["Short"] = "short"; + ShaFormat["Long"] = "long"; +})(ShaFormat = exports.ShaFormat || (exports.ShaFormat = {})); class Tag { constructor() { this.attrs = {}; @@ -863,6 +872,14 @@ function Parse(s) { if (!tag.attrs.hasOwnProperty('prefix')) { tag.attrs['prefix'] = 'sha-'; } + if (!tag.attrs.hasOwnProperty('format')) { + tag.attrs['format'] = ShaFormat.Short; + } + if (!Object.keys(ShaFormat) + .map(k => ShaFormat[k]) + .includes(tag.attrs['format'])) { + throw new Error(`Invalid format for ${s}`); + } break; } } diff --git a/src/meta.ts b/src/meta.ts index 43b47e0..972f3d9 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -224,7 +224,13 @@ export class Meta { if (!this.context.sha) { return version; } - const vraw = this.setValue(this.context.sha.substr(0, 7), tag); + + let val = this.context.sha; + if (tag.attrs['format'] === tcl.ShaFormat.Short) { + val = this.context.sha.substr(0, 7); + } + + const vraw = this.setValue(val, tag); return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true'); } diff --git a/src/tag.ts b/src/tag.ts index 474ca6d..b0c83df 100644 --- a/src/tag.ts +++ b/src/tag.ts @@ -17,6 +17,11 @@ export enum RefEvent { PR = 'pr' } +export enum ShaFormat { + Short = 'short', + Long = 'long' +} + export class Tag { public type?: Type; public attrs: Record; @@ -175,6 +180,16 @@ export function Parse(s: string): Tag { if (!tag.attrs.hasOwnProperty('prefix')) { tag.attrs['prefix'] = 'sha-'; } + if (!tag.attrs.hasOwnProperty('format')) { + tag.attrs['format'] = ShaFormat.Short; + } + if ( + !Object.keys(ShaFormat) + .map(k => ShaFormat[k]) + .includes(tag.attrs['format']) + ) { + throw new Error(`Invalid format for ${s}`); + } break; } }