Merge pull request #81 from crazy-max/sha-format

Add format attribute for type=sha
This commit is contained in:
CrazyMax 2021-05-11 20:28:33 +02:00 committed by GitHub
commit 12528c4f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 111 additions and 11 deletions

View File

@ -528,13 +528,21 @@ tags: |
type=sha 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: Extended attributes and default values:
```yaml ```yaml
tags: | tags: |
type=sha,enable=true,priority=100,prefix=sha-,suffix= type=sha,enable=true,priority=100,prefix=sha-,suffix=,format=short
``` ```
## Notes ## Notes

View File

@ -617,7 +617,39 @@ describe('push', () => {
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
"org.opencontainers.image.licenses=MIT" "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); ])('given %p with %p event', tagsLabelsTest);
}); });

View File

@ -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', () => { describe('transform', () => {
// prettier-ignore // prettier-ignore
@ -89,7 +89,8 @@ describe('transform', () => {
attrs: { attrs: {
"priority": DefaultPriorities[Type.Sha], "priority": DefaultPriorities[Type.Sha],
"enable": "true", "enable": "true",
"prefix": "sha-" "prefix": "sha-",
"format": ShaFormat.Short
} }
} }
] as Tag[], ] as Tag[],
@ -355,7 +356,21 @@ describe('parse', () => {
attrs: { attrs: {
"priority": DefaultPriorities[Type.Sha], "priority": DefaultPriorities[Type.Sha],
"enable": "true", "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, } as Tag,
false false
@ -367,7 +382,8 @@ describe('parse', () => {
attrs: { attrs: {
"priority": DefaultPriorities[Type.Sha], "priority": DefaultPriorities[Type.Sha],
"enable": "true", "enable": "true",
"prefix": "" "prefix": "",
"format": ShaFormat.Short
} }
} as Tag, } as Tag,
false false
@ -379,7 +395,8 @@ describe('parse', () => {
attrs: { attrs: {
"priority": DefaultPriorities[Type.Sha], "priority": DefaultPriorities[Type.Sha],
"enable": "false", "enable": "false",
"prefix": "sha-" "prefix": "sha-",
"format": ShaFormat.Short
} }
} as Tag, } as Tag,
false false
@ -403,6 +420,11 @@ describe('parse', () => {
`type=sha,enable=foo`, `type=sha,enable=foo`,
{} as Tag, {} as Tag,
true true
],
[
`type=sha,format=foo`,
{} as Tag,
true
] ]
])('given %p event ', async (s: string, expected: Tag, invalid: boolean) => { ])('given %p event ', async (s: string, expected: Tag, invalid: boolean) => {
try { try {

21
dist/index.js generated vendored
View File

@ -555,7 +555,11 @@ class Meta {
if (!this.context.sha) { if (!this.context.sha) {
return version; 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'); return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true');
} }
static setVersion(version, val, latest) { static setVersion(version, val, latest) {
@ -698,7 +702,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); 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 sync_1 = __importDefault(__webpack_require__(8750));
const core = __importStar(__webpack_require__(2186)); const core = __importStar(__webpack_require__(2186));
var Type; var Type;
@ -717,6 +721,11 @@ var RefEvent;
RefEvent["Tag"] = "tag"; RefEvent["Tag"] = "tag";
RefEvent["PR"] = "pr"; RefEvent["PR"] = "pr";
})(RefEvent = exports.RefEvent || (exports.RefEvent = {})); })(RefEvent = exports.RefEvent || (exports.RefEvent = {}));
var ShaFormat;
(function (ShaFormat) {
ShaFormat["Short"] = "short";
ShaFormat["Long"] = "long";
})(ShaFormat = exports.ShaFormat || (exports.ShaFormat = {}));
class Tag { class Tag {
constructor() { constructor() {
this.attrs = {}; this.attrs = {};
@ -863,6 +872,14 @@ function Parse(s) {
if (!tag.attrs.hasOwnProperty('prefix')) { if (!tag.attrs.hasOwnProperty('prefix')) {
tag.attrs['prefix'] = 'sha-'; 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; break;
} }
} }

View File

@ -224,7 +224,13 @@ export class Meta {
if (!this.context.sha) { if (!this.context.sha) {
return version; 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'); return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true');
} }

View File

@ -17,6 +17,11 @@ export enum RefEvent {
PR = 'pr' PR = 'pr'
} }
export enum ShaFormat {
Short = 'short',
Long = 'long'
}
export class Tag { export class Tag {
public type?: Type; public type?: Type;
public attrs: Record<string, string>; public attrs: Record<string, string>;
@ -175,6 +180,16 @@ export function Parse(s: string): Tag {
if (!tag.attrs.hasOwnProperty('prefix')) { if (!tag.attrs.hasOwnProperty('prefix')) {
tag.attrs['prefix'] = 'sha-'; 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; break;
} }
} }