Skip to content

Commit ca6596a

Browse files
authored
Merge pull request #71 from crazy-max/standalone-mode
Standalone mode
2 parents 5595f90 + 9c6bfbc commit ca6596a

File tree

9 files changed

+115
-16
lines changed

9 files changed

+115
-16
lines changed

.github/workflows/ci.yml

+20
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,23 @@ jobs:
106106
name: Dump context
107107
if: always()
108108
uses: crazy-max/ghaction-dump-context@v1
109+
110+
standalone:
111+
runs-on: ubuntu-latest
112+
steps:
113+
-
114+
name: Checkout
115+
uses: actions/checkout@v3
116+
-
117+
name: Uninstall moby cli
118+
run: |
119+
sudo apt-get purge -y moby-cli moby-buildx
120+
-
121+
name: Set up Docker Buildx
122+
uses: docker/setup-buildx-action@v1
123+
-
124+
name: Build
125+
uses: ./
126+
with:
127+
files: |
128+
./test/config.hcl

__tests__/buildx.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ describe('isAvailable', () => {
4444
});
4545
});
4646

47+
describe('isAvailable standalone', () => {
48+
const execSpy = jest.spyOn(exec, 'getExecOutput');
49+
buildx.isAvailable(true);
50+
51+
// eslint-disable-next-line jest/no-standalone-expect
52+
expect(execSpy).toHaveBeenCalledWith(`buildx`, [], {
53+
silent: true,
54+
ignoreReturnCode: true
55+
});
56+
});
57+
4758
describe('getVersion', () => {
4859
it('valid', async () => {
4960
const version = await buildx.getVersion();

__tests__/docker.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {describe, expect, it, jest} from '@jest/globals';
2+
import * as docker from '../src/docker';
3+
import * as exec from '@actions/exec';
4+
5+
describe('isAvailable', () => {
6+
it('cli', () => {
7+
const execSpy = jest.spyOn(exec, 'getExecOutput');
8+
docker.isAvailable();
9+
10+
// eslint-disable-next-line jest/no-standalone-expect
11+
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
12+
silent: true,
13+
ignoreReturnCode: true
14+
});
15+
});
16+
});

dist/index.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/buildx.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ export async function getMetadata(): Promise<string | undefined> {
2121
return content;
2222
}
2323

24-
export async function isAvailable(): Promise<boolean> {
24+
export async function isAvailable(standalone?: boolean): Promise<boolean> {
25+
const cmd = getCommand([], standalone);
2526
return await exec
26-
.getExecOutput('docker', ['buildx'], {
27+
.getExecOutput(cmd.command, cmd.args, {
2728
ignoreReturnCode: true,
2829
silent: true
2930
})
@@ -32,12 +33,17 @@ export async function isAvailable(): Promise<boolean> {
3233
return false;
3334
}
3435
return res.exitCode == 0;
36+
})
37+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
38+
.catch(error => {
39+
return false;
3540
});
3641
}
3742

38-
export async function getVersion(): Promise<string> {
43+
export async function getVersion(standalone?: boolean): Promise<string> {
44+
const cmd = getCommand(['version'], standalone);
3945
return await exec
40-
.getExecOutput('docker', ['buildx', 'version'], {
46+
.getExecOutput(cmd.command, cmd.args, {
4147
ignoreReturnCode: true,
4248
silent: true
4349
})
@@ -60,3 +66,10 @@ export function parseVersion(stdout: string): string {
6066
export function satisfies(version: string, range: string): boolean {
6167
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
6268
}
69+
70+
export function getCommand(args: Array<string>, standalone?: boolean) {
71+
return {
72+
command: standalone ? 'buildx' : 'docker',
73+
args: standalone ? args : ['buildx', ...args]
74+
};
75+
}

src/context.ts

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export async function getInputs(): Promise<Inputs> {
4747
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
4848
// prettier-ignore
4949
return [
50-
'buildx',
5150
...await getBakeArgs(inputs, buildxVersion),
5251
...await getCommonArgs(inputs),
5352
...inputs.targets

src/docker.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as exec from '@actions/exec';
2+
3+
export async function isAvailable(): Promise<boolean> {
4+
return await exec
5+
.getExecOutput('docker', undefined, {
6+
ignoreReturnCode: true,
7+
silent: true
8+
})
9+
.then(res => {
10+
if (res.stderr.length > 0 && res.exitCode != 0) {
11+
return false;
12+
}
13+
return res.exitCode == 0;
14+
})
15+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
16+
.catch(error => {
17+
return false;
18+
});
19+
}

src/main.ts

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,54 @@
11
import * as fs from 'fs';
22
import * as buildx from './buildx';
33
import * as context from './context';
4+
import * as docker from './docker';
45
import * as stateHelper from './state-helper';
56
import * as core from '@actions/core';
67
import * as exec from '@actions/exec';
78

89
async function run(): Promise<void> {
910
try {
11+
const inputs: context.Inputs = await context.getInputs();
12+
13+
// standalone if docker cli not available
14+
const standalone = !(await docker.isAvailable());
15+
1016
core.startGroup(`Docker info`);
11-
await exec.exec('docker', ['version']);
12-
await exec.exec('docker', ['info']);
17+
if (standalone) {
18+
core.info(`Docker info skipped in standalone mode`);
19+
} else {
20+
await exec.exec('docker', ['version'], {
21+
failOnStdErr: false
22+
});
23+
await exec.exec('docker', ['info'], {
24+
failOnStdErr: false
25+
});
26+
}
1327
core.endGroup();
1428

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

21-
const bxVersion = await buildx.getVersion();
22-
const inputs: context.Inputs = await context.getInputs();
23-
const args: string[] = await context.getArgs(inputs, bxVersion);
35+
const buildxVersion = await buildx.getVersion(standalone);
36+
await core.group(`Buildx version`, async () => {
37+
const versionCmd = buildx.getCommand(['version'], standalone);
38+
await exec.exec(versionCmd.command, versionCmd.args, {
39+
failOnStdErr: false
40+
});
41+
});
42+
43+
const args: string[] = await context.getArgs(inputs, buildxVersion);
44+
const buildCmd = buildx.getCommand(args, standalone);
2445

2546
core.startGroup(`Bake definition`);
26-
await exec.exec('docker', [...args, '--print']);
47+
await exec.exec(buildCmd.command, [...buildCmd.args, '--print']);
2748
core.endGroup();
2849

2950
await exec
30-
.getExecOutput('docker', args, {
51+
.getExecOutput(buildCmd.command, buildCmd.args, {
3152
ignoreReturnCode: true
3253
})
3354
.then(res => {

0 commit comments

Comments
 (0)