Skip to content

Commit

Permalink
Use built-in getExecOutput
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 23, 2021
1 parent faca783 commit da12839
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 84 deletions.
45 changes: 37 additions & 8 deletions __tests__/buildx.test.ts
@@ -1,18 +1,40 @@
import fs = require('fs');
import * as docker from '../src/docker';
import * as buildx from '../src/buildx';
import * as path from 'path';
import * as os from 'os';
import * as semver from 'semver';
import * as exec from '@actions/exec';

describe('isAvailable', () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
buildx.isAvailable();

expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true,
ignoreReturnCode: true
});
});

describe('getVersion', () => {
it('valid', async () => {
await exec.exec('docker', ['buildx', 'version']);
const version = await buildx.getVersion();
console.log(`version: ${version}`);
expect(semver.valid(version)).not.toBeNull();
}, 100000);
async function 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)(
'valid',
async () => {
const version = await buildx.getVersion();
console.log(`version: ${version}`);
expect(semver.valid(version)).not.toBeNull();
},
100000
);
});

describe('parseVersion', () => {
Expand All @@ -27,7 +49,14 @@ describe('parseVersion', () => {

describe('inspect', () => {
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)(
'valid',
Expand Down
71 changes: 45 additions & 26 deletions src/buildx.ts
Expand Up @@ -3,9 +3,9 @@ import * as path from 'path';
import * as semver from 'semver';
import * as util from 'util';
import * as context from './context';
import * as exec from './exec';
import * as github from './github';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';

export type Builder = {
Expand All @@ -18,36 +18,49 @@ export type Builder = {
node_platforms?: string;
};

export async function isAvailable(): Promise<Boolean> {
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.length > 0 && !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> {
const matches = /\sv?([0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse Buildx version`);
throw new Error(`Cannot parse buildx version`);
}
return semver.clean(matches[1]);
}

export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr.length > 0 && !res.success) {
return false;
}
return res.success;
});
}

export async function inspect(name: string): Promise<Builder> {
return await exec.exec(`docker`, ['buildx', 'inspect', name], true).then(res => {
if (res.stderr.length > 0 && !res.success) {
throw new Error(res.stderr);
return await exec.getExecOutput(`docker`, ['buildx', 'inspect', name], {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
const builder: Builder = {};
itlines: for (const line of res.stdout.trim().split(`\n`)) {
Expand Down Expand Up @@ -173,18 +186,24 @@ async function filename(version: string): Promise<string> {
}

export async function getBuildKitVersion(containerID: string): Promise<string> {
return exec.exec(`docker`, ['inspect', '--format', '{{.Config.Image}}', containerID], true).then(bkitimage => {
if (bkitimage.success && bkitimage.stdout.length > 0) {
return exec.exec(`docker`, ['run', '--rm', bkitimage.stdout, '--version'], true).then(bkitversion => {
if (bkitversion.success && bkitversion.stdout.length > 0) {
return exec.getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', containerID], {
ignoreReturnCode: true,
silent: true
}).then(bkitimage => {
if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) {
return exec.getExecOutput(`docker`, ['run', '--rm', bkitimage.stdout, '--version'], {
ignoreReturnCode: true,
silent: true
}).then(bkitversion => {
if (bkitversion.exitCode == 0 && bkitversion.stdout.length > 0) {
return `${bkitimage.stdout} => ${bkitversion.stdout}`;
} else if (bkitversion.stderr.length > 0) {
core.warning(bkitversion.stderr);
core.warning(bkitversion.stderr.trim());
}
return bkitversion.stdout;
});
} else if (bkitimage.stderr.length > 0) {
core.warning(bkitimage.stderr);
core.warning(bkitimage.stderr.trim());
}
return bkitimage.stdout;
});
Expand Down
7 changes: 0 additions & 7 deletions src/docker.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/exec.ts

This file was deleted.

21 changes: 12 additions & 9 deletions src/main.ts
@@ -1,12 +1,11 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as buildx from './buildx';
import * as context from './context';
import * as mexec 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 {
Expand Down Expand Up @@ -94,19 +93,23 @@ async function run(): Promise<void> {
async function cleanup(): Promise<void> {
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
core.startGroup(`BuildKit container logs`);
await mexec.exec('docker', ['logs', `${stateHelper.containerName}`], false).then(res => {
if (res.stderr.length > 0 && !res.success) {
core.warning(res.stderr);
await exec.getExecOutput('docker', ['logs', `${stateHelper.containerName}`], {
ignoreReturnCode: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
core.endGroup();
}

if (stateHelper.builderName.length > 0) {
core.startGroup(`Removing builder`);
await mexec.exec('docker', ['buildx', 'rm', `${stateHelper.builderName}`], false).then(res => {
if (res.stderr.length > 0 && !res.success) {
core.warning(res.stderr);
await exec.getExecOutput('docker', ['buildx', 'rm', `${stateHelper.builderName}`], {
ignoreReturnCode: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
core.endGroup();
Expand Down

0 comments on commit da12839

Please sign in to comment.