mirror of
https://github.com/docker/build-push-action.git
synced 2025-04-19 01:46:45 +00:00
Use built-in getExecOutput
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
a7071c9d9a
commit
a437a4518f
6 changed files with 415 additions and 473 deletions
|
@ -2,9 +2,9 @@ import csvparse from 'csv-parse/lib/sync';
|
|||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import * as semver from 'semver';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
import * as context from './context';
|
||||
import * as exec from './exec';
|
||||
|
||||
export async function getImageIDFile(): Promise<string> {
|
||||
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> {
|
||||
return await exec.exec(`docker`, ['buildx'], true).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
return false;
|
||||
}
|
||||
return res.success;
|
||||
});
|
||||
return await exec
|
||||
.getExecOutput('docker', ['buildx'], {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||
return false;
|
||||
}
|
||||
return res.exitCode == 0;
|
||||
});
|
||||
}
|
||||
|
||||
export async function getVersion(): Promise<string> {
|
||||
return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
throw new Error(res.stderr);
|
||||
}
|
||||
return parseVersion(res.stdout);
|
||||
});
|
||||
return await exec
|
||||
.getExecOutput('docker', ['buildx', 'version'], {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.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> {
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
}
|
34
src/exec.ts
34
src/exec.ts
|
@ -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()
|
||||
};
|
||||
};
|
16
src/main.ts
16
src/main.ts
|
@ -1,9 +1,9 @@
|
|||
import * as fs from 'fs';
|
||||
import * as buildx from './buildx';
|
||||
import * as context from './context';
|
||||
import * as exec from './exec';
|
||||
import * as stateHelper from './state-helper';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
|
@ -23,11 +23,15 @@ async function run(): Promise<void> {
|
|||
let inputs: context.Inputs = await context.getInputs(defContext);
|
||||
|
||||
const args: string[] = await context.getArgs(inputs, defContext, buildxVersion);
|
||||
await exec.exec('docker', args).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
throw new Error(`buildx call failed with: ${res.stderr.match(/(.*)\s*$/)![0]}`);
|
||||
}
|
||||
});
|
||||
await exec
|
||||
.getExecOutput('docker', args, {
|
||||
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();
|
||||
if (imageID) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue