Add format attribute for type=sha

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2021-05-11 20:14:23 +02:00
parent 2af9c6a52b
commit 8841ef4bb7
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
6 changed files with 111 additions and 11 deletions

View file

@ -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

View file

@ -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);
});

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', () => {
// 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 {

21
dist/index.js generated vendored
View file

@ -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;
}
}

View file

@ -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');
}

View file

@ -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<string, string>;
@ -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;
}
}