Skip to content

Commit 83e4951

Browse files
authored
Merge pull request #553 from crazy-max/tag-names
tag-names output to return tag names without image base name
2 parents 032a4b3 + 9efc872 commit 83e4951

File tree

7 files changed

+70
-20
lines changed

7 files changed

+70
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ The following outputs are available:
314314
|-------------------------|--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
315315
| `version` | String | Docker image version |
316316
| `tags` | String | Docker tags |
317+
| `tag-names` | String | Docker tag names without image base name |
317318
| `labels` | String | Docker labels |
318319
| `annotations` | String | [Annotations](https://github.com/moby/buildkit/blob/master/docs/annotations.md) |
319320
| `json` | String | JSON output of tags and labels |

__tests__/meta.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3936,6 +3936,12 @@ describe('json', () => {
39363936
"user/app:custom",
39373937
"user/app:tags"
39383938
],
3939+
"tag-names": [
3940+
"dev",
3941+
"my",
3942+
"custom",
3943+
"tags"
3944+
],
39393945
"labels": {
39403946
"foo": "",
39413947
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
@@ -3974,6 +3980,10 @@ describe('json', () => {
39743980
"user/app:dev",
39753981
"user/app:my",
39763982
],
3983+
"tag-names": [
3984+
"dev",
3985+
"my",
3986+
],
39773987
"labels": {
39783988
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
39793989
"org.opencontainers.image.description": "This your first repo!",
@@ -4017,6 +4027,13 @@ describe('json', () => {
40174027
"user/app:tags",
40184028
"user/app:latest"
40194029
],
4030+
"tag-names": [
4031+
"release1",
4032+
"my",
4033+
"custom",
4034+
"tags",
4035+
"latest"
4036+
],
40204037
"labels": {
40214038
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
40224039
"org.opencontainers.image.description": "This your first repo!",
@@ -4061,6 +4078,12 @@ describe('json', () => {
40614078
"user/app:custom",
40624079
"user/app:tags"
40634080
],
4081+
"tag-names": [
4082+
"20200110",
4083+
"my",
4084+
"custom",
4085+
"tags"
4086+
],
40644087
"labels": {
40654088
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
40664089
"org.opencontainers.image.description": "This your first repo!",
@@ -4114,6 +4137,15 @@ describe('json', () => {
41144137
"ghcr.io/user/app:tags",
41154138
"ghcr.io/user/app:latest"
41164139
],
4140+
"tag-names": [
4141+
"1.1.1",
4142+
"1.1",
4143+
"1",
4144+
"my",
4145+
"custom",
4146+
"tags",
4147+
"latest",
4148+
],
41174149
"labels": {
41184150
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
41194151
"org.opencontainers.image.description": "This your first repo!",
@@ -4156,6 +4188,11 @@ describe('json', () => {
41564188
"ghcr.io/user/app:custom",
41574189
"ghcr.io/user/app:tags"
41584190
],
4191+
"tag-names": [
4192+
"my",
4193+
"custom",
4194+
"tags",
4195+
],
41594196
"labels": {
41604197
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",
41614198
"org.opencontainers.image.description": "This your first repo!",
@@ -4196,6 +4233,10 @@ describe('json', () => {
41964233
"org/app:v1.1.1",
41974234
"org/app:latest"
41984235
],
4236+
"tag-names": [
4237+
"v1.1.1",
4238+
"latest"
4239+
],
41994240
"labels": {
42004241
"maintainer": "CrazyMax",
42014242
"org.opencontainers.image.created": "2020-01-10T00:30:00.000Z",

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ outputs:
4848
description: 'Generated Docker image version'
4949
tags:
5050
description: 'Generated Docker tags'
51+
tag-names:
52+
description: 'Generated Docker tag names without image base name'
5153
labels:
5254
description: 'Generated Docker labels'
5355
annotations:

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ actionsToolkit.run(
4747
setOutput('version', version.main || '');
4848

4949
// Docker tags
50-
const tags: Array<string> = meta.getTags();
50+
const tags = meta.getTags();
5151
if (tags.length == 0) {
5252
core.warning('No Docker tag has been generated. Check tags input.');
5353
} else {
@@ -58,6 +58,7 @@ actionsToolkit.run(
5858
});
5959
}
6060
setOutput('tags', tags.join(inputs.sepTags));
61+
setOutput('tag-names', meta.getTags(true).join(inputs.sepTags));
6162

6263
// Docker labels
6364
const labels: Array<string> = meta.getLabels();

src/meta.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -494,33 +494,37 @@ export class Meta {
494494
return images;
495495
}
496496

497-
public getTags(): Array<string> {
497+
public getTags(namesOnly?: boolean): Array<string> {
498498
if (!this.version.main) {
499499
return [];
500500
}
501-
502-
const generateTags = (imageName: string, version: string): Array<string> => {
503-
const tags: Array<string> = [];
504-
const prefix = imageName !== '' ? `${imageName}:` : '';
505-
tags.push(`${prefix}${version}`);
506-
for (const partial of this.version.partial) {
507-
tags.push(`${prefix}${partial}`);
508-
}
509-
if (this.version.latest) {
510-
const latestTag = `${this.flavor.prefixLatest ? this.flavor.prefix : ''}latest${this.flavor.suffixLatest ? this.flavor.suffix : ''}`;
511-
tags.push(`${prefix}${Meta.sanitizeTag(latestTag)}`);
512-
}
513-
return tags;
514-
};
501+
if (namesOnly) {
502+
return this.generateTags(this.version.main);
503+
}
515504

516505
const tags: Array<string> = [];
517506
const images = this.getImageNames();
518507
if (images.length > 0) {
519508
for (const imageName of images) {
520-
tags.push(...generateTags(imageName, this.version.main));
509+
tags.push(...this.generateTags(this.version.main, imageName));
521510
}
522511
} else {
523-
tags.push(...generateTags('', this.version.main));
512+
tags.push(...this.generateTags(this.version.main));
513+
}
514+
515+
return tags;
516+
}
517+
518+
private generateTags(version: string, imageName?: string): Array<string> {
519+
const tags: Array<string> = [];
520+
const prefix = imageName ? `${imageName}:` : '';
521+
tags.push(`${prefix}${version}`);
522+
for (const partial of this.version.partial) {
523+
tags.push(`${prefix}${partial}`);
524+
}
525+
if (this.version.latest) {
526+
const latestTag = `${this.flavor.prefixLatest ? this.flavor.prefix : ''}latest${this.flavor.suffixLatest ? this.flavor.suffix : ''}`;
527+
tags.push(`${prefix}${Meta.sanitizeTag(latestTag)}`);
524528
}
525529
return tags;
526530
}
@@ -568,6 +572,7 @@ export class Meta {
568572
}
569573
return {
570574
tags: this.getTags(),
575+
'tag-names': this.getTags(true),
571576
labels: this.getLabels().reduce((res, label) => {
572577
const matches = label.match(/([^=]*)=(.*)/);
573578
if (!matches) {

0 commit comments

Comments
 (0)