Use built-in `getExecOutput`

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
pull/392/head
CrazyMax 4 years ago
parent a7071c9d9a
commit a437a4518f
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7

@ -1,10 +1,10 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as exec from '@actions/exec';
import * as buildx from '../src/buildx'; import * as buildx from '../src/buildx';
import * as context from '../src/context'; import * as context from '../src/context';
import * as docker from '../src/docker';
const tmpNameSync = path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep); const tmpNameSync = path.join('/tmp/.docker-build-push-jest', '.tmpname-jest').split(path.sep).join(path.posix.sep);
const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9'; const digest = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
@ -92,9 +92,26 @@ describe('isLocalOrTarExporter', () => {
); );
}); });
describe('isAvailable', () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
buildx.isAvailable();
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true,
ignoreReturnCode: true
});
});
describe('getVersion', () => { describe('getVersion', () => {
async function isDaemonRunning() { async function isDaemonRunning() {
return await docker.isDaemonRunning(); return await exec
.getExecOutput(`docker`, ['version', '--format', '{{.Server.Os}}'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
return !res.stdout.includes(' ') && res.exitCode == 0;
});
} }
(isDaemonRunning() ? it : it.skip)( (isDaemonRunning() ? it : it.skip)(
'valid', 'valid',

1024
dist/index.js generated vendored

File diff suppressed because it is too large Load Diff

@ -2,9 +2,9 @@ import csvparse from 'csv-parse/lib/sync';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as exec from '@actions/exec';
import * as context from './context'; import * as context from './context';
import * as exec from './exec';
export async function getImageIDFile(): Promise<string> { export async function getImageIDFile(): Promise<string> {
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep); return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
@ -80,21 +80,31 @@ export function hasGitAuthToken(secrets: string[]): Boolean {
} }
export async function isAvailable(): Promise<Boolean> { export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => { return await exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', ['buildx'], {
return false; ignoreReturnCode: true,
} silent: true
return res.success; })
}); .then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
});
} }
export async function getVersion(): Promise<string> { export async function getVersion(): Promise<string> {
return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => { return await exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', ['buildx', 'version'], {
throw new Error(res.stderr); ignoreReturnCode: true,
} silent: true
return parseVersion(res.stdout); })
}); .then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return parseVersion(res.stdout);
});
} }
export async function parseVersion(stdout: string): Promise<string> { export async function parseVersion(stdout: string): Promise<string> {

@ -1,7 +0,0 @@
import * as exec from './exec';
export async function isDaemonRunning(): Promise<boolean> {
return await exec.exec(`docker`, ['version', '--format', '{{.Server.Os}}'], true).then(res => {
return !res.stdout.includes(' ') && res.success;
});
}

@ -1,34 +0,0 @@
import * as aexec from '@actions/exec';
import {ExecOptions} from '@actions/exec';
export interface ExecResult {
success: boolean;
stdout: string;
stderr: string;
}
export const exec = async (command: string, args: string[] = [], silent?: boolean): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true
};
options.listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
}
};
const returnCode: number = await aexec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};

@ -1,9 +1,9 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as buildx from './buildx'; import * as buildx from './buildx';
import * as context from './context'; import * as context from './context';
import * as exec from './exec';
import * as stateHelper from './state-helper'; import * as stateHelper from './state-helper';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec';
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
@ -23,11 +23,15 @@ async function run(): Promise<void> {
let inputs: context.Inputs = await context.getInputs(defContext); let inputs: context.Inputs = await context.getInputs(defContext);
const args: string[] = await context.getArgs(inputs, defContext, buildxVersion); const args: string[] = await context.getArgs(inputs, defContext, buildxVersion);
await exec.exec('docker', args).then(res => { await exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', args, {
throw new Error(`buildx call failed with: ${res.stderr.match(/(.*)\s*$/)![0]}`); ignoreReturnCode: true
} })
}); .then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)![0].trim()}`);
}
});
const imageID = await buildx.getImageID(); const imageID = await buildx.getImageID();
if (imageID) { if (imageID) {

Loading…
Cancel
Save