Skip to content

Commit

Permalink
Implement GCL_PROJECT_DIR_ON_HOST (#1471)
Browse files Browse the repository at this point in the history
* Implement GCL_PROJECT_DIR_ON_HOST

* Fix eslint

* Update README.md

* Do the chacha
  • Loading branch information
firecow authored Jan 20, 2025
1 parent 03be1a4 commit 4fd84db
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ CI_SERVER_PORT: 8443
CI_SERVER_SHELL_SSH_PORT: 8022
```

### 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

You need nodejs 18+
Expand Down
4 changes: 4 additions & 0 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,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`);

Expand Down
2 changes: 1 addition & 1 deletion tests/test-cases/image/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/test-cases/image/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ test.concurrent("image <test job>", 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));
});

Expand Down
5 changes: 5 additions & 0 deletions tests/test-cases/predefined-variables/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
GITLAB_USER_ID: "990",
Expand Down Expand Up @@ -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`;
Expand All @@ -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";
Expand Down

0 comments on commit 4fd84db

Please sign in to comment.