Skip to content

Commit

Permalink
feat: make project file variable file configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGkeith committed Oct 11, 2024
1 parent 794939b commit 3e8242d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/argv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ async function gitRootPath () {
}

export class Argv {
static readonly default = {
"variablesFile": ".gitlab-ci-local-variables.yml",
};

private map: Map<string, any> = new Map<string, any>();
private writeStreams: WriteStreams | undefined;
Expand Down Expand Up @@ -82,6 +85,10 @@ export class Argv {
return cwd;
}

get variablesFile (): string {
return this.map.get("variablesFile") ?? Argv.default.variablesFile;
}

get file (): string {
return this.map.get("file") ?? ".gitlab-ci.yml";
}
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ process.on("SIGUSR2", async () => await cleanupJobResources(jobs));
description: "Path to a current working directory",
requiresArg: true,
})
.option("variables-file", {
type: "string",
description: "Path to the project file variables",
requiresArg: true,
default: Argv.default.variablesFile,
})
.option("completion", {
type: "boolean",
description: "Generate tab completion script",
Expand Down
2 changes: 1 addition & 1 deletion src/variables-from-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class VariablesFromFiles {
await addVariableFileToVariables(remoteFileData, 0);
await addVariableFileToVariables(homeFileData, 10);

const projectVariablesFile = `${argv.cwd}/.gitlab-ci-local-variables.yml`;
const projectVariablesFile = `${argv.cwd}/${argv.variablesFile}`;
if (fs.existsSync(projectVariablesFile)) {
const projectVariablesFileData: any = yaml.load(await fs.readFile(projectVariablesFile, "utf8"), {schema: yaml.FAILSAFE_SCHEMA}) ?? {};
assert(projectVariablesFileData != null, "projectEntries cannot be null/undefined");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET: "firecow"
5 changes: 5 additions & 0 deletions tests/test-cases/project-variables-file/.gitlab-ci-custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
job:
image: busybox
script:
- echo $SECRET
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@ test.concurrent("project-variables-file <issue-1333>", async () => {
];
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
});

test.concurrent("project-variables-file custom-path", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
cwd: "tests/test-cases/project-variables-file",
file: ".gitlab-ci-custom.yml",
variablesFile: ".custom-local-var-file",
}, writeStreams);

const expected = [
chalk`{blueBright job} {greenBright >} firecow`,
];
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
});

0 comments on commit 3e8242d

Please sign in to comment.