3
0
Fork 0
mirror of https://gitea.com/docker/metadata-action.git synced 2024-11-21 19:49:32 +01:00

attribute to enable/disable images

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-04-27 16:58:50 +02:00
parent be6d2cc1df
commit a5680a6642
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
8 changed files with 225 additions and 9 deletions

View file

@ -146,6 +146,21 @@ jobs:
prefix=foo-
suffix=-bar
images:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Docker meta
uses: ./
with:
images: |
name=${{ env.DOCKER_IMAGE }}
name=ghcr.io/name/app,enable=${{ github.event_name == 'pull_request' }}
name=ghcr.io/name/release,enable=${{ startsWith(github.ref, 'refs/tags/') }}
labels:
runs-on: ubuntu-latest
steps:

View file

@ -19,6 +19,7 @@ ___
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
* [`images` input](#images-input)
* [`flavor` input](#flavor-input)
* [`tags` input](#tags-input)
* [`type=schedule`](#typeschedule)
@ -291,6 +292,29 @@ Following outputs are available
| `json` | String | JSON output of tags and labels |
| `bake-file` | File | [Bake definition file](https://github.com/docker/buildx#file-definition) path |
## `images` input
`images` defines a list of Docker images to use as base name for [`tags`](#tags-input):
```yaml
images: |
name/foo
ghcr.io/name/bar
# or
name=name/foo
name=ghcr.io/name/bar
```
Extended attributes and default values:
```yaml
images: |
name=,enable=true
```
* `name=<string>` image base name
* `enable=<true|false>` enable this entry (default `true`)
## `flavor` input
`flavor` defines a global behavior for [`tags`](#tags-input):

82
__tests__/image.test.ts Normal file
View file

@ -0,0 +1,82 @@
import {describe, expect, test} from '@jest/globals';
import {Transform, Image} from '../src/image';
describe('transform', () => {
// prettier-ignore
test.each([
[
[
`name/foo`
],
[
{
name: `name/foo`,
enable: true,
}
] as Image[],
false
],
[
[
`name/foo`,
`name/bar`
],
[
{
name: `name/foo`,
enable: true,
},
{
name: `name/bar`,
enable: true,
}
] as Image[],
false
],
[
[
`name=name/bar`,
`name/foo,enable=false`,
`name=ghcr.io/name/foo,enable=true`
],
[
{
name: `name/bar`,
enable: true,
},
{
name: `name/foo`,
enable: false,
},
{
name: `ghcr.io/name/foo`,
enable: true,
},
] as Image[],
false
],
[
[`value=name/foo`], undefined, true
],
[
[`name/foo,enable=bar`], undefined, true
],
[
[`name/foo,bar=baz`], undefined, true
],
[
[`name=,enable=true`], undefined, true
]
])('given %p', async (l: string[], expected: Image[], invalid: boolean) => {
try {
const images = Transform(l);
expect(images).toEqual(expected);
} catch (err) {
if (!invalid) {
console.error(err);
}
// eslint-disable-next-line jest/no-conditional-expect
expect(true).toBe(invalid);
}
});
});

View file

@ -693,6 +693,39 @@ describe('push', () => {
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.licenses=MIT"
]
],
[
'push20',
'event_push_dev.env',
{
images: [
'org/app',
'ghcr.io/user/app,enable=false'
],
tags: [
`type=edge,branch=master`,
`type=ref,event=branch,enable=false`,
`type=sha,format=long`
],
} as Inputs,
{
main: 'sha-860c1904a1ce19322e91ac35af1ab07466440c37',
partial: [],
latest: false
} as Version,
[
'org/app:sha-860c1904a1ce19322e91ac35af1ab07466440c37'
],
[
"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=sha-860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.licenses=MIT"
]
]
])('given %p with %p event', tagsLabelsTest);
});

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

50
src/image.ts Normal file
View file

@ -0,0 +1,50 @@
import {parse} from 'csv-parse/sync';
export interface Image {
name: string;
enable: boolean;
}
export function Transform(inputs: string[]): Image[] {
const images: Image[] = [];
for (const input of inputs) {
const image: Image = {name: '', enable: true};
const fields = parse(input, {
relaxColumnCount: true,
skipEmptyLines: true
})[0];
for (const field of fields) {
const parts = field
.toString()
.split('=')
.map(item => item.trim());
if (parts.length == 1) {
image.name = parts[0].toLowerCase();
} else {
const key = parts[0].toLowerCase();
const value = parts[1];
switch (key) {
case 'name': {
image.name = value.toLowerCase();
break;
}
case 'enable': {
if (!['true', 'false'].includes(value)) {
throw new Error(`Invalid enable attribute value: ${input}`);
}
image.enable = /true/i.test(value);
break;
}
default: {
throw new Error(`Unknown image attribute: ${input}`);
}
}
}
}
if (image.name.length == 0) {
throw new Error(`Image name attribute empty: ${input}`);
}
images.push(image);
}
return images;
}

View file

@ -6,6 +6,7 @@ import * as pep440 from '@renovate/pep440';
import * as semver from 'semver';
import {Inputs, tmpDir} from './context';
import {ReposGetResponseData} from './github';
import * as icl from './image';
import * as tcl from './tag';
import * as fcl from './flavor';
import * as core from '@actions/core';
@ -23,6 +24,7 @@ export class Meta {
private readonly inputs: Inputs;
private readonly context: Context;
private readonly repo: ReposGetResponseData;
private readonly images: icl.Image[];
private readonly tags: tcl.Tag[];
private readonly flavor: fcl.Flavor;
private readonly date: Date;
@ -38,6 +40,7 @@ export class Meta {
this.inputs = inputs;
this.context = context;
this.repo = repo;
this.images = icl.Transform(inputs.images);
this.tags = tcl.Transform(inputs.tags);
this.flavor = fcl.Transform(inputs.flavor);
this.date = new Date();
@ -404,20 +407,29 @@ export class Meta {
});
}
private getImageNames(): Array<string> {
const images: Array<string> = [];
for (const image of this.images) {
if (!image.enable) {
continue;
}
images.push(image.name);
}
return images;
}
public getTags(): Array<string> {
if (!this.version.main) {
return [];
}
const tags: Array<string> = [];
for (const image of this.inputs.images) {
const imageLc = image.toLowerCase();
tags.push(`${imageLc}:${this.version.main}`);
for (const imageName of this.getImageNames()) {
tags.push(`${imageName}:${this.version.main}`);
for (const partial of this.version.partial) {
tags.push(`${imageLc}:${partial}`);
tags.push(`${imageName}:${partial}`);
}
if (this.version.latest) {
tags.push(`${imageLc}:${this.flavor.prefixLatest ? this.flavor.prefix : ''}latest${this.flavor.suffixLatest ? this.flavor.suffix : ''}`);
tags.push(`${imageName}:${this.flavor.prefixLatest ? this.flavor.prefix : ''}latest${this.flavor.suffixLatest ? this.flavor.suffix : ''}`);
}
}
return tags;
@ -470,7 +482,7 @@ export class Meta {
return res;
}, {}),
args: {
DOCKER_META_IMAGES: this.inputs.images.join(','),
DOCKER_META_IMAGES: this.getImageNames().join(','),
DOCKER_META_VERSION: this.version.main
}
}