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
8 changed files with 225 additions and 9 deletions

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
}
}