Skip to content

Commit 4fd84db

Browse files
authored
Implement GCL_PROJECT_DIR_ON_HOST (#1471)
* Implement GCL_PROJECT_DIR_ON_HOST * Fix eslint * Update README.md * Do the chacha
1 parent 03be1a4 commit 4fd84db

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ CI_SERVER_PORT: 8443
386386
CI_SERVER_SHELL_SSH_PORT: 8022
387387
```
388388

389+
### Special variables
390+
- `GCL_PROJECT_DIR_ON_HOST` Absolute path to gitlab-ci-local current working directory on the host machine. Use in docker-executor jobs only.
391+
389392
## Development
390393

391394
You need nodejs 18+

src/job.ts

+4
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ export class Job {
224224
for (const unsetVariable of argv.unsetVariables) {
225225
delete this._variables[unsetVariable];
226226
}
227+
// Set GCL_PROJECT_DIR_ON_HOST if docker image
228+
if (this.imageName(this._variables)) {
229+
this._variables = {...this._variables, ...{GCL_PROJECT_DIR_ON_HOST: cwd}};
230+
}
227231

228232
assert(this.scripts || this.trigger, chalk`{blueBright ${this.name}} must have script specified`);
229233

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ default:
88
test job:
99
extends: .image
1010
script:
11-
- echo "Test something"
11+
- echo "Test something ${GCL_PROJECT_DIR_ON_HOST}"
1212

1313
test-entrypoint:
1414
# This image have an `echo ${MULTILINE_VARIABLE}` in its entry point

tests/test-cases/image/integration.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ test.concurrent("image <test job>", async () => {
1818
cwd: "tests/test-cases/image",
1919
job: ["test job"],
2020
}, writeStreams);
21-
const expected = [chalk`{blueBright test job} {greenBright >} Test something`];
21+
22+
const projectDirOnHost = `${process.cwd()}/tests/test-cases/image`;
23+
24+
const expected = [chalk`{blueBright test job} {greenBright >} Test something ${projectDirOnHost}`];
2225
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
2326
});
2427

tests/test-cases/predefined-variables/integration.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const envVars: {[key: string]: string} = {
7070
CI_SERVER_URL: "https://gitlab.com",
7171
CI_TEMPLATE_REGISTRY_HOST: "registry.gitlab.com",
7272
FF_DISABLE_UMASK_FOR_DOCKER_EXECUTOR: "false",
73+
GCL_PROJECT_DIR_ON_HOST: "", // this will get dynamicly filled
7374
GITLAB_CI: "false",
7475
GITLAB_USER_EMAIL: "[email protected]",
7576
GITLAB_USER_ID: "990",
@@ -118,6 +119,8 @@ describe("predefined-variables", () => {
118119
noColor: true,
119120
}, writeStreams);
120121

122+
envVars["GCL_PROJECT_DIR_ON_HOST"] = `${process.cwd()}/tests/test-cases/predefined-variables`;
123+
121124
let expected = "";
122125
Object.keys(envVars).forEach(key => {
123126
expected += `test-job > ${key}=${envVars[key]}\n`;
@@ -142,6 +145,8 @@ CI_SERVER_SHELL_SSH_PORT: 8022
142145
noColor: true,
143146
}, writeStreams);
144147

148+
envVars["GCL_PROJECT_DIR_ON_HOST"] = `${process.cwd()}/tests/test-cases/predefined-variables`;
149+
145150
envVars["CI_API_V4_URL"] = "https://gitlab.com:8443/api/v4";
146151
envVars["CI_JOB_URL"] = `https://gitlab.com:8443/GCL/predefined-variables/-/jobs/${mockJobId}`;
147152
envVars["CI_PIPELINE_URL"] = "https://gitlab.com:8443/GCL/predefined-variables/pipelines/0";

0 commit comments

Comments
 (0)