Skip to content

Commit

Permalink
Update to run 'devcontainer exec' without JSON parsing output (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartleeks authored Mar 10, 2023
1 parent a72556d commit 98a823d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
11 changes: 6 additions & 5 deletions azdo-task/DevcontainersCi/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,14 @@ export async function runMain(): Promise<void> {
}
};
const execResult = await devcontainer.exec(execArgs, execLog);
if (execResult.outcome !== 'success') {
if (execResult !== 0) {
console.log(
`### ERROR: Dev container exec: ${execResult.message} (exit code: ${execResult.code})\n${execResult.description}`,
`### ERROR: Dev container exec failed (exit code: ${execResult})`,
);
task.setResult(
TaskResult.Failed,
`Dev container exec failed (exit code: ${execResult})`,
);
task.setResult(TaskResult.Failed, execResult.message);
}
if (execResult.outcome !== 'success') {
return;
}
if (execLogString.length >= 25000) {
Expand Down
29 changes: 22 additions & 7 deletions common/src/dev-container-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ function parseCliOutput<T>(value: string): T | DevContainerCliError {
}
}

async function runSpecCli<T>(options: {
async function runSpecCliJsonCommand<T>(options: {
args: string[];
log: (data: string) => void;
env?: NodeJS.ProcessEnv;
}) {
// For JSON commands, pass stderr on to logging but capture stdout and parse the JSON response
let stdout = '';
const spawnOptions: SpawnOptions = {
log: data => (stdout += data),
Expand All @@ -126,6 +127,22 @@ async function runSpecCli<T>(options: {

return parseCliOutput<T>(stdout);
}
async function runSpecCliNonJsonCommand(options: {
args: string[];
log: (data: string) => void;
env?: NodeJS.ProcessEnv;
}) {
// For non-JSON commands, pass both stdout and stderr on to logging
const spawnOptions: SpawnOptions = {
log: data => options.log(data),
err: data => options.log(data),
env: options.env ? {...process.env, ...options.env} : process.env,
};
const command = getSpecCliInfo().command;
console.log(`About to run ${command} ${options.args.join(' ')}`); // TODO - take an output arg to allow GH to use core.info
const result = await spawn(command, options.args, spawnOptions);
return result.code
}

export interface DevContainerCliSuccessResult {
outcome: 'success';
Expand Down Expand Up @@ -172,7 +189,7 @@ async function devContainerBuild(
commandArgs.push('--cache-from', cacheFrom),
);
}
return await runSpecCli<DevContainerCliBuildResult>({
return await runSpecCliJsonCommand<DevContainerCliBuildResult>({
args: commandArgs,
log,
env: {DOCKER_BUILDKIT: '1', COMPOSE_DOCKER_CLI_BUILD: '1'},
Expand Down Expand Up @@ -219,15 +236,13 @@ async function devContainerUp(
commandArgs.push('--mount', mount),
);
}
return await runSpecCli<DevContainerCliUpResult>({
return await runSpecCliJsonCommand<DevContainerCliUpResult>({
args: commandArgs,
log,
env: {DOCKER_BUILDKIT: '1', COMPOSE_DOCKER_CLI_BUILD: '1'},
});
}

export interface DevContainerCliExecResult
extends DevContainerCliSuccessResult {}
export interface DevContainerCliExecArgs {
workspaceFolder: string;
command: string[];
Expand All @@ -237,14 +252,14 @@ export interface DevContainerCliExecArgs {
async function devContainerExec(
args: DevContainerCliExecArgs,
log: (data: string) => void,
): Promise<DevContainerCliExecResult | DevContainerCliError> {
): Promise<number | null> {
// const remoteEnvArgs = args.env ? args.env.flatMap(e=> ["--remote-env", e]): []; // TODO - test flatMap again
const remoteEnvArgs = getRemoteEnvArray(args.env);
const commandArgs = ["exec", "--workspace-folder", args.workspaceFolder, ...remoteEnvArgs, ...args.command];
if (args.userDataFolder) {
commandArgs.push("--user-data-folder", args.userDataFolder);
}
return await runSpecCli<DevContainerCliExecResult>({
return await runSpecCliNonJsonCommand({
args: commandArgs,
log,
env: {DOCKER_BUILDKIT: '1', COMPOSE_DOCKER_CLI_BUILD: '1'},
Expand Down
15 changes: 7 additions & 8 deletions github-action/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,22 @@ export async function runMain(): Promise<void> {
execLogString += message;
}
};
const result = await devcontainer.exec(args, execLog);
if (result.outcome !== 'success') {
core.error(
`Dev container exec: ${result.message} (exit code: ${result.code})\n${result.description}`,
);
core.setFailed(result.message);
const exitCode = await devcontainer.exec(args, execLog);
if (exitCode !== 0) {
const errorMessage = `Dev container exec failed: (exit code: ${exitCode})`;
core.error(errorMessage);
core.setFailed(errorMessage);
}
core.setOutput('runCmdOutput', execLogString);
if (Buffer.byteLength(execLogString, 'utf-8') > 1000000) {
execLogString = truncate(execLogString, 999966);
execLogString += 'TRUNCATED TO 1 MB MAX OUTPUT SIZE';
}
core.setOutput('runCmdOutput', execLogString);
return result;
return exitCode;
},
);
if (execResult.outcome !== 'success') {
if (execResult !== 0) {
return;
}
} else {
Expand Down

0 comments on commit 98a823d

Please sign in to comment.