Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGkeith committed Oct 4, 2024
1 parent 82e9d14 commit 0a3a28a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ The `--variables-file` [default: $CWD/.gitlab-ci-local-variables.yml] supports t
---
AUTHORIZATION_PASSWORD: djwqiod910321
DOCKER_LOGIN_PASSWORD: dij3213n123n12in3
# Will be type File, because value is a file path
KNOWN_HOSTS: '~/.ssh/known_hosts'
# This is only supported in the yaml format
Expand All @@ -298,6 +299,7 @@ EXAMPLE:
```
AUTHORIZATION_PASSWORD=djwqiod910321
DOCKER_LOGIN_PASSWORD=dij3213n123n12in3
# NOTE: value will be '~/.ssh/known_hosts' and NOT the content of the file
KNOWN_HOSTS='~/.ssh/known_hosts'
```

Expand Down
11 changes: 6 additions & 5 deletions src/variables-from-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class VariablesFromFiles {
}
return v;
};
const addToVariables = async (key: string, val: any, scopePriority: number) => {
const addToVariables = async (key: string, val: any, scopePriority: number, isDotEnv = false) => {
const {type, values} = unpack(val);
for (const [matcher, content] of Object.entries(values)) {
assert(typeof content == "string", `${key}.${matcher} content must be text or multiline text`);
if (type === "variable" || (type === null && !/^[/|~]/.exec(content))) {
if (isDotEnv || type === "variable" || (type === null && !/^[/|~]/.exec(content))) {
const regexp = matcher === "*" ? /.*/g : new RegExp(`^${matcher.replace(/\*/g, ".*")}$`, "g");
variables[key] = variables[key] ?? {type: "variable", environments: []};
variables[key].environments.push({content, regexp, regexpPriority: matcher.length, scopePriority});
Expand Down Expand Up @@ -115,25 +115,26 @@ export class VariablesFromFiles {

const projectVariablesFile = `${argv.cwd}/${argv.variablesFile}`;
if (fs.existsSync(projectVariablesFile)) {

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

if (typeof(projectVariablesFileData) === "string") {
isDotEnvFormat = true;
projectVariablesFileData = dotenv.parse(projectVariablesFileRawContent);
}
} catch (e) {
if (e instanceof yaml.YAMLException) {
isDotEnvFormat = true;
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);
await addToVariables(k, v, 24, isDotEnvFormat);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test-cases/project-variables-file/.envs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ SECRET_DB_DATABASE=laravel
SECRET_DB_HOST=127.0.0.1
SECRET_DB_PASSWORD=
SECRET_DB_PORT=3306
# comment and newlines are allowed

# comments are allowed in .env
SECRET_DB_USERNAME=root
SECRET_FILESYSTEM_DISK=local
SECRET_KNOWN_HOSTS="~/known_hosts"
SECRET_LOG_CHANNEL=stack
SECRET_LOG_DEPRECATIONS_CHANNEL=null
SECRET_LOG_LEVEL=debug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ job:
job2:
image: busybox
script:
- env | grep SECRET
- env | grep SECRET | sort
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {initSpawnSpy} from "../../mocks/utils.mock";
import {WhenStatics} from "../../mocks/when-statics";
import fs from "fs-extra";
import path from "path";
import {stripAnsi} from "../../utils";

const cwd = "tests/test-cases/project-variables-file";
const emptyFileVariable = "dummy";
Expand Down Expand Up @@ -102,7 +103,7 @@ test.concurrent("project-variables-file custom-path (.env)", async () => {
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
});

test.concurrent("project-variables-file custom-path (.envs)", async () => {
test.only("project-variables-file custom-path (.envs)", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
cwd: cwd,
Expand All @@ -111,29 +112,30 @@ test.concurrent("project-variables-file custom-path (.envs)", async () => {
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`,
];
const expected = `
job2 > SECRET_APP_DEBUG=true
job2 > SECRET_APP_ENV=local
job2 > SECRET_APP_KEY=
job2 > SECRET_APP_NAME=Laravel
job2 > SECRET_APP_URL=http://localhost
job2 > SECRET_BROADCAST_DRIVER=log
job2 > SECRET_CACHE_DRIVER=file
job2 > SECRET_DB_CONNECTION=mysql
job2 > SECRET_DB_DATABASE=laravel
job2 > SECRET_DB_HOST=127.0.0.1
job2 > SECRET_DB_PASSWORD=
job2 > SECRET_DB_PORT=3306
job2 > SECRET_DB_USERNAME=root
job2 > SECRET_FILESYSTEM_DISK=local
job2 > SECRET_KNOWN_HOSTS=~/known_hosts
job2 > SECRET_LOG_CHANNEL=stack
job2 > SECRET_LOG_DEPRECATIONS_CHANNEL=null
job2 > SECRET_LOG_LEVEL=debug
job2 > SECRET_MEMCACHED_HOST=127.0.0.1
job2 > SECRET_QUEUE_CONNECTION=sync
job2 > SECRET_SESSION_DRIVER=file
job2 > SECRET_SESSION_LIFETIME=120`;

expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
const stdout = stripAnsi(writeStreams.stdoutLines.join("\n"));
expect(stdout).toContain(expected);
});

0 comments on commit 0a3a28a

Please sign in to comment.