Skip to content

Commit

Permalink
chore: use anonymous func to generate tags and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Nov 30, 2023
1 parent aacea38 commit d6a296c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 19 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,23 @@ jobs:
${{ steps.docker_meta.outputs.bake-file-annotations }}
targets: |
release
no-images:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Docker meta
uses: ./
with:
tags: |
type=schedule
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=sha
-
name: Print envs
run: env|sort
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ images: |
* `name=<string>` image base name
* `enable=<true|false>` enable this entry (default `true`)

If `images` is empty, tags will be generated without base name.

## `flavor` input

`flavor` defines a global behavior for [`tags`](#tags-input):
Expand Down
110 changes: 110 additions & 0 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,50 @@ describe('push', () => {
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=sha-860c1904a1ce19322e91ac35af1ab07466440c37"
]
],
[
'push21',
'event_push_master.env',
{
images: [] as string[],
tags: [
`type=raw,value=mytag-{{branch}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD'}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}`,
`type=raw,value=mytag-tag-{{tag}}`,
`type=raw,value=mytag-baseref-{{base_ref}}`,
`type=raw,value=mytag-defbranch,enable={{is_default_branch}}`
],
} as Inputs,
{
main: 'mytag-master',
partial: [
'mytag-20200110',
'mytag-20200110-093000',
'mytag-tag-',
'mytag-baseref-',
'mytag-defbranch'
],
latest: false
} as Version,
[
'mytag-master',
'mytag-20200110',
'mytag-20200110-093000',
'mytag-tag-',
'mytag-baseref-',
'mytag-defbranch'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=266574110acf203503badf966df2ea24b5d732d7",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=mytag-master"
]
]
])('given %p with %p event', tagsLabelsTest);
});
Expand Down Expand Up @@ -1780,6 +1824,43 @@ describe('tag', () => {
"org.opencontainers.image.version=v1.2.3rc2"
]
],
[
'tag33',
'event_tag_v1.1.1.env',
{
images: [] as string[],
tags: [
`type=pep440,pattern={{version}}`,
`type=pep440,pattern={{major}}.{{minor}}.{{patch}}`,
`type=pep440,pattern={{major}}.{{minor}}`,
`type=pep440,pattern={{major}}`
]
} as Inputs,
{
main: '1.1.1',
partial: [
"1.1",
"1"
],
latest: true
} as Version,
[
'1.1.1',
'1.1',
'1',
'latest'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=1.1.1"
]
]
])('given %p with %p event', tagsLabelsTest);
});

Expand Down Expand Up @@ -2066,6 +2147,35 @@ describe('latest', () => {
"org.opencontainers.image.version=v1.1.1"
]
],
[
'latest10',
'event_tag_v2.0.8-beta.67.env',
{
images: [] as string[],
tags: [
`type=match,"pattern=\\d.\\d.\\d"`
]
} as Inputs,
{
main: '2.0.8',
partial: [],
latest: true
} as Version,
[
'2.0.8',
'latest',
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=2.0.8"
]
]
])('given %p with %p event', tagsLabelsTest);
});

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inputs:
required: true
images:
description: 'List of Docker images to use as base name for tags'
required: true
required: false
tags:
description: 'List of tags as key-value pair attributes'
required: false
Expand Down
36 changes: 18 additions & 18 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,29 @@ export class Meta {
if (!this.version.main) {
return [];
}
const tags: Array<string> = [];
const images = this.getImageNames();
if (Array.isArray(images) && images.length) {
for (const imageName of images) {
tags.push(`${imageName}:${this.version.main}`);
for (const partial of this.version.partial) {
tags.push(`${imageName}:${partial}`);
}
if (this.version.latest) {
const latestTag = `${this.flavor.prefixLatest ? this.flavor.prefix : ''}latest${this.flavor.suffixLatest ? this.flavor.suffix : ''}`;
tags.push(`${imageName}:${Meta.sanitizeTag(latestTag)}`);
}
}
}
else {
tags.push(this.version.main);

const generateTags = (imageName: string, version: string): Array<string> => {
const tags: Array<string> = [];
const prefix = imageName !== '' ? `${imageName}:` : '';
tags.push(`${prefix}${version}`);
for (const partial of this.version.partial) {
tags.push(partial);
tags.push(`${prefix}${partial}`);
}
if (this.version.latest) {
const latestTag = `${this.flavor.prefixLatest ? this.flavor.prefix : ''}latest${this.flavor.suffixLatest ? this.flavor.suffix : ''}`;
tags.push(Meta.sanitizeTag(latestTag));
tags.push(`${prefix}${Meta.sanitizeTag(latestTag)}`);
}
return tags;
};

const tags: Array<string> = [];
const images = this.getImageNames();
if (images.length > 0) {
for (const imageName of images) {
tags.push(...generateTags(imageName, this.version.main));
}
} else {
tags.push(...generateTags('', this.version.main));
}
return tags;
}
Expand Down

0 comments on commit d6a296c

Please sign in to comment.