Skip to content

Commit

Permalink
Merge pull request #71 from crazy-max/standalone-mode
Browse files Browse the repository at this point in the history
Standalone mode
  • Loading branch information
crazy-max authored May 5, 2022
2 parents 5595f90 + 9c6bfbc commit ca6596a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 16 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,23 @@ jobs:
name: Dump context
if: always()
uses: crazy-max/ghaction-dump-context@v1

standalone:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Uninstall moby cli
run: |
sudo apt-get purge -y moby-cli moby-buildx
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Build
uses: ./
with:
files: |
./test/config.hcl
11 changes: 11 additions & 0 deletions __tests__/buildx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ describe('isAvailable', () => {
});
});

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

// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`buildx`, [], {
silent: true,
ignoreReturnCode: true
});
});

describe('getVersion', () => {
it('valid', async () => {
const version = await buildx.getVersion();
Expand Down
16 changes: 16 additions & 0 deletions __tests__/docker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {describe, expect, it, jest} from '@jest/globals';
import * as docker from '../src/docker';
import * as exec from '@actions/exec';

describe('isAvailable', () => {
it('cli', () => {
const execSpy = jest.spyOn(exec, 'getExecOutput');
docker.isAvailable();

// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
silent: true,
ignoreReturnCode: true
});
});
});
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions src/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export async function getMetadata(): Promise<string | undefined> {
return content;
}

export async function isAvailable(): Promise<boolean> {
export async function isAvailable(standalone?: boolean): Promise<boolean> {
const cmd = getCommand([], standalone);
return await exec
.getExecOutput('docker', ['buildx'], {
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
Expand All @@ -32,12 +33,17 @@ export async function isAvailable(): Promise<boolean> {
return false;
}
return res.exitCode == 0;
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
return false;
});
}

export async function getVersion(): Promise<string> {
export async function getVersion(standalone?: boolean): Promise<string> {
const cmd = getCommand(['version'], standalone);
return await exec
.getExecOutput('docker', ['buildx', 'version'], {
.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
Expand All @@ -60,3 +66,10 @@ export function parseVersion(stdout: string): string {
export function satisfies(version: string, range: string): boolean {
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
}

export function getCommand(args: Array<string>, standalone?: boolean) {
return {
command: standalone ? 'buildx' : 'docker',
args: standalone ? args : ['buildx', ...args]
};
}
1 change: 0 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export async function getInputs(): Promise<Inputs> {
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
// prettier-ignore
return [
'buildx',
...await getBakeArgs(inputs, buildxVersion),
...await getCommonArgs(inputs),
...inputs.targets
Expand Down
19 changes: 19 additions & 0 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as exec from '@actions/exec';

export async function isAvailable(): Promise<boolean> {
return await exec
.getExecOutput('docker', undefined, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.catch(error => {
return false;
});
}
37 changes: 29 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
import * as fs from 'fs';
import * as buildx from './buildx';
import * as context from './context';
import * as docker from './docker';
import * as stateHelper from './state-helper';
import * as core from '@actions/core';
import * as exec from '@actions/exec';

async function run(): Promise<void> {
try {
const inputs: context.Inputs = await context.getInputs();

// standalone if docker cli not available
const standalone = !(await docker.isAvailable());

core.startGroup(`Docker info`);
await exec.exec('docker', ['version']);
await exec.exec('docker', ['info']);
if (standalone) {
core.info(`Docker info skipped in standalone mode`);
} else {
await exec.exec('docker', ['version'], {
failOnStdErr: false
});
await exec.exec('docker', ['info'], {
failOnStdErr: false
});
}
core.endGroup();

if (!(await buildx.isAvailable())) {
if (!(await buildx.isAvailable(standalone))) {
core.setFailed(`Docker buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
return;
}
stateHelper.setTmpDir(context.tmpDir());

const bxVersion = await buildx.getVersion();
const inputs: context.Inputs = await context.getInputs();
const args: string[] = await context.getArgs(inputs, bxVersion);
const buildxVersion = await buildx.getVersion(standalone);
await core.group(`Buildx version`, async () => {
const versionCmd = buildx.getCommand(['version'], standalone);
await exec.exec(versionCmd.command, versionCmd.args, {
failOnStdErr: false
});
});

const args: string[] = await context.getArgs(inputs, buildxVersion);
const buildCmd = buildx.getCommand(args, standalone);

core.startGroup(`Bake definition`);
await exec.exec('docker', [...args, '--print']);
await exec.exec(buildCmd.command, [...buildCmd.args, '--print']);
core.endGroup();

await exec
.getExecOutput('docker', args, {
.getExecOutput(buildCmd.command, buildCmd.args, {
ignoreReturnCode: true
})
.then(res => {
Expand Down

0 comments on commit ca6596a

Please sign in to comment.