Skip to content
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

Allow injecting environment variables to jobs #237

Merged
merged 19 commits into from
Dec 5, 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
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@
],
"main": "./dist/extension.js",
"contributes": {
"configuration": {
"title": "Settings For Commands",
"properties": {
"local-ci.command.job.enable-pre-command": {
"type": "boolean",
"defaul": false,
"description": "Enable entering bash to run before the command that runs the job, like doppler run --. When true, this will prompt you to enter the pre-command bash every time a job runs."
},
"local-ci.command.job.enable-post-command": {
"type": "boolean",
"defaul": false,
"description": "Enable entering bash to run after the command that runs the job, like -e FOO=123 -e BAR=321. When true, this will prompt you to enter the post-command bash every time a job runs."
}
}
},
"commands": [
{
"command": "local-ci.job.run",
Expand Down
3 changes: 3 additions & 0 deletions src/gateway/FakeEditorGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default class FakeEditorGateway {
workspace: {
asRelativePath: (path: string) => path,
findFiles: () => Promise.resolve([{ fsPath: 'one/two/' }]),
getConfiguration: () => {
return { get: (_config: string) => false }; // eslint-disable-line @typescript-eslint/no-unused-vars
},
getWorkspaceFolder: () => '',
onDidSaveTextDocument: () => null,
registerTextDocumentContentProvider: () => null,
Expand Down
17 changes: 12 additions & 5 deletions src/job/JobRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type vscode from 'vscode';
import { getBinaryPath } from '../../node/binary';
import areTerminalsClosed from 'terminal/areTerminalsClosed';
import BuildAgentSettings from 'config/BuildAgentSettings';
import Images from 'containerization/Images';
import CommandDecorators from 'terminal/CommandDecorators';
import ConfigFile from 'config/ConfigFile';
import EditorGateway from 'gateway/EditorGateway';
import FinalTerminal from 'terminal/FinalTerminal';
Expand All @@ -19,6 +19,7 @@ import getLocalVolumePath from 'containerization/getLocalVolumePath';
import getProcessFilePath from 'process/getProcessFilePath';
import getRepoPath from 'common/getRepoPath';
import getTerminalName from 'terminal/getTerminalName';
import Images from 'containerization/Images';
import JobFactory from 'job/JobFactory';
import JobListener from './JobListener';
import JobProvider from 'job/JobProvider';
Expand All @@ -43,8 +44,8 @@ export default class JobRunner {
@inject(BuildAgentSettings)
buildAgentSettings!: BuildAgentSettings;

@inject(Images)
images!: Images;
@inject(CommandDecorators)
commandDecorators!: CommandDecorators;

@inject(ConfigFile)
configFile!: ConfigFile;
Expand All @@ -58,6 +59,9 @@ export default class JobRunner {
@inject(Types.IFsGateway)
fsGateway!: FsGateway;

@inject(Images)
images!: Images;

@inject(JobFactory)
jobFactory!: JobFactory;

Expand Down Expand Up @@ -139,15 +143,18 @@ export default class JobRunner {
this.fsGateway.fs.mkdirSync(localVolume, { recursive: true });
}

// This allows persisting files between jobs with persist_to_workspace and attach_workspace.
// This allows persisting files between jobs with persist_to_workspace and attach_workspace, and caching.
const volume = `${localVolume}:${CONTAINER_STORAGE_DIRECTORY}`;
const jobConfigPath = isJobInDynamicConfig
? dynamicConfigFilePath
: processFilePath;

const { getPreCommand, getPostCommand, evalPreCommand, evalPostCommand } =
this.commandDecorators.get();

terminal.sendText(
`cat ${jobConfigPath} | docker run -i --rm mikefarah/yq -C
${getBinaryPath()} local execute --job '${jobName}' --config ${jobConfigPath} -v ${volume}`
${getPreCommand} ${getPostCommand} ${evalPreCommand} ${getBinaryPath()} local execute --job '${jobName}' --config ${jobConfigPath} -v ${volume} ${evalPostCommand}`
);

const committedImageRepo = `${COMMITTED_IMAGE_NAMESPACE}/${jobName}`;
Expand Down
30 changes: 30 additions & 0 deletions src/terminal/CommandDecorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Types from 'common/Types';
import EditorGateway from 'gateway/EditorGateway';
import { inject, injectable } from 'inversify';

@injectable()
export default class CommandDecorators {
@inject(Types.IEditorGateway)
editorGateway!: EditorGateway;

get() {
const isPreCommand = this.editorGateway.editor.workspace
.getConfiguration('local-ci')
.get('command.job.enable-pre-command');

const isPostCommand = this.editorGateway.editor.workspace
.getConfiguration('local-ci')
.get('command.job.enable-post-command');

return {
getPreCommand: isPreCommand
? `echo "Please enter what you want to run before the job command (this will appear in stdout):"; read pre_command;`
: ``,
getPostCommand: isPostCommand
? `echo "Please enter what you want to run after the job command, followed by enter (you will not see anything as you type):"; read -s post_command;`
: ``,
evalPreCommand: isPreCommand ? `eval $pre_command` : ``,
evalPostCommand: isPostCommand ? `eval $post_command` : ``,
};
}
}
45 changes: 45 additions & 0 deletions src/terminal/test/CommandDecorators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import FakeEditorGateway from 'gateway/FakeEditorGateway';
import AppTestHarness from 'test-tool/helper/AppTestHarness';
import CommandDecorators from '../CommandDecorators';

let editorGateway: FakeEditorGateway;
let testHarness: AppTestHarness;
let commandDecorators: CommandDecorators;

describe('CommandDecorators', () => {
beforeEach(() => {
testHarness = new AppTestHarness();
testHarness.init();
editorGateway = testHarness.editorGateway;
commandDecorators = testHarness.container.get(CommandDecorators);
});

test('neither pre nor post command enabled', () => {
expect(commandDecorators.get()).toEqual({
getPreCommand: '',
getPostCommand: '',
evalPreCommand: '',
evalPostCommand: '',
});
});

test('both pre and post command enabled', () => {
editorGateway.editor.workspace.getConfiguration = jest.fn(() => {
return {
get: (configuration: string) => {
return [
'command.job.enable-pre-command',
'command.job.enable-post-command',
].includes(configuration);
},
};
});

expect(commandDecorators.get()).toEqual({
getPreCommand: `echo "Please enter what you want to run before the job command (this will appear in stdout):"; read pre_command;`,
getPostCommand: `echo "Please enter what you want to run after the job command, followed by enter (you will not see anything as you type):"; read -s post_command;`,
evalPreCommand: `eval $pre_command`,
evalPostCommand: `eval $post_command`,
});
});
});