Skip to content

Commit

Permalink
Added default-image option to be used with --shell-executor-no-image=…
Browse files Browse the repository at this point in the history
…false
  • Loading branch information
NGPetrov committed Oct 24, 2024
1 parent f42d102 commit 7c6fa09
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ export class Argv {
argv.injectDotenv(`${argv.cwd}/.gitlab-ci-local-env`, args);

if (!argv.shellExecutorNoImage && argv.shellIsolation) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --shell-isolation does not work with --no-shell-executor-no-image\n`);
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --shell-isolation does not work with --shell-executor-no-image\n`);
}

if (argv.defaultImage && argv.shellIsolation) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --default-image does not work with --shell-isolation=true\n`);
}

if (argv.defaultImage && argv.shellExecutorNoImage) {
writeStreams?.stderr(chalk`{black.bgYellowBright WARN } --default-image does not work with --shell-executor-no-image=true\n`);
}

return argv;
}

Expand Down Expand Up @@ -273,6 +282,10 @@ export class Argv {
return this.map.get("shellExecutorNoImage") ?? true;
}

get defaultImage (): string | null {
return this.map.get("defaultImage") ?? null;
}

get maximumIncludes (): number {
return this.map.get("maximumIncludes") ?? 150; // https://docs.gitlab.com/ee/administration/settings/continuous_integration.html#maximum-includes
}
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ process.on("SIGUSR2", async () => await cleanupJobResources(jobs));
description: "Whether to use shell executor when no image is specified.",
requiresArg: false,
})
.option("default-image", {
type: "string",
description: "When using --shell-executor-no-image=false which image to be used for the container. Defaults to docker.io/ruby:3.1 if not set.",
requiresArg: false,
})
.option("mount-cache", {
type: "boolean",
description: "Enable docker mount based caching",
Expand Down
2 changes: 1 addition & 1 deletion src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ export class Job {
return null;
} else {
// https://docs.gitlab.com/ee/ci/runners/hosted_runners/linux.html#container-images
return "docker.io/ruby:3.1";
return this.argv.defaultImage ?? "docker.io/ruby:3.1";
}
}
const expanded = Utils.expandVariables(vars);
Expand Down
5 changes: 5 additions & 0 deletions tests/test-cases/shell-executor-no-image/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
test-job:
stage: test
script:
- echo "Heya from default-image"
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {WriteStreamsMock} from "../../../src/write-streams.js";
import {handler} from "../../../src/handler.js";

test("shell-executor-no-image false default-image alpine", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
pullPolicy: "if-not-present",
cwd: "tests/test-cases/shell-executor-no-image/",
shellExecutorNoImage: false,
defaultImage: "alpine:latest",
job: ["test-job"],
noColor: true,
}, writeStreams);

expect(writeStreams.stdoutLines.join("\n")).toMatch(/test-job starting alpine:latest \(test\)/);
});

test("shell-executor-no-image false default-image null", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
pullPolicy: "if-not-present",
cwd: "tests/test-cases/shell-executor-no-image/",
shellExecutorNoImage: false,
defaultImage: null,
job: ["test-job"],
noColor: true,
}, writeStreams);

expect(writeStreams.stdoutLines.join("\n")).toMatch(/test-job starting docker.io\/ruby:3.1 \(test\)/);
});

test("shell-executor-no-image true default-image doesnt-matter", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
pullPolicy: "if-not-present",
cwd: "tests/test-cases/shell-executor-no-image/",
shellExecutorNoImage: true,
defaultImage: "doesnt-matter",
job: ["test-job"],
noColor: true,
}, writeStreams);

expect(writeStreams.stdoutLines.join("\n")).toMatch(/test-job starting shell \(test\)/);
expect(writeStreams.stderrLines.join("\n")).toMatch(/WARN\s\s--default-image does not work with --shell-executor-no-image=true/);
});

0 comments on commit 7c6fa09

Please sign in to comment.