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 action works only on Linux distributions, and it is not supported on Windows or Mac platforms at this time.
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).
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).
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 exec from "@actions/exec";
import { CommandResult } from "./types";
interface Buildah {
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(image);
args.push(context);
return await this.execute(args);
return this.execute(args);
}
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(container);
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;
}
return this.execute(args);
}
return result;
}
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
@ -82,7 +79,7 @@ export class BuildahCli implements Buildah {
});
}
args.push(container);
return await this.execute(args);
return this.execute(args);
}
async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> {
@ -90,7 +87,7 @@ export class BuildahCli implements Buildah {
core.debug(container);
core.debug(newImageName);
const args: string[] = ["commit", ...flags, container, newImageName];
return await this.execute(args);
return this.execute(args);
}
private convertArrayToStringArg(args: string[]): string {
@ -103,25 +100,27 @@ export class BuildahCli implements Buildah {
private async execute(args: string[]): Promise<CommandResult> {
if (!this.executable) {
return Promise.reject(new Error('Unable to call buildah executable'));
throw new Error('Unable to call buildah executable');
}
let output = '';
let error = '';
let stdOut = '';
let stdErr = '';
const options: exec.ExecOptions = {};
options.listeners = {
stdout: (data: Buffer): void => {
output += data.toString();
stdOut += data.toString();
},
stderr: (data: Buffer): void => {
error += data.toString();
stdErr += data.toString();
}
};
const exitCode = await exec.exec(this.executable, args, options);
if (exitCode === 1) {
return Promise.resolve({ succeeded: false, error });
if (exitCode !== 0) {
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> {
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
@ -21,23 +21,31 @@ export async function run(): Promise<void> {
const newImage = `${core.getInput('image', { required: true })}:${core.getInput('tag', { required: true })}`;
if (dockerFiles.length !== 0) {
doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles);
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles);
} 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> {
const context = path.join(workspace, core.getInput('context'));
const buildArgs = getInputList(core.getInput('build-args'));
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.'));
if (dockerFiles.length === 1) {
core.info(`Performing build from Dockerfile`);
}
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');
const content = getInputList('content');
const entrypoint = getInputList('entrypoint');
@ -52,23 +60,17 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
const languages = await recognizer.detectLanguages(workspace);
baseImage = await getSuggestedBaseImage(languages);
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 {
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);
if (container.succeeded === false) {
return Promise.reject(new Error(container.reason));
}
const containerId = container.output.replace('\n', '');
const copyResult = await cli.copy(containerId, content);
if (copyResult.succeeded === false) {
return Promise.reject(new Error(copyResult.reason));
}
await cli.copy(containerId, content);
const newImageConfig: BuildahConfigSettings = {
entrypoint: entrypoint,
@ -76,15 +78,8 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
workingdir: workingDir,
envs: envs
};
const configResult = await cli.config(containerId, newImageConfig);
if (configResult.succeeded === false) {
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));
}
await cli.config(containerId, newImageConfig);
await cli.commit(containerId, newImage, ['--squash']);
}
function getInputList(name: string): string[] {
@ -117,7 +112,6 @@ async function getSuggestedBaseImage(languages: 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 languageImageJSON = JSON.parse(rawData);
return languageImageJSON[language.name];

View file

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