Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Etchells <tetchell@redhat.com>
  • Loading branch information
tetchel committed Nov 23, 2020
1 parent 54d567b commit b3924cf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 64 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

41 changes: 20 additions & 21 deletions src/buildah.ts
Original file line number Diff line number Diff line change
@@ -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>;
Expand Down Expand Up @@ -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> {
Expand All @@ -82,15 +79,15 @@ 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> {
core.debug('commit');
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 {
Expand All @@ -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
};
}
}
40 changes: 13 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,23 +21,23 @@ 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);
}
}

async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[]): Promise<void> {
core.info(`Performing Docker build`);
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.'));
}
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');
Expand All @@ -52,39 +52,26 @@ 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,
port: port,
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[] {
Expand Down Expand Up @@ -117,7 +104,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];
Expand Down
16 changes: 5 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
};

0 comments on commit b3924cf

Please sign in to comment.