Skip to content

Commit

Permalink
feat: add support for .env format for variables-file
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGkeith committed Oct 4, 2024
1 parent f70f530 commit 2985889
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/variables-from-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import chalk from "chalk";
import {Argv} from "./argv";
import assert from "assert";
import {Utils} from "./utils";
import dotenv from "dotenv";

export interface CICDVariable {
type: "file" | "variable";
Expand Down Expand Up @@ -114,8 +115,22 @@ export class VariablesFromFiles {

const projectVariablesFile = `${argv.cwd}/${argv.variablesFile}`;
if (fs.existsSync(projectVariablesFile)) {
const projectVariablesFileData: any = yaml.load(await fs.readFile(projectVariablesFile, "utf8"), {schema: yaml.FAILSAFE_SCHEMA}) ?? {};

const projectVariablesFileRawContent = await fs.readFile(projectVariablesFile, "utf8");
let projectVariablesFileData;
try {
projectVariablesFileData = yaml.load(projectVariablesFileRawContent, {schema: yaml.FAILSAFE_SCHEMA}) ?? {};

if (typeof(projectVariablesFileData) === "string") {
projectVariablesFileData = dotenv.parse(projectVariablesFileRawContent);
}
} catch (e) {
if (e instanceof yaml.YAMLException) {
projectVariablesFileData = dotenv.parse(projectVariablesFileRawContent);
}
}
assert(projectVariablesFileData != null, "projectEntries cannot be null/undefined");

assert(Utils.isObject(projectVariablesFileData), `${argv.cwd}/.gitlab-ci-local-variables.yml must contain an object`);
for (const [k, v] of Object.entries(projectVariablesFileData)) {
await addToVariables(k, v, 24);
Expand Down
1 change: 1 addition & 0 deletions tests/test-cases/project-variables-file/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SECRET="holycow"
23 changes: 23 additions & 0 deletions tests/test-cases/project-variables-file/.envs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SECRET_APP_DEBUG=true
SECRET_APP_ENV=local
SECRET_APP_KEY=
SECRET_APP_NAME="Laravel"
SECRET_APP_URL=http://localhost
SECRET_BROADCAST_DRIVER=log
SECRET_CACHE_DRIVER=file
SECRET_DB_CONNECTION=mysql
SECRET_DB_DATABASE=laravel
SECRET_DB_HOST=127.0.0.1
SECRET_DB_PASSWORD=
SECRET_DB_PORT=3306
# comment and newlines are allowed

SECRET_DB_USERNAME=root
SECRET_FILESYSTEM_DISK=local
SECRET_LOG_CHANNEL=stack
SECRET_LOG_DEPRECATIONS_CHANNEL=null
SECRET_LOG_LEVEL=debug
SECRET_MEMCACHED_HOST=127.0.0.1
SECRET_QUEUE_CONNECTION=sync
SECRET_SESSION_DRIVER=file
SECRET_SESSION_LIFETIME=120
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
Expand Up @@ -3,3 +3,8 @@ job:
image: busybox
script:
- echo $SECRET

job2:
image: busybox
script:
- env | grep SECRET
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,62 @@ test.concurrent("project-variables-file custom-path", async () => {
cwd: "tests/test-cases/project-variables-file",
file: ".gitlab-ci-custom.yml",
variablesFile: ".custom-local-var-file",
job: ["job"],
}, writeStreams);

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

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

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

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

const expected = [
chalk`{blueBright job2} {greenBright >} SECRET_APP_DEBUG=true`,
chalk`{blueBright job2} {greenBright >} SECRET_APP_ENV=local`,
chalk`{blueBright job2} {greenBright >} SECRET_APP_KEY=`,
chalk`{blueBright job2} {greenBright >} SECRET_APP_NAME=Laravel`,
chalk`{blueBright job2} {greenBright >} SECRET_APP_URL=http://localhost`,
chalk`{blueBright job2} {greenBright >} SECRET_BROADCAST_DRIVER=log`,
chalk`{blueBright job2} {greenBright >} SECRET_CACHE_DRIVER=file`,
chalk`{blueBright job2} {greenBright >} SECRET_DB_CONNECTION=mysql`,
chalk`{blueBright job2} {greenBright >} SECRET_DB_DATABASE=laravel`,
chalk`{blueBright job2} {greenBright >} SECRET_DB_HOST=127.0.0.1`,
chalk`{blueBright job2} {greenBright >} SECRET_DB_PASSWORD=`,
chalk`{blueBright job2} {greenBright >} SECRET_DB_PORT=3306`,
chalk`{blueBright job2} {greenBright >} SECRET_DB_USERNAME=root`,
chalk`{blueBright job2} {greenBright >} SECRET_FILESYSTEM_DISK=local`,
chalk`{blueBright job2} {greenBright >} SECRET_LOG_CHANNEL=stack`,
chalk`{blueBright job2} {greenBright >} SECRET_LOG_DEPRECATIONS_CHANNEL=null`,
chalk`{blueBright job2} {greenBright >} SECRET_LOG_LEVEL=debug`,
chalk`{blueBright job2} {greenBright >} SECRET_MEMCACHED_HOST=127.0.0.1`,
chalk`{blueBright job2} {greenBright >} SECRET_QUEUE_CONNECTION=sync`,
chalk`{blueBright job2} {greenBright >} SECRET_SESSION_DRIVER=file`,
chalk`{blueBright job2} {greenBright >} SECRET_SESSION_LIFETIME=120`,
];

expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
});

0 comments on commit 2985889

Please sign in to comment.