Skip to content

Commit

Permalink
fix: capture the output for stdout of docker cp and date
Browse files Browse the repository at this point in the history
Signed-off-by: Amin Yahyaabadi <[email protected]>
  • Loading branch information
aminya committed Mar 30, 2024
1 parent a6ad893 commit a04f0ef
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
16 changes: 10 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/extract-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { run } from './run.js';

async function extractCache(cacheSource: string, cacheTarget: string, scratchDir: string) {
// Prepare Timestamp for Layer Cache Busting
const { stdout: date } = await run('date', ['--iso=ns']);
const { stdout: date } = await run('date', ['--iso=ns'], true);
await fs.writeFile(path.join(scratchDir, 'buildstamp'), date);

// Prepare Dancefile to Access Caches
Expand All @@ -31,7 +31,7 @@ RUN --mount=type=cache,target=${cacheTarget} \
await run('docker', ['create', '-ti', '--name', 'cache-container', 'dance:extract']);

// Unpack Docker Image into Scratch
const { stdout: tarOutput } = await run('docker', ['cp', '-L', 'cache-container:/var/dance-cache', '-']);
const { stdout: tarOutput } = await run('docker', ['cp', '-L', 'cache-container:/var/dance-cache', '-'], true);
await fs.writeFile(path.join(scratchDir, 'dance-cache.tar'), tarOutput);
await run('tar', ['-H', 'posix', '-x', '-C', scratchDir, '-f', path.join(scratchDir, 'dance-cache.tar')]);

Expand Down
2 changes: 1 addition & 1 deletion src/inject-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function injectCache(cacheSource: string, cacheTarget: string, scratchDir:
await fs.mkdir(cacheSource, { recursive: true });

// Prepare Timestamp for Layer Cache Busting
const { stdout: date } = await run('date', ['--iso=ns']);
const { stdout: date } = await run('date', ['--iso=ns'], true);
await fs.writeFile(path.join(cacheSource, 'buildstamp'), date);

// Prepare Dancefile to Access Caches
Expand Down
7 changes: 5 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import spawn from 'spawn-please'

export async function run(command: string, args: string[]) {
export async function run(command: string, args: string[], captureOutput = true) {
try {
return await spawn(command, args, {}, { stdout: 'inherit', stderr: 'inherit' });
const spawnOpts = captureOutput ?
{ stdout: 'pipe', stderr: 'pipe', } :
{ stdout: 'inherit', stderr: 'inherit' };
return await spawn(command, args, {}, spawnOpts);
} catch (error) {
console.error(`Error running command: ${command} ${args.join(' ')}`);
throw error;
Expand Down

0 comments on commit a04f0ef

Please sign in to comment.