mirror of
https://github.com/redhat-actions/buildah-build.git
synced 2025-06-08 01:49:03 +00:00
Replace input dockerfiles with containerfiles (#69)
* Replace input dockerfiles with containerfiles Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
parent
ab006ef445
commit
f9dfea0413
10 changed files with 106 additions and 76 deletions
|
@ -19,7 +19,7 @@ export interface BuildahConfigSettings {
|
|||
|
||||
interface Buildah {
|
||||
buildUsingDocker(
|
||||
image: string, context: string, dockerFiles: string[], buildArgs: string[],
|
||||
image: string, context: string, containerFiles: string[], buildArgs: string[],
|
||||
useOCI: boolean, arch: string, layers: string, extraArgs: string[]
|
||||
): Promise<CommandResult>;
|
||||
from(baseImage: string): Promise<CommandResult>;
|
||||
|
@ -62,7 +62,7 @@ export class BuildahCli implements Buildah {
|
|||
}
|
||||
|
||||
async buildUsingDocker(
|
||||
image: string, context: string, dockerFiles: string[], buildArgs: string[],
|
||||
image: string, context: string, containerFiles: string[], buildArgs: string[],
|
||||
useOCI: boolean, arch: string, layers: string, extraArgs: string[]
|
||||
): Promise<CommandResult> {
|
||||
const args: string[] = [ "bud" ];
|
||||
|
@ -70,7 +70,7 @@ export class BuildahCli implements Buildah {
|
|||
args.push("--arch");
|
||||
args.push(arch);
|
||||
}
|
||||
dockerFiles.forEach((file) => {
|
||||
containerFiles.forEach((file) => {
|
||||
args.push("-f");
|
||||
args.push(file);
|
||||
});
|
||||
|
|
|
@ -24,6 +24,12 @@ export enum Inputs {
|
|||
* Default: None.
|
||||
*/
|
||||
BUILD_ARGS = "build-args",
|
||||
/**
|
||||
* List of Containerfile paths (eg: ./Containerfile)
|
||||
* Required: false
|
||||
* Default: None.
|
||||
*/
|
||||
CONTAINERFILES = "containerfiles",
|
||||
/**
|
||||
* List of files/directories to copy inside the base image
|
||||
* Required: false
|
||||
|
@ -37,7 +43,7 @@ export enum Inputs {
|
|||
*/
|
||||
CONTEXT = "context",
|
||||
/**
|
||||
* List of Dockerfile paths (eg: ./Dockerfile)
|
||||
* Alias for "containerfiles". "containerfiles" takes precedence if both are set.
|
||||
* Required: false
|
||||
* Default: None.
|
||||
*/
|
||||
|
|
53
src/index.ts
53
src/index.ts
|
@ -8,7 +8,9 @@ import * as io from "@actions/io";
|
|||
import * as path from "path";
|
||||
import { Inputs, Outputs } from "./generated/inputs-outputs";
|
||||
import { BuildahCli, BuildahConfigSettings } from "./buildah";
|
||||
import { splitByNewline } from "./utils";
|
||||
import {
|
||||
getArch, getContainerfiles, getInputList, splitByNewline,
|
||||
} from "./utils";
|
||||
|
||||
export async function run(): Promise<void> {
|
||||
if (process.env.RUNNER_OS !== "Linux") {
|
||||
|
@ -27,7 +29,7 @@ export async function run(): Promise<void> {
|
|||
|
||||
const DEFAULT_TAG = "latest";
|
||||
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
|
||||
const dockerFiles = getInputList(Inputs.DOCKERFILES);
|
||||
const containerFiles = getContainerfiles();
|
||||
const image = core.getInput(Inputs.IMAGE, { required: true });
|
||||
const tags = core.getInput(Inputs.TAGS);
|
||||
const tagsList: string[] = tags.split(" ");
|
||||
|
@ -42,8 +44,8 @@ export async function run(): Promise<void> {
|
|||
|
||||
const arch = getArch();
|
||||
|
||||
if (dockerFiles.length !== 0) {
|
||||
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI, arch);
|
||||
if (containerFiles.length !== 0) {
|
||||
await doBuildUsingContainerFiles(cli, newImage, workspace, containerFiles, useOCI, arch);
|
||||
}
|
||||
else {
|
||||
await doBuildFromScratch(cli, newImage, useOCI, arch);
|
||||
|
@ -57,19 +59,19 @@ export async function run(): Promise<void> {
|
|||
core.setOutput(Outputs.IMAGE_WITH_TAG, `${image}:${tagsList[0]}`);
|
||||
}
|
||||
|
||||
async function doBuildUsingDockerFiles(
|
||||
cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean, arch: string
|
||||
async function doBuildUsingContainerFiles(
|
||||
cli: BuildahCli, newImage: string, workspace: string, containerFiles: string[], useOCI: boolean, arch: string
|
||||
): Promise<void> {
|
||||
if (dockerFiles.length === 1) {
|
||||
core.info(`Performing build from Dockerfile`);
|
||||
if (containerFiles.length === 1) {
|
||||
core.info(`Performing build from Containerfile`);
|
||||
}
|
||||
else {
|
||||
core.info(`Performing build from ${dockerFiles.length} Dockerfiles`);
|
||||
core.info(`Performing build from ${containerFiles.length} Containerfiles`);
|
||||
}
|
||||
|
||||
const context = path.join(workspace, core.getInput(Inputs.CONTEXT));
|
||||
const buildArgs = getInputList(Inputs.BUILD_ARGS);
|
||||
const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
|
||||
const containerFileAbsPaths = containerFiles.map((file) => path.join(workspace, file));
|
||||
const layers = core.getInput(Inputs.LAYERS);
|
||||
|
||||
const inputExtraArgsStr = core.getInput(Inputs.EXTRA_ARGS);
|
||||
|
@ -81,7 +83,7 @@ async function doBuildUsingDockerFiles(
|
|||
buildahBudExtraArgs = lines.flatMap((line) => line.split(" ")).map((arg) => arg.trim());
|
||||
}
|
||||
await cli.buildUsingDocker(
|
||||
newImage, context, dockerFileAbsPaths, buildArgs, useOCI, arch, layers, buildahBudExtraArgs
|
||||
newImage, context, containerFileAbsPaths, buildArgs, useOCI, arch, layers, buildahBudExtraArgs
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -112,33 +114,4 @@ async function doBuildFromScratch(
|
|||
await cli.commit(containerId, newImage, useOCI);
|
||||
}
|
||||
|
||||
function getInputList(name: string): string[] {
|
||||
const items = core.getInput(name);
|
||||
if (!items) {
|
||||
return [];
|
||||
}
|
||||
return items
|
||||
.split(/\r?\n/)
|
||||
.filter((x) => x)
|
||||
.reduce<string[]>(
|
||||
(acc, line) => acc.concat(line).map((pat) => pat.trim()),
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
function getArch(): string {
|
||||
// 'arch' should be used over 'archs', see https://github.com/redhat-actions/buildah-build/issues/60
|
||||
const archs = core.getInput(Inputs.ARCHS);
|
||||
const arch = core.getInput(Inputs.ARCH);
|
||||
|
||||
if (arch && archs) {
|
||||
core.warning(
|
||||
`Please use only one input of "${Inputs.ARCH}" and "${Inputs.ARCHS}". "${Inputs.ARCH}" takes precedence, `
|
||||
+ `so --arch argument will be "${arch}".`
|
||||
);
|
||||
}
|
||||
|
||||
return arch || archs;
|
||||
}
|
||||
|
||||
run().catch(core.setFailed);
|
||||
|
|
47
src/utils.ts
47
src/utils.ts
|
@ -9,6 +9,7 @@ import * as core from "@actions/core";
|
|||
import * as path from "path";
|
||||
import * as io from "@actions/io";
|
||||
import * as os from "os";
|
||||
import { Inputs } from "./generated/inputs-outputs";
|
||||
|
||||
async function findStorageDriver(filePaths: string[]): Promise<string> {
|
||||
let storageDriver = "";
|
||||
|
@ -63,3 +64,49 @@ export async function findFuseOverlayfsPath(): Promise<string | undefined> {
|
|||
export function splitByNewline(s: string): string[] {
|
||||
return s.split(/\r?\n/);
|
||||
}
|
||||
|
||||
export function getArch(): string {
|
||||
// 'arch' should be used over 'archs', see https://github.com/redhat-actions/buildah-build/issues/60
|
||||
const archs = core.getInput(Inputs.ARCHS);
|
||||
const arch = core.getInput(Inputs.ARCH);
|
||||
|
||||
if (arch && archs) {
|
||||
core.warning(
|
||||
`Both "${Inputs.ARCH}" and "${Inputs.ARCHS}" inputs are set. `
|
||||
+ `Please use only one of these two inputs, as they are aliases of one another. `
|
||||
+ `"${Inputs.ARCH}" takes precedence.`
|
||||
);
|
||||
}
|
||||
|
||||
return arch || archs;
|
||||
}
|
||||
|
||||
export function getContainerfiles(): string[] {
|
||||
// 'containerfile' should be used over 'dockerfile',
|
||||
// see https://github.com/redhat-actions/buildah-build/issues/57
|
||||
const containerfiles = getInputList(Inputs.CONTAINERFILES);
|
||||
const dockerfiles = getInputList(Inputs.DOCKERFILES);
|
||||
|
||||
if (containerfiles.length !== 0 && dockerfiles.length !== 0) {
|
||||
core.warning(
|
||||
`Both "${Inputs.CONTAINERFILES}" and "${Inputs.DOCKERFILES}" inputs are set. `
|
||||
+ `Please use only one of these two inputs, as they are aliases of one another. `
|
||||
+ `"${Inputs.CONTAINERFILES}" takes precedence.`
|
||||
);
|
||||
}
|
||||
|
||||
return containerfiles.length !== 0 ? containerfiles : dockerfiles;
|
||||
}
|
||||
|
||||
export function getInputList(name: string): string[] {
|
||||
const items = core.getInput(name);
|
||||
if (!items) {
|
||||
return [];
|
||||
}
|
||||
const splitItems = splitByNewline(items);
|
||||
return splitItems
|
||||
.reduce<string[]>(
|
||||
(acc, line) => acc.concat(line).map((item) => item.trim()),
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue