Add feature to output image with multiple tags (#21)

Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
Divyanshu Agrawal 2021-02-01 23:24:50 +05:30 committed by GitHub
parent 75dab40354
commit 88e0085544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1971 additions and 154 deletions

View file

@ -1,14 +1,7 @@
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as path from "path";
interface Buildah {
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
config(container: string, setting: {}): Promise<CommandResult>;
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
}
import CommandResult from "./types";
export interface BuildahConfigSettings {
entrypoint?: string[];
@ -17,70 +10,83 @@ export interface BuildahConfigSettings {
workingdir?: string;
}
export class BuildahCli implements Buildah {
interface Buildah {
buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
): Promise<CommandResult>;
from(baseImage: string): Promise<CommandResult>;
copy(container: string, contentToCopy: string[]): Promise<CommandResult | undefined>;
config(container: string, setting: BuildahConfigSettings): Promise<CommandResult>;
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
}
private executable: string;
export class BuildahCli implements Buildah {
private readonly executable: string;
constructor(executable: string) {
this.executable = executable;
}
private getImageFormatOption(useOCI: boolean): string[] {
return [ '--format', useOCI ? 'oci' : 'docker' ];
private static getImageFormatOption(useOCI: boolean): string[] {
return [ "--format", useOCI ? "oci" : "docker" ];
}
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult> {
const args: string[] = ['bud'];
dockerFiles.forEach(file => {
args.push('-f');
async buildUsingDocker(
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
): Promise<CommandResult> {
const args: string[] = [ "bud" ];
dockerFiles.forEach((file) => {
args.push("-f");
args.push(file);
});
buildArgs.forEach((buildArg) => {
args.push('--build-arg');
args.push("--build-arg");
args.push(buildArg);
});
args.push(...this.getImageFormatOption(useOCI));
args.push('-t');
args.push(...BuildahCli.getImageFormatOption(useOCI));
args.push("-t");
args.push(image);
args.push(context);
return this.execute(args);
}
async from(baseImage: string): Promise<CommandResult> {
return this.execute(['from', baseImage]);
return this.execute([ "from", baseImage ]);
}
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult | undefined> {
async copy(container: string, contentToCopy: string[], contentPath?: string): Promise<CommandResult | undefined> {
if (contentToCopy.length === 0) {
return undefined;
}
core.debug('copy');
core.debug("copy");
core.debug(container);
for (const content of contentToCopy) {
const args: string[] = ["copy", container, content];
if (path) {
args.push(path);
const args: string[] = [ "copy", container, content ];
if (contentPath) {
args.push(contentPath);
}
return this.execute(args);
}
return undefined;
}
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
core.debug('config');
core.debug("config");
core.debug(container);
const args: string[] = ['config'];
const args: string[] = [ "config" ];
if (settings.entrypoint) {
args.push('--entrypoint');
args.push(this.convertArrayToStringArg(settings.entrypoint));
args.push("--entrypoint");
args.push(BuildahCli.convertArrayToStringArg(settings.entrypoint));
}
if (settings.port) {
args.push('--port');
args.push("--port");
args.push(settings.port);
}
if (settings.envs) {
settings.envs.forEach((env) => {
args.push('--env');
args.push("--env");
args.push(env);
});
}
@ -89,23 +95,34 @@ export class BuildahCli implements Buildah {
}
async commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult> {
core.debug('commit');
core.debug("commit");
core.debug(container);
core.debug(newImageName);
const args: string[] = [ 'commit', ...this.getImageFormatOption(useOCI), '--squash', container, newImageName ];
const args: string[] = [
"commit", ...BuildahCli.getImageFormatOption(useOCI),
"--squash", container, newImageName,
];
return this.execute(args);
}
private convertArrayToStringArg(args: string[]): string {
let arrayAsString = '[';
args.forEach(arg => {
async tag(imageName: string, tags: string[]): Promise<CommandResult> {
const args: string[] = [ "tag" ];
for (const tag of tags) {
args.push(`${imageName}:${tag}`);
}
core.info(`Tagging the built image with tags ${tags.toString()}`);
return this.execute(args);
}
private static convertArrayToStringArg(args: string[]): string {
let arrayAsString = "[";
args.forEach((arg) => {
arrayAsString += `"${arg}",`;
});
return `${arrayAsString.slice(0, -1)}]`;
}
private async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)
let stdout = "";
@ -115,18 +132,19 @@ export class BuildahCli implements Buildah {
finalExecOptions.ignoreReturnCode = true; // the return code is processed below
finalExecOptions.listeners = {
stdline: (line) => {
stdline: (line): void => {
stdout += line + "\n";
},
errline: (line) => {
stderr += line + "\n"
errline: (line):void => {
stderr += line + "\n";
},
}
};
const exitCode = await exec.exec(this.executable, args, finalExecOptions);
if (execOptions.ignoreReturnCode !== true && exitCode !== 0) {
// Throwing the stderr as part of the Error makes the stderr show up in the action outline, which saves some clicking when debugging.
// Throwing the stderr as part of the Error makes the stderr
// show up in the action outline, which saves some clicking when debugging.
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
if (stderr) {
error += `\n${stderr}`;
@ -135,7 +153,7 @@ export class BuildahCli implements Buildah {
}
return {
exitCode, output: stdout, error: stderr
exitCode, output: stdout, error: stderr,
};
}
}