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-13 11:38:29 +00:00
|
|
|
import { CommandResult } from "./types";
|
2020-11-06 17:42:45 +00:00
|
|
|
|
|
|
|
interface Buildah {
|
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-13 11:38:29 +00:00
|
|
|
async from(baseImage: string): Promise<CommandResult> {
|
2020-11-06 17:42:45 +00:00
|
|
|
return await this.execute(['from', baseImage]);
|
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
|
|
|
|
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult> {
|
2020-11-06 22:57:01 +00:00
|
|
|
core.debug('copy');
|
|
|
|
core.debug(container);
|
2020-11-13 11:38:29 +00:00
|
|
|
let result: CommandResult;
|
|
|
|
for (const content of contentToCopy) {
|
|
|
|
const args: string[] = ["copy", container, content];
|
|
|
|
if (path) {
|
|
|
|
args.push(path);
|
|
|
|
}
|
|
|
|
result = await this.execute(args);
|
|
|
|
if (result.succeeded === false) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-13 11:38:29 +00:00
|
|
|
|
|
|
|
return result;
|
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);
|
|
|
|
return await this.execute(args);
|
|
|
|
}
|
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];
|
|
|
|
return await this.execute(args);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return Promise.reject(new Error('Unable to call buildah executable'));
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
let error = '';
|
|
|
|
|
2020-11-06 22:57:01 +00:00
|
|
|
const options: exec.ExecOptions = {};
|
|
|
|
options.listeners = {
|
|
|
|
stdout: (data: Buffer): void => {
|
|
|
|
output += data.toString();
|
|
|
|
},
|
|
|
|
stderr: (data: Buffer): void => {
|
|
|
|
error += 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);
|
|
|
|
if (exitCode === 1) {
|
2020-11-06 17:42:45 +00:00
|
|
|
return Promise.resolve({ succeeded: false, error: error });
|
|
|
|
}
|
|
|
|
return Promise.resolve({ succeeded: true, output: output });
|
|
|
|
}
|
|
|
|
}
|