Skip to content

Commit a795069

Browse files
authored
image:entrypoint implemented
1 parent 8f60469 commit a795069

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

src/job-expanders.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ export function artifacts(gitlabData: any) {
4444
});
4545
}
4646

47-
export function image(gitlabData: any, envs: { [key: string]: string }) {
47+
export function image(gitlabData: any) {
4848
Utils.forEachRealJob(gitlabData, (_, jobData) => {
4949
const expandedImage = jobData.image || (gitlabData.default || {}).image || gitlabData.image;
5050
if (expandedImage) {
51-
jobData.image = Utils.expandText(expandedImage, envs);
51+
jobData.image = {
52+
name: typeof expandedImage === 'string' ? expandedImage : expandedImage.name,
53+
entrypoint: typeof expandedImage === 'string' ? null : expandedImage.entrypoint,
54+
}
5255
}
5356
});
5457
}

src/job.ts

+31-14
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,30 @@ export class Job {
101101
this.allowFailure = ruleResult.allowFailure;
102102
}
103103

104-
if (this.interactive && (this.when !== 'manual' || this.image !== null)) {
104+
if (this.interactive && (this.when !== 'manual' || this.imageName !== null)) {
105105
throw new ExitError(`${this.getJobNameString()} @Interactive decorator cannot have image: and must be when:manual`);
106106
}
107107
}
108108

109-
get image(): string | null {
110-
let image = this.jobData['image']
109+
get imageName(): string | null {
110+
const image = this.jobData['image'];
111111
if (!image) {
112112
return null;
113113
}
114114

115-
image = Utils.expandText(image, this.expandedVariables);
116-
return image.includes(':') ? image : `${image}:latest`
115+
const imageName = Utils.expandText(image.name, this.expandedVariables);
116+
return imageName.includes(':') ? imageName : `${imageName}:latest`;
117+
}
118+
119+
get imageEntrypoint(): string[] | null {
120+
const image = this.jobData['image'];
121+
if (!image) {
122+
return null;
123+
}
124+
if (typeof image.entrypoint !== 'object') {
125+
throw new ExitError(`image:entrypoint must be an array`);
126+
}
127+
return image.entrypoint;
117128
}
118129

119130
get stage(): string {
@@ -164,7 +175,7 @@ export class Job {
164175
await fs.truncate(this.getOutputFilesPath());
165176
if (!this.interactive) {
166177
const jobNameStr = this.getJobNameString();
167-
process.stdout.write(`${jobNameStr} ${magentaBright("starting")} ${this.image ?? "shell"} (${yellow(this.stage)})\n`);
178+
process.stdout.write(`${jobNameStr} ${magentaBright("starting")} ${this.imageName ?? "shell"} (${yellow(this.stage)})\n`);
168179
}
169180

170181
const prescripts = this.beforeScripts.concat(this.scripts);
@@ -294,14 +305,14 @@ export class Job {
294305
});
295306
}
296307

297-
if (this.image) {
308+
if (this.imageName) {
298309
time = process.hrtime();
299-
process.stdout.write(`${jobNameStr} ${magentaBright('pulling')} ${this.image}\n`);
310+
process.stdout.write(`${jobNameStr} ${magentaBright('pulling')} ${this.imageName}\n`);
300311
let pullCmd = ``;
301-
pullCmd += `docker image ls --format '{{.Repository}}:{{.Tag}}' | grep -E '^${this.image}$'\n`
312+
pullCmd += `docker image ls --format '{{.Repository}}:{{.Tag}}' | grep -E '^${this.imageName}$'\n`
302313
pullCmd += `if [ "$?" -ne 0 ]; then\n`
303-
pullCmd += `\techo "Pulling ${this.image}"\n`
304-
pullCmd += `\tdocker pull ${this.image}\n`
314+
pullCmd += `\techo "Pulling ${this.imageName}"\n`
315+
pullCmd += `\tdocker pull ${this.imageName}\n`
305316
pullCmd += `fi\n`
306317
await Utils.spawn(pullCmd, this.cwd);
307318
endTime = process.hrtime(time);
@@ -314,11 +325,17 @@ export class Job {
314325
dockerCmd += `docker create -u 0:0 -i `;
315326
}
316327

328+
if (this.imageEntrypoint) {
329+
this.imageEntrypoint.forEach((e) => {
330+
dockerCmd += `--entrypoint "${e}" `
331+
});
332+
}
333+
317334
for (const [key, value] of Object.entries(this.expandedVariables)) {
318335
dockerCmd += `-e ${key}="${String(value).trim()}" `
319336
}
320337

321-
dockerCmd += `${this.image} sh -c "\n`
338+
dockerCmd += `${this.imageName} sh -c "\n`
322339
dockerCmd += `if [ -x /usr/local/bin/bash ]; then\n`
323340
dockerCmd += `\texec /usr/local/bin/bash \n`;
324341
dockerCmd += `elif [ -x /usr/bin/bash ]; then\n`;
@@ -356,7 +373,7 @@ export class Job {
356373

357374
cp.stdin.write(`set -eo pipefail\n`);
358375

359-
if (this.image) {
376+
if (this.imageName) {
360377
cp.stdin.write(`cd /builds/\n`);
361378
cp.stdin.write(`chown root:root -R .\n`);
362379
cp.stdin.write(`chmod a+w -R .\n`);
@@ -402,7 +419,7 @@ export class Job {
402419
cp.on("error", (err) => reject(err));
403420
});
404421

405-
if (this.image) {
422+
if (this.imageName) {
406423
for (const artifactPath of this.artifacts.paths) {
407424
const expandedPath = Utils.expandText(artifactPath, this.expandedVariables).replace(/\/$/, '');
408425

src/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class Parser {
171171
// Expand various fields in gitlabData
172172
jobExpanders.jobExtends(gitlabData);
173173
jobExpanders.artifacts(gitlabData);
174-
jobExpanders.image(gitlabData, gitlabData.variables || {});
174+
jobExpanders.image(gitlabData);
175175
jobExpanders.beforeScripts(gitlabData);
176176
jobExpanders.afterScripts(gitlabData);
177177
jobExpanders.scripts(gitlabData);

tests/cases.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ test('image <test-entrypoint>', async () => {
138138

139139
});
140140

141+
test('image <test-entrypoint-override>', async () => {
142+
await defaultCmd.handler({
143+
cwd: 'tests/test-cases/image',
144+
job: 'test-entrypoint-override'
145+
});
146+
expect(mockProcessStdout).toHaveBeenCalledWith("Test something\n");
147+
expect(mockProcessExit).toBeCalledTimes(0);
148+
});
149+
141150
test('no-script <test-job>', async () => {
142151
try {
143152
await defaultCmd.handler({

tests/test-cases/image/.gitlab-ci.yml

+7
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ test-entrypoint:
1919
- pwd
2020
- echo "Test Entrypoint"
2121
- cat test-file.txt
22+
23+
test-entrypoint-override:
24+
image:
25+
name: firecow/gitlab-ci-local-test-image:entrypoint-with-exit
26+
entrypoint: [""]
27+
script:
28+
echo "Test something"

0 commit comments

Comments
 (0)