mirror of
https://github.com/redhat-actions/buildah-build.git
synced 2025-06-08 01:49:03 +00:00
Add feature to output image with multiple tags (#21)
Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
parent
75dab40354
commit
88e0085544
13 changed files with 1971 additions and 154 deletions
104
src/buildah.ts
104
src/buildah.ts
|
@ -1,14 +1,7 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as exec from "@actions/exec";
|
||||
import * as path from "path";
|
||||
|
||||
interface Buildah {
|
||||
buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult>;
|
||||
from(baseImage: string): Promise<CommandResult>;
|
||||
copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
|
||||
config(container: string, setting: {}): Promise<CommandResult>;
|
||||
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
|
||||
}
|
||||
import CommandResult from "./types";
|
||||
|
||||
export interface BuildahConfigSettings {
|
||||
entrypoint?: string[];
|
||||
|
@ -17,70 +10,83 @@ export interface BuildahConfigSettings {
|
|||
workingdir?: string;
|
||||
}
|
||||
|
||||
export class BuildahCli implements Buildah {
|
||||
interface Buildah {
|
||||
buildUsingDocker(
|
||||
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
|
||||
): Promise<CommandResult>;
|
||||
from(baseImage: string): Promise<CommandResult>;
|
||||
copy(container: string, contentToCopy: string[]): Promise<CommandResult | undefined>;
|
||||
config(container: string, setting: BuildahConfigSettings): Promise<CommandResult>;
|
||||
commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult>;
|
||||
}
|
||||
|
||||
private executable: string;
|
||||
export class BuildahCli implements Buildah {
|
||||
private readonly executable: string;
|
||||
|
||||
constructor(executable: string) {
|
||||
this.executable = executable;
|
||||
}
|
||||
|
||||
private getImageFormatOption(useOCI: boolean): string[] {
|
||||
return [ '--format', useOCI ? 'oci' : 'docker' ];
|
||||
private static getImageFormatOption(useOCI: boolean): string[] {
|
||||
return [ "--format", useOCI ? "oci" : "docker" ];
|
||||
}
|
||||
|
||||
async buildUsingDocker(image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean): Promise<CommandResult> {
|
||||
const args: string[] = ['bud'];
|
||||
dockerFiles.forEach(file => {
|
||||
args.push('-f');
|
||||
async buildUsingDocker(
|
||||
image: string, context: string, dockerFiles: string[], buildArgs: string[], useOCI: boolean,
|
||||
): Promise<CommandResult> {
|
||||
const args: string[] = [ "bud" ];
|
||||
dockerFiles.forEach((file) => {
|
||||
args.push("-f");
|
||||
args.push(file);
|
||||
});
|
||||
buildArgs.forEach((buildArg) => {
|
||||
args.push('--build-arg');
|
||||
args.push("--build-arg");
|
||||
args.push(buildArg);
|
||||
});
|
||||
args.push(...this.getImageFormatOption(useOCI));
|
||||
args.push('-t');
|
||||
args.push(...BuildahCli.getImageFormatOption(useOCI));
|
||||
args.push("-t");
|
||||
args.push(image);
|
||||
args.push(context);
|
||||
return this.execute(args);
|
||||
}
|
||||
|
||||
async from(baseImage: string): Promise<CommandResult> {
|
||||
return this.execute(['from', baseImage]);
|
||||
return this.execute([ "from", baseImage ]);
|
||||
}
|
||||
|
||||
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult | undefined> {
|
||||
async copy(container: string, contentToCopy: string[], contentPath?: string): Promise<CommandResult | undefined> {
|
||||
if (contentToCopy.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
core.debug('copy');
|
||||
core.debug("copy");
|
||||
core.debug(container);
|
||||
for (const content of contentToCopy) {
|
||||
const args: string[] = ["copy", container, content];
|
||||
if (path) {
|
||||
args.push(path);
|
||||
const args: string[] = [ "copy", container, content ];
|
||||
if (contentPath) {
|
||||
args.push(contentPath);
|
||||
}
|
||||
return this.execute(args);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
|
||||
core.debug('config');
|
||||
core.debug("config");
|
||||
core.debug(container);
|
||||
const args: string[] = ['config'];
|
||||
const args: string[] = [ "config" ];
|
||||
if (settings.entrypoint) {
|
||||
args.push('--entrypoint');
|
||||
args.push(this.convertArrayToStringArg(settings.entrypoint));
|
||||
args.push("--entrypoint");
|
||||
args.push(BuildahCli.convertArrayToStringArg(settings.entrypoint));
|
||||
}
|
||||
if (settings.port) {
|
||||
args.push('--port');
|
||||
args.push("--port");
|
||||
args.push(settings.port);
|
||||
}
|
||||
if (settings.envs) {
|
||||
settings.envs.forEach((env) => {
|
||||
args.push('--env');
|
||||
args.push("--env");
|
||||
args.push(env);
|
||||
});
|
||||
}
|
||||
|
@ -89,23 +95,34 @@ export class BuildahCli implements Buildah {
|
|||
}
|
||||
|
||||
async commit(container: string, newImageName: string, useOCI: boolean): Promise<CommandResult> {
|
||||
core.debug('commit');
|
||||
core.debug("commit");
|
||||
core.debug(container);
|
||||
core.debug(newImageName);
|
||||
const args: string[] = [ 'commit', ...this.getImageFormatOption(useOCI), '--squash', container, newImageName ];
|
||||
const args: string[] = [
|
||||
"commit", ...BuildahCli.getImageFormatOption(useOCI),
|
||||
"--squash", container, newImageName,
|
||||
];
|
||||
return this.execute(args);
|
||||
}
|
||||
|
||||
private convertArrayToStringArg(args: string[]): string {
|
||||
let arrayAsString = '[';
|
||||
args.forEach(arg => {
|
||||
async tag(imageName: string, tags: string[]): Promise<CommandResult> {
|
||||
const args: string[] = [ "tag" ];
|
||||
for (const tag of tags) {
|
||||
args.push(`${imageName}:${tag}`);
|
||||
}
|
||||
core.info(`Tagging the built image with tags ${tags.toString()}`);
|
||||
return this.execute(args);
|
||||
}
|
||||
|
||||
private static convertArrayToStringArg(args: string[]): string {
|
||||
let arrayAsString = "[";
|
||||
args.forEach((arg) => {
|
||||
arrayAsString += `"${arg}",`;
|
||||
});
|
||||
return `${arrayAsString.slice(0, -1)}]`;
|
||||
}
|
||||
|
||||
private async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
|
||||
|
||||
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)
|
||||
|
||||
let stdout = "";
|
||||
|
@ -115,18 +132,19 @@ export class BuildahCli implements Buildah {
|
|||
finalExecOptions.ignoreReturnCode = true; // the return code is processed below
|
||||
|
||||
finalExecOptions.listeners = {
|
||||
stdline: (line) => {
|
||||
stdline: (line): void => {
|
||||
stdout += line + "\n";
|
||||
},
|
||||
errline: (line) => {
|
||||
stderr += line + "\n"
|
||||
errline: (line):void => {
|
||||
stderr += line + "\n";
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
const exitCode = await exec.exec(this.executable, args, finalExecOptions);
|
||||
|
||||
if (execOptions.ignoreReturnCode !== true && exitCode !== 0) {
|
||||
// Throwing the stderr as part of the Error makes the stderr show up in the action outline, which saves some clicking when debugging.
|
||||
// Throwing the stderr as part of the Error makes the stderr
|
||||
// show up in the action outline, which saves some clicking when debugging.
|
||||
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
|
||||
if (stderr) {
|
||||
error += `\n${stderr}`;
|
||||
|
@ -135,7 +153,7 @@ export class BuildahCli implements Buildah {
|
|||
}
|
||||
|
||||
return {
|
||||
exitCode, output: stdout, error: stderr
|
||||
exitCode, output: stdout, error: stderr,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
126
src/index.ts
126
src/index.ts
|
@ -1,37 +1,42 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as io from '@actions/io';
|
||||
import { BuildahCli, BuildahConfigSettings } from './buildah';
|
||||
import * as recognizer from 'language-recognizer';
|
||||
import {promises as fs} from 'fs';
|
||||
import * as path from 'path';
|
||||
import { Language } from 'language-recognizer/lib/types';
|
||||
import * as core from "@actions/core";
|
||||
import * as io from "@actions/io";
|
||||
import * as path from "path";
|
||||
import { BuildahCli, BuildahConfigSettings } from "./buildah";
|
||||
|
||||
export async function run(): Promise<void> {
|
||||
|
||||
if (process.env.RUNNER_OS !== 'Linux') {
|
||||
throw new Error('buildah, and therefore this action, only works on Linux. Please use a Linux runner.');
|
||||
if (process.env.RUNNER_OS !== "Linux") {
|
||||
throw new Error("buildah, and therefore this action, only works on Linux. Please use a Linux runner.");
|
||||
}
|
||||
|
||||
// get buildah cli
|
||||
const buildahPath = await io.which('buildah', true);
|
||||
const buildahPath = await io.which("buildah", true);
|
||||
const cli: BuildahCli = new BuildahCli(buildahPath);
|
||||
|
||||
const workspace = process.env['GITHUB_WORKSPACE'];
|
||||
let dockerFiles = getInputList('dockerfiles');
|
||||
const newImage = `${core.getInput('image', { required: true })}:${core.getInput('tag', { required: true })}`;
|
||||
|
||||
const useOCI = core.getInput("oci") == "true";
|
||||
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
|
||||
const dockerFiles = getInputList("dockerfiles");
|
||||
const image = core.getInput("image", { required: true });
|
||||
const tags = core.getInput("tags") || "latest";
|
||||
const tagsList: string[] = tags.split(" ");
|
||||
const newImage = `${image}:${tagsList[0]}`;
|
||||
const useOCI = core.getInput("oci") === "true";
|
||||
|
||||
if (dockerFiles.length !== 0) {
|
||||
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI);
|
||||
} else {
|
||||
await doBuildFromScratch(cli, newImage, workspace, useOCI);
|
||||
}
|
||||
else {
|
||||
await doBuildFromScratch(cli, newImage, useOCI);
|
||||
}
|
||||
|
||||
core.setOutput("image", newImage);
|
||||
if (tagsList.length > 1) {
|
||||
await cli.tag(image, tagsList);
|
||||
}
|
||||
core.setOutput("image", image);
|
||||
core.setOutput("tags", tags);
|
||||
}
|
||||
|
||||
async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean): Promise<void> {
|
||||
async function doBuildUsingDockerFiles(
|
||||
cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean,
|
||||
): Promise<void> {
|
||||
if (dockerFiles.length === 1) {
|
||||
core.info(`Performing build from Dockerfile`);
|
||||
}
|
||||
|
@ -39,46 +44,34 @@ async function doBuildUsingDockerFiles(cli: BuildahCli, newImage: string, worksp
|
|||
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, useOCI);
|
||||
const context = path.join(workspace, core.getInput("context"));
|
||||
const buildArgs = getInputList("build-args");
|
||||
const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
|
||||
await cli.buildUsingDocker(newImage, context, dockerFileAbsPaths, buildArgs, useOCI);
|
||||
}
|
||||
|
||||
async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace: string, useOCI: boolean): Promise<void> {
|
||||
core.info(`Performing build from scratch`)
|
||||
async function doBuildFromScratch(
|
||||
cli: BuildahCli, newImage: string, useOCI: boolean,
|
||||
): Promise<void> {
|
||||
core.info(`Performing build from scratch`);
|
||||
|
||||
let baseImage = core.getInput('base-image');
|
||||
const content = getInputList('content');
|
||||
const entrypoint = getInputList('entrypoint');
|
||||
const port = core.getInput('port');
|
||||
const workingDir = core.getInput('workdir');
|
||||
const envs = getInputList('envs');
|
||||
|
||||
// if base-image is not specified by the user we need to pick one automatically
|
||||
if (!baseImage) {
|
||||
if (workspace) {
|
||||
// check language/framework used and pick base-image automatically
|
||||
const languages = await recognizer.detectLanguages(workspace);
|
||||
baseImage = await getSuggestedBaseImage(languages);
|
||||
if (!baseImage) {
|
||||
throw new Error('No base image found to create a new container');
|
||||
}
|
||||
} else {
|
||||
throw new Error('No base image found to create a new container');
|
||||
}
|
||||
}
|
||||
const baseImage = core.getInput("base-image", { required: true });
|
||||
const content = getInputList("content");
|
||||
const entrypoint = getInputList("entrypoint");
|
||||
const port = core.getInput("port");
|
||||
const workingDir = core.getInput("workdir");
|
||||
const envs = getInputList("envs");
|
||||
|
||||
const container = await cli.from(baseImage);
|
||||
const containerId = container.output.replace('\n', '');
|
||||
const containerId = container.output.replace("\n", "");
|
||||
|
||||
await cli.copy(containerId, content);
|
||||
|
||||
const newImageConfig: BuildahConfigSettings = {
|
||||
entrypoint: entrypoint,
|
||||
port: port,
|
||||
entrypoint,
|
||||
port,
|
||||
workingdir: workingDir,
|
||||
envs: envs
|
||||
envs,
|
||||
};
|
||||
await cli.config(containerId, newImageConfig);
|
||||
await cli.commit(containerId, newImage, useOCI);
|
||||
|
@ -87,36 +80,15 @@ async function doBuildFromScratch(cli: BuildahCli, newImage: string, workspace:
|
|||
function getInputList(name: string): string[] {
|
||||
const items = core.getInput(name);
|
||||
if (!items) {
|
||||
return [];
|
||||
return [];
|
||||
}
|
||||
return items
|
||||
.split(/\r?\n/)
|
||||
.filter(x => x)
|
||||
.reduce<string[]>(
|
||||
(acc, line) => acc.concat(line).map(pat => pat.trim()),
|
||||
[]
|
||||
.split(/\r?\n/)
|
||||
.filter((x) => x)
|
||||
.reduce<string[]>(
|
||||
(acc, line) => acc.concat(line).map((pat) => pat.trim()),
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
async function getSuggestedBaseImage(languages: Language[]): Promise<string> {
|
||||
if (!languages || languages.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
for (const language of languages) {
|
||||
const baseImage = await getBaseImageByLanguage(language);
|
||||
if (baseImage) {
|
||||
return baseImage;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function getBaseImageByLanguage(language: Language): Promise<string> {
|
||||
const rawData = await fs.readFile(path.join(__dirname, '..', 'language-image.json'), 'utf-8');
|
||||
const languageImageJSON = JSON.parse(rawData);
|
||||
return languageImageJSON[language.name];
|
||||
}
|
||||
|
||||
run().catch(core.setFailed);
|
||||
|
|
|
@ -3,3 +3,5 @@ type CommandResult = {
|
|||
output: string
|
||||
error: string
|
||||
};
|
||||
|
||||
export default CommandResult;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue