mirror of
https://github.com/redhat-actions/buildah-build.git
synced 2025-04-20 09:01:23 +00:00
Fix buildah issue of using overlay
as storage driver (#51)
Work around https://github.com/redhat-actions/buildah-build/issues/45 Signed-off-by: divyansh42 <diagrawa@redhat.com>
This commit is contained in:
parent
65f18d484c
commit
6dbeb7e1f6
9 changed files with 142 additions and 29 deletions
6
.github/workflows/dockerfile_build.yml
vendored
6
.github/workflows/dockerfile_build.yml
vendored
|
@ -31,12 +31,6 @@ jobs:
|
|||
RUN echo "hello world"
|
||||
EOF
|
||||
|
||||
# Workaround to fix https://github.com/containers/buildah/issues/3120
|
||||
- run: |
|
||||
sudo apt-get install fuse-overlayfs
|
||||
mkdir -vp ~/.config/containers
|
||||
printf "[storage.options]\nmount_program=\"/usr/bin/fuse-overlayfs\"" > ~/.config/containers/storage.conf
|
||||
|
||||
# Build image using Buildah action
|
||||
- name: Build Image
|
||||
id: build_image
|
||||
|
|
6
.github/workflows/scratch_build.yml
vendored
6
.github/workflows/scratch_build.yml
vendored
|
@ -68,12 +68,6 @@ jobs:
|
|||
path: ${{ env.MVN_REPO_DIR }}
|
||||
key: ${{ env.MVN_HASH }}
|
||||
|
||||
# Workaround to fix https://github.com/containers/buildah/issues/3120
|
||||
- run: |
|
||||
sudo apt-get install fuse-overlayfs
|
||||
mkdir -vp ~/.config/containers
|
||||
printf "[storage.options]\nmount_program=\"/usr/bin/fuse-overlayfs\"" > ~/.config/containers/storage.conf
|
||||
|
||||
# Build image using Buildah action
|
||||
- name: Build Image
|
||||
id: build_image
|
||||
|
|
2
dist/index.js
vendored
2
dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
11
package-lock.json
generated
11
package-lock.json
generated
|
@ -166,6 +166,12 @@
|
|||
"integrity": "sha512-t+i85G2LLauDOlH3MQqxVblCKMt5yyRHZsO7NoVKE8T1W1aIosH1bs5xH2RqwXaWw2Si+r66W/tuHRQzKbR51w==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/ini": {
|
||||
"version": "1.3.30",
|
||||
"resolved": "https://registry.npmjs.org/@types/ini/-/ini-1.3.30.tgz",
|
||||
"integrity": "sha512-2+iF8zPSbpU83UKE+PNd4r/MhwNAdyGpk3H+VMgEH3EhjFZq1kouLgRoZrmIcmoGX97xFvqdS44DkICR5Nz3tQ==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/json-schema": {
|
||||
"version": "7.0.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.7.tgz",
|
||||
|
@ -1146,6 +1152,11 @@
|
|||
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
|
||||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
||||
},
|
||||
"ini": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ini/-/ini-2.0.0.tgz",
|
||||
"integrity": "sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA=="
|
||||
},
|
||||
"is-arrayish": {
|
||||
"version": "0.2.1",
|
||||
"resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz",
|
||||
|
|
|
@ -23,12 +23,14 @@
|
|||
"@actions/core": "^1.2.6",
|
||||
"@actions/exec": "^1.0.4",
|
||||
"@actions/io": "^1.0.2",
|
||||
"ini": "^2.0.0",
|
||||
"language-recognizer": "0.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@redhat-actions/action-io-generator": "^1.5.0",
|
||||
"@redhat-actions/eslint-config": "^1.2.11",
|
||||
"@redhat-actions/tsconfig": "^1.1.1",
|
||||
"@types/ini": "^1.3.30",
|
||||
"@types/node": "^12",
|
||||
"@typescript-eslint/eslint-plugin": "^4.14.1",
|
||||
"@typescript-eslint/parser": "^4.14.1",
|
||||
|
|
|
@ -2,6 +2,7 @@ import * as core from "@actions/core";
|
|||
import * as exec from "@actions/exec";
|
||||
import * as path from "path";
|
||||
import CommandResult from "./types";
|
||||
import { isStorageDriverOverlay, findFuseOverlayfsPath } from "./utils";
|
||||
|
||||
export interface BuildahConfigSettings {
|
||||
entrypoint?: string[];
|
||||
|
@ -25,10 +26,32 @@ interface Buildah {
|
|||
export class BuildahCli implements Buildah {
|
||||
private readonly executable: string;
|
||||
|
||||
public storageOptsEnv = "";
|
||||
|
||||
constructor(executable: string) {
|
||||
this.executable = executable;
|
||||
}
|
||||
|
||||
// Checks for storage driver if found "overlay",
|
||||
// then checks if "fuse-overlayfs" is installed.
|
||||
// If yes, add mount program to use "fuse-overlayfs"
|
||||
async setStorageOptsEnv(): Promise<void> {
|
||||
if (await isStorageDriverOverlay()) {
|
||||
const fuseOverlayfsPath = await findFuseOverlayfsPath();
|
||||
if (fuseOverlayfsPath) {
|
||||
core.info(`Overriding storage mount_program with "fuse-overlayfs" in environment`);
|
||||
this.storageOptsEnv = `overlay.mount_program=${fuseOverlayfsPath}`;
|
||||
}
|
||||
else {
|
||||
core.warning(`"fuse-overlayfs" is not found. Install it before running this action. `
|
||||
+ `For more detail see https://github.com/redhat-actions/buildah-build/issues/45`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
core.info("Storage driver is not 'overlay', so not overriding storage configuration");
|
||||
}
|
||||
}
|
||||
|
||||
private static getImageFormatOption(useOCI: boolean): string[] {
|
||||
return [ "--format", useOCI ? "oci" : "docker" ];
|
||||
}
|
||||
|
@ -140,7 +163,10 @@ export class BuildahCli implements Buildah {
|
|||
return `${arrayAsString.slice(0, -1)}]`;
|
||||
}
|
||||
|
||||
async execute(args: string[], execOptions: exec.ExecOptions = {}): Promise<CommandResult> {
|
||||
async execute(
|
||||
args: string[],
|
||||
execOptions: exec.ExecOptions & { group?: boolean } = {},
|
||||
): Promise<CommandResult> {
|
||||
// ghCore.info(`${EXECUTABLE} ${args.join(" ")}`)
|
||||
|
||||
let stdout = "";
|
||||
|
@ -158,20 +184,47 @@ export class BuildahCli implements Buildah {
|
|||
},
|
||||
};
|
||||
|
||||
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.
|
||||
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
|
||||
if (stderr) {
|
||||
error += `\n${stderr}`;
|
||||
}
|
||||
throw new Error(error);
|
||||
if (execOptions.group) {
|
||||
const groupName = [ this.executable, ...args ].join(" ");
|
||||
core.startGroup(groupName);
|
||||
}
|
||||
|
||||
return {
|
||||
exitCode, output: stdout, error: stderr,
|
||||
};
|
||||
// To solve https://github.com/redhat-actions/buildah-build/issues/45
|
||||
const execEnv: { [key: string] : string } = {};
|
||||
Object.entries(process.env).forEach(([ key, value ]) => {
|
||||
if (value != null) {
|
||||
execEnv[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
if (this.storageOptsEnv) {
|
||||
execEnv.STORAGE_OPTS = this.storageOptsEnv;
|
||||
}
|
||||
|
||||
finalExecOptions.env = execEnv;
|
||||
|
||||
try {
|
||||
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.
|
||||
let error = `${path.basename(this.executable)} exited with code ${exitCode}`;
|
||||
if (stderr) {
|
||||
error += `\n${stderr}`;
|
||||
}
|
||||
throw new Error(error);
|
||||
}
|
||||
|
||||
return {
|
||||
exitCode, output: stdout, error: stderr,
|
||||
};
|
||||
}
|
||||
|
||||
finally {
|
||||
if (execOptions.group) {
|
||||
core.endGroup();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,10 @@ export async function run(): Promise<void> {
|
|||
const cli: BuildahCli = new BuildahCli(buildahPath);
|
||||
|
||||
// print buildah version
|
||||
await cli.execute([ "version" ]);
|
||||
await cli.execute([ "version" ], { group: true });
|
||||
|
||||
// Check if fuse-overlayfs exists and find the storage driver
|
||||
await cli.setStorageOptsEnv();
|
||||
|
||||
const DEFAULT_TAG = "latest";
|
||||
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
|
||||
|
|
56
src/utils.ts
Normal file
56
src/utils.ts
Normal file
|
@ -0,0 +1,56 @@
|
|||
import * as ini from "ini";
|
||||
import { promises as fs } from "fs";
|
||||
import * as core from "@actions/core";
|
||||
import * as path from "path";
|
||||
import * as io from "@actions/io";
|
||||
import * as os from "os";
|
||||
|
||||
async function findStorageDriver(filePaths: string[]): Promise<string> {
|
||||
let storageDriver = "";
|
||||
for (const filePath of filePaths) {
|
||||
core.debug(`Checking if the storage file exists at ${filePath}`);
|
||||
if (await fileExists(filePath)) {
|
||||
core.debug(`Storage file exists at ${filePath}`);
|
||||
const fileContent = ini.parse(await fs.readFile(filePath, "utf-8"));
|
||||
if (fileContent.storage.driver) {
|
||||
storageDriver = fileContent.storage.driver;
|
||||
}
|
||||
}
|
||||
}
|
||||
return storageDriver;
|
||||
}
|
||||
|
||||
export async function isStorageDriverOverlay(): Promise<boolean> {
|
||||
let xdgConfigHome = path.join(os.homedir(), ".config");
|
||||
if (process.env.XDG_CONFIG_HOME) {
|
||||
xdgConfigHome = process.env.XDG_CONFIG_HOME;
|
||||
}
|
||||
const filePaths: string[] = [
|
||||
"/etc/containers/storage.conf",
|
||||
path.join(xdgConfigHome, "containers/storage.conf"),
|
||||
];
|
||||
const storageDriver = await findStorageDriver(filePaths);
|
||||
return (storageDriver === "overlay");
|
||||
}
|
||||
|
||||
async function fileExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await fs.access(filePath);
|
||||
return true;
|
||||
}
|
||||
catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function findFuseOverlayfsPath(): Promise<string | undefined> {
|
||||
let fuseOverlayfsPath;
|
||||
try {
|
||||
fuseOverlayfsPath = await io.which("fuse-overlayfs");
|
||||
}
|
||||
catch (err) {
|
||||
core.debug(err);
|
||||
}
|
||||
|
||||
return fuseOverlayfsPath;
|
||||
}
|
Loading…
Reference in a new issue