+
Skip to content

Make the additional env vars more robust #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/common/AppIoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import BaseIoc from 'common/BaseIoc';
import ChildProcessGateway from 'gateway/ChildProcessGateway';
import EditorGateway from 'gateway/EditorGateway';
import EnvVar from 'process/EnvVar';
import FsGateway from 'gateway/FsGateway';
import HttpGateway from 'gateway/HttpGateway';
import OsGateway from 'gateway/OsGateway';
Expand All @@ -16,6 +17,7 @@ iocContainer
.to(ChildProcessGateway)
.inSingletonScope();
iocContainer.bind(Types.IEditorGateway).to(EditorGateway).inSingletonScope();
iocContainer.bind(Types.IEnvVar).to(EnvVar).inSingletonScope();
iocContainer.bind(Types.IFsGateway).to(FsGateway).inSingletonScope();
iocContainer.bind(Types.IHttpGateway).to(HttpGateway).inSingletonScope();
iocContainer.bind(Types.IOsGateway).to(OsGateway).inSingletonScope();
Expand Down
1 change: 1 addition & 0 deletions src/common/Types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
IChildProcessGateway: Symbol.for('IChildProcessGateway'),
IEditorGateway: Symbol.for('IEditorGateway'),
IEnvVar: Symbol.for('IEnvVar'),
IFsGateway: Symbol.for('IFsGateway'),
IHttpGateway: Symbol.for('IHttpGateway'),
IOsGateway: Symbol.for('IOsGateway'),
Expand Down
10 changes: 8 additions & 2 deletions src/config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import getDynamicConfigParametersPath from './getDynamicConfigParametersPath';
import getDynamicConfigPath from './getDynamicConfigPath';
import getProcessedConfig from './getProcessedConfig';
import getProcessFilePath from 'process/getProcessFilePath';
import getRepoPath from 'common/getRepoPath';
import ProcessFile from 'process/ProcessFile';
import ReporterGateway from 'gateway/ReporterGateway';
import Spawn from 'common/Spawn';
Expand Down Expand Up @@ -55,7 +56,11 @@ export default class Config {
this.childProcessGateway.cp,
this.spawn.getOptions()
);
this.processFile.write(processedConfig, processFilePath);
this.processFile.write(
processedConfig,
processFilePath,
getRepoPath(configFilePath)
);

const dynamicConfigFilePath = getDynamicConfigPath(configFilePath);
if (this.fsGateway.fs.existsSync(dynamicConfigFilePath)) {
Expand All @@ -70,7 +75,8 @@ export default class Config {
this.childProcessGateway.cp,
this.spawn.getOptions()
),
dynamicConfigFilePath
dynamicConfigFilePath,
getRepoPath(configFilePath)
);
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/job/Children.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class Children {
return [
...this.getLogTreeItems(
logs,
'getJobName' in parentElement ? parentElement.getJobName() : ''
'getJobName' in parentElement ? parentElement?.getJobName() : ''
),
...this.getJobTreeItems(children, logs, runningJob, jobDependencies),
];
Expand Down
3 changes: 2 additions & 1 deletion src/job/JobRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import getImageFromJob from 'containerization/getImageFromJob';
import getLogFilePath from 'log/getLogFilePath';
import getLocalVolumePath from 'containerization/getLocalVolumePath';
import getProcessFilePath from 'process/getProcessFilePath';
import getRepoPath from 'common/getRepoPath';
import getTerminalName from 'terminal/getTerminalName';
import JobFactory from 'job/JobFactory';
import JobListener from './JobListener';
Expand Down Expand Up @@ -87,7 +88,7 @@ export default class JobRunner {
}

const configFilePath = await this.configFile.getPath(context);
const repoPath = path.dirname(path.dirname(configFilePath));
const repoPath = getRepoPath(configFilePath);
const terminal = this.editorGateway.editor.window.createTerminal({
name: getTerminalName(jobName),
message: `Running the CircleCI® job ${jobName}…`,
Expand Down
39 changes: 39 additions & 0 deletions src/process/EnvVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { injectable, inject } from 'inversify';
import Types from 'common/Types';
import { addEnvVars } from 'script';
import ChildProcessGateway from 'gateway/ChildProcessGateway';
import Spawn from 'common/Spawn';

@injectable()
export default class EnvVar {
@inject(Types.IChildProcessGateway)
childProcessGateway!: ChildProcessGateway;

@inject(Spawn)
spawn!: Spawn;

getStep(repoPath: string) {
let command;
try {
const exportVars = this.childProcessGateway.cp
.execSync(addEnvVars, {
...this.spawn.getOptions(repoPath),
timeout: 2000,
})
.toString();

command = `echo '${exportVars}' >> $BASH_ENV`;
} catch (error) {
command = `echo "There was an error setting the variables: ${
(error as ErrorWithMessage).message
}"`;
}

return {
run: {
name: 'Set more environment variables command',
command,
},
};
}
}
16 changes: 16 additions & 0 deletions src/process/FakeEnvVar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { injectable } from 'inversify';

@injectable()
export default class FakeEnvVar {
getStep() {
return {
run: {
name: 'Set more environment variables',
command: `echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV`,
},
};
}
}
28 changes: 7 additions & 21 deletions src/process/ProcessFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import {
DYNAMIC_CONFIG_PARAMETERS_FILE_NAME,
DYNAMIC_CONFIG_PATH_IN_CONTAINER,
} from 'constant';
import { addEnvVars } from 'script';
import EnvVar from './EnvVar';

@injectable()
export default class ProcessFile {
@inject(Types.IEnvVar)
envVar!: EnvVar;

@inject(Types.IFsGateway)
fsGateway!: FsGateway;

Expand All @@ -28,9 +31,9 @@ export default class ProcessFile {
* this copies the files inside the container to the volume shared with the local machine.
* This way, they can persist between jobs.
* Likewise, on attach_workspace, it copies from the volume.
* The processedConfig was already compiled by the CircleCI® CLI binary.
* The processedConfig was already compiled by the CircleCI CLI binary.
*/
write(processedConfig: string, processFilePath: string) {
write(processedConfig: string, processFilePath: string, repoPath: string) {
const config = getConfig(processedConfig);

if (!config) {
Expand Down Expand Up @@ -134,21 +137,13 @@ export default class ProcessFile {
return step;
});

// If a 'checkout' step exists, insert env vars right after it.
if (newSteps?.includes('checkout')) {
newSteps?.splice(
newSteps?.indexOf('checkout') + 1,
0,
this.getEnvVarStep()
);
}

return {
...accumulator,
[jobName]: {
...configJobs[jobName],
steps: [
this.getEnsureVolumeIsWritableStep(),
this.envVar.getStep(repoPath),
...(newSteps ?? []),
],
},
Expand Down Expand Up @@ -224,13 +219,4 @@ export default class ProcessFile {
},
};
}

private getEnvVarStep() {
return {
run: {
name: 'Set more environment variables',
command: addEnvVars,
},
};
}
}
4 changes: 2 additions & 2 deletions src/process/test/ProcessFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('ProcessFile', () => {
const processFile = testHarness.container.get(ProcessFile);
const writeFileSpy = jest.fn();
fsGateway.fs.writeFileSync = writeFileSpy;
processFile.write(withCacheFixture, '/foo/baz/');
processFile.write(withCacheFixture, '/foo/baz/', '/your/repo/');

expect(writeFileSpy).toHaveBeenCalledTimes(1);
expect(normalize(writeFileSpy.mock.lastCall[1])).toEqual(
Expand All @@ -33,7 +33,7 @@ describe('ProcessFile', () => {
const processFile = testHarness.container.get(ProcessFile);
const writeFileSpy = jest.fn();
fsGateway.fs.writeFileSync = writeFileSpy;
processFile.write(dyanamicConfigFixture, '/foo/baz/');
processFile.write(dyanamicConfigFixture, '/foo/baz/', '/your/repo/');

expect(writeFileSpy).toHaveBeenCalledTimes(1);
expect(normalize(writeFileSpy.mock.lastCall[1])).toEqual(
Expand Down
9 changes: 4 additions & 5 deletions src/script/addEnvVars.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/sh

# shellcheck disable=SC2016
{
echo 'export CIRCLE_SHA1=$(git rev-parse HEAD)'
echo 'export CIRCLE_BRANCH=$(git rev-parse --abbrev-ref HEAD)'
echo 'export CIRCLE_PROJECT_REPONAME=$(basename $(git remote get-url origin))'
} >> "$BASH_ENV"
echo "export CIRCLE_SHA1=$(git rev-parse HEAD)"
echo "export CIRCLE_BRANCH=$(git rev-parse --abbrev-ref HEAD)"
echo "export CIRCLE_PROJECT_REPONAME=$(basename "$(git remote get-url origin)")"
echo "export CIRCLE_REPOSITORY_URL=$(git remote get-url origin)"
13 changes: 6 additions & 7 deletions src/test-tool/expected/dynamic-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- checkout
- run:
name: Set more environment variables
command: >
{
echo 'export CIRCLE_SHA1=$(git rev-parse HEAD)'
echo 'export CIRCLE_BRANCH=$(git rev-parse --abbrev-ref HEAD)'
echo 'export CIRCLE_PROJECT_REPONAME=$(basename $(git remote get-url origin))'
} >> "$BASH_ENV"
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- checkout
- run:
name: Create config
command: echo "Creating the dynamic config here"
Expand Down
62 changes: 55 additions & 7 deletions src/test-tool/expected/with-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- checkout
- run:
name: Set more environment variables
command: >
{
echo 'export CIRCLE_SHA1=$(git rev-parse HEAD)'
echo 'export CIRCLE_BRANCH=$(git rev-parse --abbrev-ref HEAD)'
echo 'export CIRCLE_PROJECT_REPONAME=$(basename $(git remote get-url origin))'
} >> "$BASH_ENV"
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- checkout
- run:
name: Restore cache
command: >
Expand Down Expand Up @@ -91,6 +90,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand Down Expand Up @@ -130,6 +136,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand All @@ -155,6 +168,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand All @@ -180,6 +200,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand Down Expand Up @@ -232,6 +259,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand Down Expand Up @@ -260,6 +294,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand All @@ -285,6 +326,13 @@ jobs:
then
sudo chown $(whoami) /tmp/local-ci
fi
- run:
name: Set more environment variables
command: >-
echo 'echo "export CIRCLE_SHA1=037f2d35d59080dc313608c565b55f37d2f42c7e"
echo "export CIRCLE_BRANCH=develop"
echo "export CIRCLE_PROJECT_REPONAME=local-ci"
echo "export CIRCLE_REPOSITORY_URL=git@github.com:getlocalci/local-ci.git"' >> $BASH_ENV
- run:
name: Attach workspace
command: |-
Expand Down
2 changes: 2 additions & 0 deletions src/test-tool/helper/AppTestHarness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FakeHttpGateway from 'gateway/FakeHttpGateway';
import FakeOsGateway from 'gateway/FakeOsGateway';
import FakeProcessGateway from 'gateway/FakeProcessGateway';
import FakeReporterGateway from 'gateway/FakeReporterGateway';
import FakeEnvVar from 'process/FakeEnvVar';

export default class AppTestHarness {
container!: Container;
Expand All @@ -25,6 +26,7 @@ export default class AppTestHarness {
.bind(Types.IChildProcessGateway)
.to(FakeChildProcessGateway)
.inSingletonScope();
this.container.bind(Types.IEnvVar).to(FakeEnvVar).inSingletonScope();
this.container
.bind(Types.IEditorGateway)
.to(FakeEditorGateway)
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载