Skip to content

Commit

Permalink
feat: hack to bypass linux file permissions issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mdubourg001 authored Feb 26, 2021
2 parents 0e521cc + b432f90 commit 74be36a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pre:
- mkdir -p node_modules/lodash
- touch node_modules/lodash/index.js
- touch foo.txt
- mkdir bar && touch bar/bar.txt
- mkdir -p bar && touch bar/bar.txt
artifacts:
paths:
- foo.txt
Expand Down
46 changes: 41 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const path = require("path");
const fs = require("fs-extra");
const slugify = require("slugify");
const { performance } = require("perf_hooks");
const { promisify } = require("util");
const osExec = promisify(require("child_process").exec);

const define = require("./pre-defined");

Expand Down Expand Up @@ -73,11 +75,20 @@ function mkdirpRecSync(dir) {
}
}

async function execCommands(workdir, container, commands, onerror) {
async function execCommands({
workdir,
container,
commands,
onerror,
verbose = true,
}) {
const preparedCommands = typeof commands === "string" ? [commands] : commands;

for (const command of preparedCommands) {
console.log(chalk.bold(chalk.green(command)));
if (verbose) {
console.log(chalk.bold(chalk.green(command)));
}

let exec = null;
let stream = null;

Expand All @@ -101,6 +112,7 @@ async function execCommands(workdir, container, commands, onerror) {
if (inspect.ExitCode !== 0) reject();
resolve();
});

container.modem.demuxStream(stream, process.stdout, {
write: (err) => console.error(chalk.red(err.toString())),
});
Expand Down Expand Up @@ -395,17 +407,41 @@ async function main() {
if (job.before_script || DEFAULT.before_script) {
const commands = job.before_script ?? DEFAULT.before_script;

await execCommands(workdir, container, commands, onerror);
await execCommands({ workdir, container, commands, onerror });
}

// running script
await execCommands(workdir, container, job.script, onerror);
await execCommands({
workdir,
container,
commands: job.script,
onerror,
});

// running after_script
if (job.after_script || DEFAULT.after_script) {
const commands = job.after_script ?? DEFAULT.after_script;

await execCommands(workdir, container, commands, onerror);
await execCommands({ workdir, container, commands, onerror });
}

// hack for linux: chown -R <workdir>/ inside container to fix root permissions
// TODO: do other platforms need the hack ?
if (process.platform === "linux") {
const { stdout, stderr } = await osExec("id -u");

if (stderr) {
await onerror(stderr, container);
} else if (stdout.trim()) {
const commands = [`chown -R ${stdout.trim()}: ${workdir}`];
await execCommands({
workdir,
container,
commands,
onerror,
verbose: false,
});
}
}

// updating cache directory (if policy asks) after job ended
Expand Down

0 comments on commit 74be36a

Please sign in to comment.