Skip to content

Commit 12528c4

Browse files
authored
Merge pull request #81 from crazy-max/sha-format
Add format attribute for type=sha
2 parents 2af9c6a + 8841ef4 commit 12528c4

File tree

6 files changed

+111
-11
lines changed

6 files changed

+111
-11
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,21 @@ tags: |
528528
type=sha
529529
```
530530

531-
Output Git short commit as Docker tag like `sha-ad132f5`.
531+
```yaml
532+
tags: |
533+
# minimal using short sha
534+
type=sha
535+
# full length sha
536+
type=sha,format=long
537+
```
538+
539+
Output Git short commit (or long if specified) as Docker tag like `sha-ad132f5`.
532540

533541
Extended attributes and default values:
534542

535543
```yaml
536544
tags: |
537-
type=sha,enable=true,priority=100,prefix=sha-,suffix=
545+
type=sha,enable=true,priority=100,prefix=sha-,suffix=,format=short
538546
```
539547

540548
## Notes

__tests__/meta.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,39 @@ describe('push', () => {
617617
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
618618
"org.opencontainers.image.licenses=MIT"
619619
]
620-
]
620+
],
621+
[
622+
'push18',
623+
'event_push.env',
624+
{
625+
images: ['org/app', 'ghcr.io/user/app'],
626+
tags: [
627+
`type=ref,event=branch`,
628+
`type=sha,format=long`
629+
],
630+
} as Inputs,
631+
{
632+
main: 'dev',
633+
partial: ['sha-90dd6032fac8bda1b6c4436a2e65de27961ed071'],
634+
latest: false
635+
} as Version,
636+
[
637+
'org/app:dev',
638+
'org/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071',
639+
'ghcr.io/user/app:dev',
640+
'ghcr.io/user/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071'
641+
],
642+
[
643+
"org.opencontainers.image.title=Hello-World",
644+
"org.opencontainers.image.description=This your first repo!",
645+
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
646+
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
647+
"org.opencontainers.image.version=dev",
648+
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
649+
"org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
650+
"org.opencontainers.image.licenses=MIT"
651+
]
652+
],
621653
])('given %p with %p event', tagsLabelsTest);
622654
});
623655

__tests__/tag.test.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Transform, Parse, Tag, Type, RefEvent, DefaultPriorities} from '../src/tag';
1+
import {Transform, Parse, Tag, Type, RefEvent, ShaFormat, DefaultPriorities} from '../src/tag';
22

33
describe('transform', () => {
44
// prettier-ignore
@@ -89,7 +89,8 @@ describe('transform', () => {
8989
attrs: {
9090
"priority": DefaultPriorities[Type.Sha],
9191
"enable": "true",
92-
"prefix": "sha-"
92+
"prefix": "sha-",
93+
"format": ShaFormat.Short
9394
}
9495
}
9596
] as Tag[],
@@ -355,7 +356,21 @@ describe('parse', () => {
355356
attrs: {
356357
"priority": DefaultPriorities[Type.Sha],
357358
"enable": "true",
358-
"prefix": "sha-"
359+
"prefix": "sha-",
360+
"format": ShaFormat.Short
361+
}
362+
} as Tag,
363+
false
364+
],
365+
[
366+
`type=sha,format=long`,
367+
{
368+
type: Type.Sha,
369+
attrs: {
370+
"priority": DefaultPriorities[Type.Sha],
371+
"enable": "true",
372+
"prefix": "sha-",
373+
"format": ShaFormat.Long
359374
}
360375
} as Tag,
361376
false
@@ -367,7 +382,8 @@ describe('parse', () => {
367382
attrs: {
368383
"priority": DefaultPriorities[Type.Sha],
369384
"enable": "true",
370-
"prefix": ""
385+
"prefix": "",
386+
"format": ShaFormat.Short
371387
}
372388
} as Tag,
373389
false
@@ -379,7 +395,8 @@ describe('parse', () => {
379395
attrs: {
380396
"priority": DefaultPriorities[Type.Sha],
381397
"enable": "false",
382-
"prefix": "sha-"
398+
"prefix": "sha-",
399+
"format": ShaFormat.Short
383400
}
384401
} as Tag,
385402
false
@@ -403,6 +420,11 @@ describe('parse', () => {
403420
`type=sha,enable=foo`,
404421
{} as Tag,
405422
true
423+
],
424+
[
425+
`type=sha,format=foo`,
426+
{} as Tag,
427+
true
406428
]
407429
])('given %p event ', async (s: string, expected: Tag, invalid: boolean) => {
408430
try {

dist/index.js

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/meta.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ export class Meta {
224224
if (!this.context.sha) {
225225
return version;
226226
}
227-
const vraw = this.setValue(this.context.sha.substr(0, 7), tag);
227+
228+
let val = this.context.sha;
229+
if (tag.attrs['format'] === tcl.ShaFormat.Short) {
230+
val = this.context.sha.substr(0, 7);
231+
}
232+
233+
const vraw = this.setValue(val, tag);
228234
return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true');
229235
}
230236

src/tag.ts

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export enum RefEvent {
1717
PR = 'pr'
1818
}
1919

20+
export enum ShaFormat {
21+
Short = 'short',
22+
Long = 'long'
23+
}
24+
2025
export class Tag {
2126
public type?: Type;
2227
public attrs: Record<string, string>;
@@ -175,6 +180,16 @@ export function Parse(s: string): Tag {
175180
if (!tag.attrs.hasOwnProperty('prefix')) {
176181
tag.attrs['prefix'] = 'sha-';
177182
}
183+
if (!tag.attrs.hasOwnProperty('format')) {
184+
tag.attrs['format'] = ShaFormat.Short;
185+
}
186+
if (
187+
!Object.keys(ShaFormat)
188+
.map(k => ShaFormat[k])
189+
.includes(tag.attrs['format'])
190+
) {
191+
throw new Error(`Invalid format for ${s}`);
192+
}
178193
break;
179194
}
180195
}

0 commit comments

Comments
 (0)