Skip to content

Commit

Permalink
attribute to enable/disable images
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Apr 22, 2022
1 parent 0950af0 commit 4cbf34c
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 9 deletions.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -285,6 +286,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
tags: |
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):
Expand Down
82 changes: 82 additions & 0 deletions __tests__/image.test.ts
@@ -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);
}
});
});
33 changes: 33 additions & 0 deletions __tests__/meta.test.ts
Expand Up @@ -689,6 +689,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);
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions src/image.ts
@@ -0,0 +1,50 @@
import csvparse from 'csv-parse/lib/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 = csvparse(input, {
relaxColumnCount: true,
skipLinesWithEmptyValues: 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;
}
26 changes: 19 additions & 7 deletions src/meta.ts
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -37,6 +39,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();
Expand Down Expand Up @@ -375,20 +378,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;
Expand Down Expand Up @@ -441,7 +453,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
}
}
Expand Down

0 comments on commit 4cbf34c

Please sign in to comment.