2021-02-01 17:54:50 +00:00
|
|
|
import * as core from "@actions/core";
|
|
|
|
import * as io from "@actions/io";
|
|
|
|
import * as path from "path";
|
2021-02-08 16:24:18 +00:00
|
|
|
import { Inputs, Outputs } from "./generated/inputs-outputs";
|
2021-02-01 17:54:50 +00:00
|
|
|
import { BuildahCli, BuildahConfigSettings } from "./buildah";
|
2020-11-06 17:42:45 +00:00
|
|
|
|
|
|
|
export async function run(): Promise<void> {
|
2021-02-01 17:54:50 +00:00
|
|
|
if (process.env.RUNNER_OS !== "Linux") {
|
|
|
|
throw new Error("buildah, and therefore this action, only works on Linux. Please use a Linux runner.");
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
2020-11-19 08:19:57 +00:00
|
|
|
|
2020-11-06 17:42:45 +00:00
|
|
|
// get buildah cli
|
2021-02-01 17:54:50 +00:00
|
|
|
const buildahPath = await io.which("buildah", true);
|
2020-11-19 08:19:57 +00:00
|
|
|
const cli: BuildahCli = new BuildahCli(buildahPath);
|
|
|
|
|
2021-02-01 17:54:50 +00:00
|
|
|
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
|
2021-02-08 16:24:18 +00:00
|
|
|
const dockerFiles = getInputList(Inputs.DOCKERFILES);
|
|
|
|
const image = core.getInput(Inputs.IMAGE, { required: true });
|
|
|
|
const tags = core.getInput(Inputs.TAGS) || "latest";
|
2021-02-01 17:54:50 +00:00
|
|
|
const tagsList: string[] = tags.split(" ");
|
|
|
|
const newImage = `${image}:${tagsList[0]}`;
|
2021-02-08 16:24:18 +00:00
|
|
|
const useOCI = core.getInput(Inputs.OCI) === "true";
|
2021-02-17 18:27:20 +00:00
|
|
|
let archs: string | undefined = core.getInput(Inputs.ARCHS);
|
|
|
|
// remove white spaces (if any) in archs input
|
|
|
|
archs = archs.replace(/\s+/g, "");
|
2020-11-26 18:00:17 +00:00
|
|
|
|
2020-11-19 08:19:57 +00:00
|
|
|
if (dockerFiles.length !== 0) {
|
2021-02-17 18:27:20 +00:00
|
|
|
await doBuildUsingDockerFiles(cli, newImage, workspace, dockerFiles, useOCI, archs);
|
2021-02-01 17:54:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-02-17 18:27:20 +00:00
|
|
|
await doBuildFromScratch(cli, newImage, useOCI, archs);
|
2020-11-19 18:46:44 +00:00
|
|
|
}
|
2020-11-23 22:08:21 +00:00
|
|
|
|
2021-02-01 17:54:50 +00:00
|
|
|
if (tagsList.length > 1) {
|
|
|
|
await cli.tag(image, tagsList);
|
|
|
|
}
|
2021-02-08 16:24:18 +00:00
|
|
|
core.setOutput(Outputs.IMAGE, image);
|
|
|
|
core.setOutput(Outputs.TAGS, tags);
|
2020-11-19 08:19:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:54:50 +00:00
|
|
|
async function doBuildUsingDockerFiles(
|
2021-02-17 18:27:20 +00:00
|
|
|
cli: BuildahCli, newImage: string, workspace: string, dockerFiles: string[], useOCI: boolean, archs: string
|
2021-02-01 17:54:50 +00:00
|
|
|
): Promise<void> {
|
2020-11-23 22:08:21 +00:00
|
|
|
if (dockerFiles.length === 1) {
|
|
|
|
core.info(`Performing build from Dockerfile`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
core.info(`Performing build from ${dockerFiles.length} Dockerfiles`);
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:24:18 +00:00
|
|
|
const context = path.join(workspace, core.getInput(Inputs.CONTEXT));
|
|
|
|
const buildArgs = getInputList(Inputs.BUILD_ARGS);
|
2021-02-01 17:54:50 +00:00
|
|
|
const dockerFileAbsPaths = dockerFiles.map((file) => path.join(workspace, file));
|
2021-02-17 18:27:20 +00:00
|
|
|
await cli.buildUsingDocker(newImage, context, dockerFileAbsPaths, buildArgs, useOCI, archs);
|
2020-11-19 08:19:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:54:50 +00:00
|
|
|
async function doBuildFromScratch(
|
2021-02-17 18:27:20 +00:00
|
|
|
cli: BuildahCli, newImage: string, useOCI: boolean, archs: string
|
2021-02-01 17:54:50 +00:00
|
|
|
): Promise<void> {
|
|
|
|
core.info(`Performing build from scratch`);
|
2020-11-06 17:42:45 +00:00
|
|
|
|
2021-02-08 16:24:18 +00:00
|
|
|
const baseImage = core.getInput(Inputs.BASE_IMAGE, { required: true });
|
|
|
|
const content = getInputList(Inputs.CONTENT);
|
|
|
|
const entrypoint = getInputList(Inputs.ENTRYPOINT);
|
|
|
|
const port = core.getInput(Inputs.PORT);
|
|
|
|
const workingDir = core.getInput(Inputs.WORKDIR);
|
|
|
|
const envs = getInputList(Inputs.ENVS);
|
2020-11-19 18:46:44 +00:00
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
const container = await cli.from(baseImage);
|
2021-02-01 17:54:50 +00:00
|
|
|
const containerId = container.output.replace("\n", "");
|
2020-11-06 17:42:45 +00:00
|
|
|
|
2020-11-23 22:08:21 +00:00
|
|
|
await cli.copy(containerId, content);
|
2020-11-06 17:42:45 +00:00
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
const newImageConfig: BuildahConfigSettings = {
|
2021-02-01 17:54:50 +00:00
|
|
|
entrypoint,
|
|
|
|
port,
|
2020-11-13 11:38:29 +00:00
|
|
|
workingdir: workingDir,
|
2021-02-01 17:54:50 +00:00
|
|
|
envs,
|
2021-02-17 18:27:20 +00:00
|
|
|
archs,
|
2020-11-13 11:38:29 +00:00
|
|
|
};
|
2020-11-23 22:08:21 +00:00
|
|
|
await cli.config(containerId, newImageConfig);
|
2020-11-26 18:00:17 +00:00
|
|
|
await cli.commit(containerId, newImage, useOCI);
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 11:38:29 +00:00
|
|
|
function getInputList(name: string): string[] {
|
2020-11-06 17:42:45 +00:00
|
|
|
const items = core.getInput(name);
|
2020-11-13 11:38:29 +00:00
|
|
|
if (!items) {
|
2021-02-01 17:54:50 +00:00
|
|
|
return [];
|
2020-11-06 17:42:45 +00:00
|
|
|
}
|
|
|
|
return items
|
2021-02-01 17:54:50 +00:00
|
|
|
.split(/\r?\n/)
|
|
|
|
.filter((x) => x)
|
|
|
|
.reduce<string[]>(
|
|
|
|
(acc, line) => acc.concat(line).map((pat) => pat.trim()),
|
|
|
|
[],
|
2020-11-13 11:38:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-19 18:46:44 +00:00
|
|
|
run().catch(core.setFailed);
|