Skip to content

Commit

Permalink
Merge pull request #81 from crazy-max/sha-format
Browse files Browse the repository at this point in the history
Add format attribute for type=sha
  • Loading branch information
crazy-max committed May 11, 2021
2 parents 2af9c6a + 8841ef4 commit 12528c4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion __tests__/meta.test.ts
Expand Up @@ -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);
});

Expand Down
32 changes: 27 additions & 5 deletions __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
Expand Down Expand Up @@ -89,7 +89,8 @@ describe('transform', () => {
attrs: {
"priority": DefaultPriorities[Type.Sha],
"enable": "true",
"prefix": "sha-"
"prefix": "sha-",
"format": ShaFormat.Short
}
}
] as Tag[],
Expand Down Expand Up @@ -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
Expand All @@ -367,7 +382,8 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Sha],
"enable": "true",
"prefix": ""
"prefix": "",
"format": ShaFormat.Short
}
} as Tag,
false
Expand All @@ -379,7 +395,8 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Sha],
"enable": "false",
"prefix": "sha-"
"prefix": "sha-",
"format": ShaFormat.Short
}
} as Tag,
false
Expand All @@ -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 {
Expand Down
21 changes: 19 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/meta.ts
Expand Up @@ -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');
}

Expand Down
15 changes: 15 additions & 0 deletions src/tag.ts
Expand Up @@ -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>;
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 12528c4

Please sign in to comment.