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";
|
|
|
|
|
|
|
|
interface Buildah {
|
2020-11-23 19:03:32 +00:00
|
|
|
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[]): 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>;
|
|
|
|
commit(container: string, newImageName: string, flags?: string[]): Promise<CommandResult>;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-23 19:03:32 +00:00
|
|
|
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[]): 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-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-06 17:42:45 +00:00
|
|
|
async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> {
|
2020-11-06 22:57:01 +00:00
|
|
|
core.debug('commit');
|
|
|
|
core.debug(container);
|
|
|
|
core.debug(newImageName);
|
2020-11-06 17:42:45 +00:00
|
|
|
const args: string[] = ["commit", ...flags, 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
|
|
|
|
|
|
|
private async execute(args: string[]): Promise<CommandResult> {
|
|
|
|
if (!this.executable) {
|
2020-11-23 22:08:21 +00:00
|
|
|
throw new Error('Unable to call buildah executable');
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 22:08:21 +00:00
|
|
|
let stdOut = '';
|
|
|
|
let stdErr = '';
|
2020-11-23 19:03:32 +00:00
|
|
|
|
2020-11-06 22:57:01 +00:00
|
|
|
const options: exec.ExecOptions = {};
|
|
|
|
options.listeners = {
|
|
|
|
stdout: (data: Buffer): void => {
|
2020-11-23 22:08:21 +00:00
|
|
|
stdOut += data.toString();
|
2020-11-06 22:57:01 +00:00
|
|
|
},
|
|
|
|
stderr: (data: Buffer): void => {
|
2020-11-23 22:08:21 +00:00
|
|
|
stdErr += data.toString();
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
};
|
2020-11-06 22:57:01 +00:00
|
|
|
const exitCode = await exec.exec(this.executable, args, options);
|
2020-11-23 22:08:21 +00:00
|
|
|
if (exitCode !== 0) {
|
|
|
|
throw new Error(`Buildah exited with code ${exitCode}`);
|
2020-11-23 19:03:32 +00:00
|
|
|
}
|
2020-11-23 22:08:21 +00:00
|
|
|
return {
|
|
|
|
exitCode, output: stdOut, error: stdErr
|
|
|
|
};
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-23 19:03:32 +00:00
|
|
|
}
|