Skip to content

Commit ea080e1

Browse files
committed
Refactor remoteVariables handling to support multiple values and update documentation
1 parent 43b18b5 commit ea080e1

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ Variables will now appear in your jobs, if project or group matches git remote,
277277

278278
### Remote file variables
279279

280+
You can also fetch variable files from remote git repositories. If you need multiple variable files, repeat the `--remote-variables` flag.
281+
280282
```shell
281283
gitlab-ci-local --remote-variables git@gitlab.com:firecow/example.git=gitlab-variables.yml=master
282284
```

src/argv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class Argv {
165165
return this.map.get("pullPolicy") ?? "if-not-present";
166166
}
167167

168-
get remoteVariables (): string {
168+
get remoteVariables (): Array<string> {
169169
return this.map.get("remoteVariables");
170170
}
171171

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ process.on("SIGUSR2", async () => await cleanupJobResources(jobs));
153153
requiresArg: false,
154154
})
155155
.option("remote-variables", {
156-
type: "string",
156+
type: "array",
157157
description: "Fetch variables file from remote location",
158158
requiresArg: false,
159159
})

src/variables-from-files.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {Argv} from "./argv.js";
77
import assert from "assert";
88
import {Utils} from "./utils.js";
99
import dotenv from "dotenv";
10+
import deepmerge from "deepmerge";
1011

1112
export interface CICDVariable {
1213
type: "file" | "variable";
@@ -33,13 +34,15 @@ export class VariablesFromFiles {
3334
let homeFileData: any = {};
3435

3536
if (remoteVariables && !autoCompleting) {
36-
const match = /(?<url>git@.*?)=(?<file>.*?)=(?<ref>.*)/.exec(remoteVariables);
37-
assert(match != null, "--remote-variables is malformed use 'git@gitlab.com:firecow/example.git=gitlab-variables.yml=master' syntax");
38-
const url = match.groups?.url;
39-
const file = match.groups?.file;
40-
const ref = match.groups?.ref;
41-
const res = await Utils.bash(`set -eou pipefail; git archive --remote=${url} ${ref} ${file} | tar -xO ${file}`, cwd);
42-
remoteFileData = yaml.load(`${res.stdout}`);
37+
for (let i = 0; i < remoteVariables.length; i++) {
38+
const match = /(?<url>git@.*?)=(?<file>.*?)=(?<ref>.*)/.exec(remoteVariables[i]);
39+
assert(match != null, "--remote-variables is malformed use 'git@gitlab.com:firecow/example.git=gitlab-variables.yml=master' syntax");
40+
const url = match.groups?.url;
41+
const file = match.groups?.file;
42+
const ref = match.groups?.ref;
43+
const res = await Utils.bash(`set -eou pipefail; git archive --remote=${url} ${ref} ${file} | tar -xO ${file}`, cwd);
44+
remoteFileData = deepmerge.all([remoteFileData, yaml.load(`${res.stdout}`)]);
45+
}
4346
}
4447

4548
if (await fs.pathExists(homeVariablesFile)) {

tests/test-cases/remote-variables-file/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test.concurrent("remote-variables-file <test-job>", async () => {
1818
await handler({
1919
cwd: "tests/test-cases/remote-variables-file",
2020
job: ["test-job"],
21-
remoteVariables: "git@gitlab.com:example/firecow.git=variables.yml=main",
21+
remoteVariables: ["git@gitlab.com:example/firecow.git=variables.yml=main"],
2222
}, writeStreams);
2323

2424
const expected = [

0 commit comments

Comments
 (0)