Fix error handling

Signed-off-by: Tim Etchells <tetchell@redhat.com>
This commit is contained in:
Tim Etchells 2020-11-23 17:08:21 -05:00
parent 54d567b235
commit a65863e0db
6 changed files with 51 additions and 66 deletions

View file

@ -6,9 +6,7 @@
Buildah is a GitHub Action for building OCI-compatible (Docker- and Kubernetes-compatible) images quickly and easily. Buildah is a GitHub Action for building OCI-compatible (Docker- and Kubernetes-compatible) images quickly and easily.
Buildah action works only on Linux distributions, and it is not supported on Windows or Mac platforms at this time. Buildah only works on Linux. GitHub's [Ubuntu Environments](https://github.com/actions/virtual-environments#available-environments) (`ubuntu-18.04` and newer) come with buildah installed. If you are not using these environments, or if you want to use a different version, you must first [install buildah](https://github.com/containers/buildah/blob/master/install.md).
Note that GitHub's [Ubuntu Environments](https://github.com/actions/virtual-environments#available-environments) (ubuntu-20.04 and ubuntu-18.04) come with buildah 1.17.0 installed. If you are not using these environments, you must first [install buildah](https://github.com/containers/buildah/blob/master/install.md).
After building your image, use [push-to-registry](https://github.com/redhat-actions/push-to-registry) to push the image and make it pullable. After building your image, use [push-to-registry](https://github.com/redhat-actions/push-to-registry) to push the image and make it pullable.

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,5 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import * as exec from "@actions/exec"; import * as exec from "@actions/exec";
import { CommandResult } from "./types";
interface Buildah { interface Buildah {
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[]): Promise<CommandResult>; buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[]): Promise<CommandResult>;
@ -38,29 +37,27 @@ export class BuildahCli implements Buildah {
args.push('-t'); args.push('-t');
args.push(image); args.push(image);
args.push(context); args.push(context);
return await this.execute(args); return this.execute(args);
} }
async from(baseImage: string): Promise<CommandResult> { async from(baseImage: string): Promise<CommandResult> {
return await this.execute(['from', baseImage]); return this.execute(['from', baseImage]);
} }
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult> { async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult | undefined> {
if (contentToCopy.length === 0) {
return undefined;
}
core.debug('copy'); core.debug('copy');
core.debug(container); core.debug(container);
let result: CommandResult;
for (const content of contentToCopy) { for (const content of contentToCopy) {
const args: string[] = ["copy", container, content]; const args: string[] = ["copy", container, content];
if (path) { if (path) {
args.push(path); args.push(path);
} }
result = await this.execute(args); return this.execute(args);
if (result.succeeded === false) {
return result;
}
} }
return result;
} }
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> { async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
@ -82,7 +79,7 @@ export class BuildahCli implements Buildah {
}); });
} }
args.push(container); args.push(container);
return await this.execute(args); return this.execute(args);
} }
async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> { async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> {
@ -90,7 +87,7 @@ export class BuildahCli implements Buildah {
core.debug(container); core.debug(container);
core.debug(newImageName); core.debug(newImageName);
const args: string[] = ["commit", ...flags, container, newImageName]; const args: string[] = ["commit", ...flags, container, newImageName];
return await this.execute(args); return this.execute(args);
} }
private convertArrayToStringArg(args: string[]): string { private convertArrayToStringArg(args: string[]): string {
@ -103,25 +100,27 @@ export class BuildahCli implements Buildah {
private async execute(args: string[]): Promise<CommandResult> { private async execute(args: string[]): Promise<CommandResult> {
if (!this.executable) { if (!this.executable) {
return Promise.reject(new Error('Unable to call buildah executable')); throw new Error('Unable to call buildah executable');
} }
let output = ''; let stdOut = '';
let error = ''; let stdErr = '';
const options: exec.ExecOptions = {}; const options: exec.ExecOptions = {};
options.listeners = { options.listeners = {
stdout: (data: Buffer): void => { stdout: (data: Buffer): void => {
output += data.toString(); stdOut += data.toString();
}, },
stderr: (data: Buffer): void => { stderr: (data: Buffer): void => {
error += data.toString(); stdErr += data.toString();
} }
}; };
const exitCode = await exec.exec(this.executable, args, options); const exitCode = await exec.exec(this.executable, args, options);
if (exitCode === 1) { if (exitCode !== 0) {
return Promise.resolve({ succeeded: false, error }); throw new Error(`Buildah exited with code ${exitCode}`);
} }
return Promise.resolve({ succeeded: true, output }); return {
exitCode, output: stdOut, error: stdErr
};
} }
} }

View file

@ -9,7 +9,7 @@ import { Language } from 'language-recognizer/lib/types';
export async function run(): Promise<void> { export async function run(): Promise<void> {
if (process.env.RUNNER_OS !== 'Linux') { if (process.env.RUNNER_OS !== 'Linux') {
return Promise.reject(new Error('Only linux platform is supported at this time.')); throw new Error('buildah, and therefore this action, only works on Linux. Please use a Linux runner.');
} }
// get buildah cli // get buildah cli
@ -21,23 +21,31 @@ export async function run(): Promise<void> {
const newImage = `${core.getInput('image', { required: true })}:${core.getInput('tag', { required: true })}`; const newImage = `${core.getInput('image', { required: true })}:${core.getInput('tag', { required: true })}`;
if (dockerFiles.length !== 0) { if (dockerFiles.length !== 0) {
doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles); await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles);
} else { } else {
doBuildFromScratch(cli, newImage, workspace); await doBuildFromScratch(cli, newImage, workspace);
} }
core.setOutput("image", newImage);
} }
async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[]): Promise<void> { async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[]): Promise<void> {
const context = path.join(workspace, core.getInput('context')); if (dockerFiles.length === 1) {
const buildArgs = getInputList(core.getInput('build-args')); core.info(`Performing build from Dockerfile`);
dockerFiles = dockerFiles.map(file => path.join(workspace, file));
const build = await cli.buildUsingDocker(newImage, context, dockerFiles, buildArgs);
if (build.succeeded === false) {
return Promise.reject(new Error('Failed building an image from docker files.'));
} }
else {
core.info(`Performing build from ${dockerFiles.length} Dockerfiles`);
}
const context = path.join(workspace, core.getInput('context'));
const buildArgs = getInputList('build-args');
dockerFiles = dockerFiles.map(file => path.join(workspace, file));
await cli.buildUsingDocker(newImage, context, dockerFiles, buildArgs);
} }
async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string) { async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string): Promise<void> {
core.info(`Performing build from scratch`)
let baseImage = core.getInput('base-image'); let baseImage = core.getInput('base-image');
const content = getInputList('content'); const content = getInputList('content');
const entrypoint = getInputList('entrypoint'); const entrypoint = getInputList('entrypoint');
@ -52,23 +60,17 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
const languages = await recognizer.detectLanguages(workspace); const languages = await recognizer.detectLanguages(workspace);
baseImage = await getSuggestedBaseImage(languages); baseImage = await getSuggestedBaseImage(languages);
if (!baseImage) { if (!baseImage) {
return Promise.reject(new Error('No base image found to create a new container')); throw new Error('No base image found to create a new container');
} }
} else { } else {
return Promise.reject(new Error('No base image found to create a new container')); throw new Error('No base image found to create a new container');
} }
} }
const container = await cli.from(baseImage); const container = await cli.from(baseImage);
if (container.succeeded === false) {
return Promise.reject(new Error(container.reason));
}
const containerId = container.output.replace('\n', ''); const containerId = container.output.replace('\n', '');
const copyResult = await cli.copy(containerId, content); await cli.copy(containerId, content);
if (copyResult.succeeded === false) {
return Promise.reject(new Error(copyResult.reason));
}
const newImageConfig: BuildahConfigSettings = { const newImageConfig: BuildahConfigSettings = {
entrypoint: entrypoint, entrypoint: entrypoint,
@ -76,15 +78,8 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
workingdir: workingDir, workingdir: workingDir,
envs: envs envs: envs
}; };
const configResult = await cli.config(containerId, newImageConfig); await cli.config(containerId, newImageConfig);
if (configResult.succeeded === false) { await cli.commit(containerId, newImage, ['--squash']);
return Promise.reject(new Error(configResult.reason));
}
const commit = await cli.commit(containerId, newImage, ['--squash']);
if (commit.succeeded === false) {
return Promise.reject(new Error(commit.reason));
}
} }
function getInputList(name: string): string[] { function getInputList(name: string): string[] {
@ -117,7 +112,6 @@ async function getSuggestedBaseImage(languages: Language[]): Promise<string> {
} }
async function getBaseImageByLanguage(language: Language): Promise<string> { async function getBaseImageByLanguage(language: Language): Promise<string> {
// eslint-disable-next-line no-undef
const rawData = await fs.readFile(path.join(__dirname, '..', 'language-image.json'), 'utf-8'); const rawData = await fs.readFile(path.join(__dirname, '..', 'language-image.json'), 'utf-8');
const languageImageJSON = JSON.parse(rawData); const languageImageJSON = JSON.parse(rawData);
return languageImageJSON[language.name]; return languageImageJSON[language.name];

View file

@ -1,11 +1,5 @@
export interface CommandSucceeeded { type CommandResult = {
readonly succeeded: true; exitCode: number
readonly output?: string; output: string
} error: string
};
export interface CommandFailed {
readonly succeeded: false;
readonly reason?: string;
}
export type CommandResult = CommandFailed | CommandSucceeeded;