From 30cbf2620bb8cf7a153cb2f22d9593d7f48b2a8e Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Fri, 17 Jan 2025 12:14:15 +0100 Subject: [PATCH 1/4] Implement GCL_PROJECT_DIR_ON_HOST --- README.md | 5 +++++ src/job.ts | 4 ++++ tests/test-cases/image/.gitlab-ci.yml | 2 +- tests/test-cases/image/integration.test.ts | 7 +++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8f934fe08..6e9abc265 100644 --- a/README.md +++ b/README.md @@ -386,6 +386,11 @@ CI_SERVER_PORT: 8443 CI_SERVER_SHELL_SSH_PORT: 8022 ``` +### Speciel job variables +- `GCL_PROJECT_DIR_ON_HOST` + - Absolute path to gitlab-ci-local current working directory on the host machine + - Use in docker-executor jobs only + ## Development You need nodejs 18+ diff --git a/src/job.ts b/src/job.ts index 9fff857c8..c249ebe81 100644 --- a/src/job.ts +++ b/src/job.ts @@ -211,6 +211,10 @@ export class Job { for (const unsetVariable of argv.unsetVariables) { delete this._variables[unsetVariable]; } + // Set GCL_PROJECT_DIR_ON_HOST if docker image + if (this.imageName(this._variables)) { + this._variables = {...this._variables, ...{GCL_PROJECT_DIR_ON_HOST: cwd}}; + } assert(this.scripts || this.trigger, chalk`{blueBright ${this.name}} must have script specified`); diff --git a/tests/test-cases/image/.gitlab-ci.yml b/tests/test-cases/image/.gitlab-ci.yml index fe049c28e..6a65d1432 100644 --- a/tests/test-cases/image/.gitlab-ci.yml +++ b/tests/test-cases/image/.gitlab-ci.yml @@ -8,7 +8,7 @@ default: test job: extends: .image script: - - echo "Test something" + - echo "Test something ${GCL_PROJECT_DIR_ON_HOST}" test-entrypoint: # This image have an `echo ${MULTILINE_VARIABLE}` in its entry point diff --git a/tests/test-cases/image/integration.test.ts b/tests/test-cases/image/integration.test.ts index 8c220e20a..5c6484518 100644 --- a/tests/test-cases/image/integration.test.ts +++ b/tests/test-cases/image/integration.test.ts @@ -1,7 +1,7 @@ import {WriteStreamsMock} from "../../../src/write-streams.js"; import {handler} from "../../../src/handler.js"; import fs from "fs-extra"; -import chalk from "chalk"; +import chalk, {blueBright} from "chalk"; import {initSpawnSpy} from "../../mocks/utils.mock.js"; import {WhenStatics} from "../../mocks/when-statics.js"; import {cleanupJobResources, Job} from "../../../src/job.js"; @@ -18,7 +18,10 @@ test.concurrent("image ", async () => { cwd: "tests/test-cases/image", job: ["test job"], }, writeStreams); - const expected = [chalk`{blueBright test job} {greenBright >} Test something`]; + + const projectDirOnHost = `${process.cwd()}/tests/test-cases/image`; + + const expected = [chalk`{blueBright test job} {greenBright >} Test something ${projectDirOnHost}`]; expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected)); }); From 583ad464b95b579f35a9a7c5f81969dc45c856e5 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Fri, 17 Jan 2025 12:16:49 +0100 Subject: [PATCH 2/4] Fix eslint --- tests/test-cases/image/integration.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-cases/image/integration.test.ts b/tests/test-cases/image/integration.test.ts index 5c6484518..9f69a4a5b 100644 --- a/tests/test-cases/image/integration.test.ts +++ b/tests/test-cases/image/integration.test.ts @@ -1,7 +1,7 @@ import {WriteStreamsMock} from "../../../src/write-streams.js"; import {handler} from "../../../src/handler.js"; import fs from "fs-extra"; -import chalk, {blueBright} from "chalk"; +import chalk from "chalk"; import {initSpawnSpy} from "../../mocks/utils.mock.js"; import {WhenStatics} from "../../mocks/when-statics.js"; import {cleanupJobResources, Job} from "../../../src/job.js"; From fa3cc1717ed1c444449994ac62b9435f432dbe55 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Fri, 17 Jan 2025 12:19:37 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6e9abc265..2382387e8 100644 --- a/README.md +++ b/README.md @@ -386,10 +386,8 @@ CI_SERVER_PORT: 8443 CI_SERVER_SHELL_SSH_PORT: 8022 ``` -### Speciel job variables -- `GCL_PROJECT_DIR_ON_HOST` - - Absolute path to gitlab-ci-local current working directory on the host machine - - Use in docker-executor jobs only +### Special variables +- `GCL_PROJECT_DIR_ON_HOST` Absolute path to gitlab-ci-local current working directory on the host machine. Use in docker-executor jobs only. ## Development From 69e36611e2a9bf3e7490e149b76b6271a9e5ecb7 Mon Sep 17 00:00:00 2001 From: Mads Jon Nielsen Date: Fri, 17 Jan 2025 12:30:11 +0100 Subject: [PATCH 4/4] Do the chacha --- tests/test-cases/predefined-variables/integration.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test-cases/predefined-variables/integration.test.ts b/tests/test-cases/predefined-variables/integration.test.ts index 024732ce7..a287b4c98 100644 --- a/tests/test-cases/predefined-variables/integration.test.ts +++ b/tests/test-cases/predefined-variables/integration.test.ts @@ -70,6 +70,7 @@ const envVars: {[key: string]: string} = { CI_SERVER_URL: "https://gitlab.com", CI_TEMPLATE_REGISTRY_HOST: "registry.gitlab.com", FF_DISABLE_UMASK_FOR_DOCKER_EXECUTOR: "false", + GCL_PROJECT_DIR_ON_HOST: "", // this will get dynamicly filled GITLAB_CI: "false", GITLAB_USER_EMAIL: "test@test.com", GITLAB_USER_ID: "990", @@ -118,6 +119,8 @@ describe("predefined-variables", () => { noColor: true, }, writeStreams); + envVars["GCL_PROJECT_DIR_ON_HOST"] = `${process.cwd()}/tests/test-cases/predefined-variables`; + let expected = ""; Object.keys(envVars).forEach(key => { expected += `test-job > ${key}=${envVars[key]}\n`; @@ -142,6 +145,8 @@ CI_SERVER_SHELL_SSH_PORT: 8022 noColor: true, }, writeStreams); + envVars["GCL_PROJECT_DIR_ON_HOST"] = `${process.cwd()}/tests/test-cases/predefined-variables`; + envVars["CI_API_V4_URL"] = "https://gitlab.com:8443/api/v4"; envVars["CI_JOB_URL"] = `https://gitlab.com:8443/GCL/predefined-variables/-/jobs/${mockJobId}`; envVars["CI_PIPELINE_URL"] = "https://gitlab.com:8443/GCL/predefined-variables/pipelines/0";