2020-11-06 22:57:01 +00:00
|
|
|
import * as core from "@actions/core";
|
2020-11-06 17:42:45 +00:00
|
|
|
import * as exec from "@actions/exec";
|
2020-11-26 18:52:13 +00:00
|
|
|
import * as path from "path";
|
2020-11-06 17:42:45 +00:00
|
|
|
|
|
|
|
interface Buildah {
|
2020-11-26 18:00:17 +00:00
|
|
|
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult>;
|
2020-11-13 11:38:29 +00:00
|
|
|
from(baseImage: string): Promise<CommandResult>;
|
|
|
|
copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
|
2020-11-06 17:42:45 +00:00
|
|
|
config(container: string, setting: {}): Promise<CommandResult>;
|
2020-11-26 18:00:17 +00:00
|
|
|
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface BuildahConfigSettings {
|
|
|
|
entrypoint?: string[];
|
|
|
|
envs?: string[];
|
|
|
|
port?: string;
|
2020-11-13 11:38:29 +00:00
|
|
|
workingdir?: string;
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BuildahCli implements Buildah {
|
2020-11-13 11:38:29 +00:00
|
|
|
|
2020-11-06 17:42:45 +00:00
|
|
|
private executable: string;
|
2020-11-13 11:38:29 +00:00
|
|
|
|
2020-11-06 17:42:45 +00:00
|
|
|
constructor(executable: string) {
|
|
|
|
this.executable = executable;
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:00:17 +00:00
|
|
|
private getImageFormatOption(useOCI: boolean): string[] {
|
|
|
|
return [ '--format', useOCI ? 'oci' : 'docker' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult> {
|
2020-11-19 08:19:57 +00:00
|
|
|
const args: string[] = ['bud'];
|
|
|
|
dockerFiles.forEach(file => {
|
|
|
|
args.push('-f');
|
|
|
|
args.push(file);
|
|
|
|
});
|
2020-11-23 19:03:32 +00:00
|
|
|
buildArgs.forEach((buildArg) => {
|
|
|
|
args.push('--build-arg');
|
|
|
|
args.push(buildArg);
|
2020-11-26 18:00:17 +00:00
|
|
|
});
|
|
|
|
args.push(...this.getImageFormatOption(useOCI));
|
2020-11-19 08:19:57 +00:00
|
|
|
args.push('-t');
|
|
|
|
args.push(image);
|
|
|
|
args.push(context);
|
2020-11-23 22:08:21 +00:00
|
|
|
return this.execute(args);
|
2020-11-19 08:19:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
async from(baseImage: string): Promise<CommandResult> {
|
2020-11-23 22:08:21 +00:00
|
|
|
return this.execute(['from', baseImage]);
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
|
2020-11-23 22:08:21 +00:00
|
|
|
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult | undefined> {
|
|
|
|
if (contentToCopy.length === 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-11-06 22:57:01 +00:00
|
|
|
core.debug('copy');
|
|
|
|
core.debug(container);
|
2020-11-13 11:38:29 +00:00
|
|
|
for (const content of contentToCopy) {
|
|
|
|
const args: string[] = ["copy", container, content];
|
|
|
|
if (path) {
|
|
|
|
args.push(path);
|
|
|
|
}
|
2020-11-23 22:08:21 +00:00
|
|
|
return this.execute(args);
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
|
2020-11-06 17:42:45 +00:00
|
|
|
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
|
2020-11-06 22:57:01 +00:00
|
|
|
core.debug('config');
|
|
|
|
core.debug(container);
|
2020-11-06 17:42:45 +00:00
|
|
|
const args: string[] = ['config'];
|
|
|
|
if (settings.entrypoint) {
|
|
|
|
args.push('--entrypoint');
|
2020-11-07 12:25:52 +00:00
|
|
|
args.push(this.convertArrayToStringArg(settings.entrypoint));
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
if (settings.port) {
|
|
|
|
args.push('--port');
|
2020-11-07 11:43:03 +00:00
|
|
|
args.push(settings.port);
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
if (settings.envs) {
|
|
|
|
settings.envs.forEach((env) => {
|
|
|
|
args.push('--env');
|
|
|
|
args.push(env);
|
|
|
|
});
|
|
|
|
}
|
2020-11-06 17:42:45 +00:00
|
|
|
args.push(container);
|
2020-11-23 22:08:21 +00:00
|
|
|
return this.execute(args);
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
|
2020-11-26 18:00:17 +00:00
|
|
|
async commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult> {
|
2020-11-06 22:57:01 +00:00
|
|
|
core.debug('commit');
|
|
|
|
core.debug(container);
|
|
|
|
core.debug(newImageName);
|
2020-11-26 18:00:17 +00:00
|
|
|
const args: string[] = [ 'commit', ...this.getImageFormatOption(useOCI), '--squash', container, newImageName ];
|
2020-11-23 22:08:21 +00:00
|
|
|
return this.execute(args);
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 12:25:52 +00:00
|
|
|
private convertArrayToStringArg(args: string[]): string {
|
|
|
|
let arrayAsString = '[';
|
|
|
|
args.forEach(arg => {
|
|
|
|
arrayAsString += `"${arg}",`;
|
|
|
|
});
|
|
|
|
return `${arrayAsString.slice(0, -1)}]`;
|
|
|
|
}
|
2020-11-06 17:42:45 +00:00
|
|
|
|
2020-11-26 18:52:13 +00:00
|
|
|
private async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
|
|
|
|
|
|
|
|
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)
|
|
|
|
|
|
|
|
let stdout = "";
|
|
|
|
let stderr = "";
|
2020-11-06 17:42:45 +00:00
|
|
|
|
2020-11-26 18:52:13 +00:00
|
|
|
const finalExecOptions = { ...execOptions };
|
|
|
|
finalExecOptions.ignoreReturnCode = true; // the return code is processed below
|
2020-11-23 19:03:32 +00:00
|
|
|
|
2020-11-26 18:52:13 +00:00
|
|
|
finalExecOptions.listeners = {
|
|
|
|
stdline: (line) => {
|
|
|
|
stdout += line + "\n";
|
2020-11-06 22:57:01 +00:00
|
|
|
},
|
2020-11-26 18:52:13 +00:00
|
|
|
errline: (line) => {
|
|
|
|
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.
|
|
|
|
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
|
|
|
|
if (stderr) {
|
|
|
|
error += `\n${stderr}`;
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-26 18:52:13 +00:00
|
|
|
throw new Error(error);
|
2020-11-23 19:03:32 +00:00
|
|
|
}
|
2020-11-26 18:52:13 +00:00
|
|
|
|
2020-11-23 22:08:21 +00:00
|
|
|
return {
|
2020-11-26 18:52:13 +00:00
|
|
|
exitCode, output: stdout, error: stderr
|
2020-11-23 22:08:21 +00:00
|
|
|
};
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-23 19:03:32 +00:00
|
|
|
}
|