Standalone mode
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
20
.github/workflows/ci.yml
vendored
20
.github/workflows/ci.yml
vendored
@@ -106,3 +106,23 @@ jobs:
|
|||||||
name: Dump context
|
name: Dump context
|
||||||
if: always()
|
if: always()
|
||||||
uses: crazy-max/ghaction-dump-context@v1
|
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
|
||||||
|
|||||||
@@ -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', () => {
|
describe('getVersion', () => {
|
||||||
it('valid', async () => {
|
it('valid', async () => {
|
||||||
const version = await buildx.getVersion();
|
const version = await buildx.getVersion();
|
||||||
|
|||||||
16
__tests__/docker.test.ts
Normal file
16
__tests__/docker.test.ts
Normal file
@@ -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
dist/index.js
generated
vendored
4
dist/index.js
generated
vendored
File diff suppressed because one or more lines are too long
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
@@ -21,9 +21,10 @@ export async function getMetadata(): Promise<string | undefined> {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isAvailable(): Promise<boolean> {
|
export async function isAvailable(standalone?: boolean): Promise<boolean> {
|
||||||
|
const cmd = getCommand([], standalone);
|
||||||
return await exec
|
return await exec
|
||||||
.getExecOutput('docker', ['buildx'], {
|
.getExecOutput(cmd.command, cmd.args, {
|
||||||
ignoreReturnCode: true,
|
ignoreReturnCode: true,
|
||||||
silent: true
|
silent: true
|
||||||
})
|
})
|
||||||
@@ -32,12 +33,17 @@ export async function isAvailable(): Promise<boolean> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return res.exitCode == 0;
|
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
|
return await exec
|
||||||
.getExecOutput('docker', ['buildx', 'version'], {
|
.getExecOutput(cmd.command, cmd.args, {
|
||||||
ignoreReturnCode: true,
|
ignoreReturnCode: true,
|
||||||
silent: true
|
silent: true
|
||||||
})
|
})
|
||||||
@@ -60,3 +66,10 @@ export function parseVersion(stdout: string): string {
|
|||||||
export function satisfies(version: string, range: string): boolean {
|
export function satisfies(version: string, range: string): boolean {
|
||||||
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
|
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]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,7 +47,6 @@ export async function getInputs(): Promise<Inputs> {
|
|||||||
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
|
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
return [
|
return [
|
||||||
'buildx',
|
|
||||||
...await getBakeArgs(inputs, buildxVersion),
|
...await getBakeArgs(inputs, buildxVersion),
|
||||||
...await getCommonArgs(inputs),
|
...await getCommonArgs(inputs),
|
||||||
...inputs.targets
|
...inputs.targets
|
||||||
|
|||||||
19
src/docker.ts
Normal file
19
src/docker.ts
Normal file
@@ -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
src/main.ts
37
src/main.ts
@@ -1,33 +1,54 @@
|
|||||||
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 docker from './docker';
|
||||||
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';
|
import * as exec from '@actions/exec';
|
||||||
|
|
||||||
async function run(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
const inputs: context.Inputs = await context.getInputs();
|
||||||
|
|
||||||
|
// standalone if docker cli not available
|
||||||
|
const standalone = !(await docker.isAvailable());
|
||||||
|
|
||||||
core.startGroup(`Docker info`);
|
core.startGroup(`Docker info`);
|
||||||
await exec.exec('docker', ['version']);
|
if (standalone) {
|
||||||
await exec.exec('docker', ['info']);
|
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();
|
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.`);
|
core.setFailed(`Docker buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stateHelper.setTmpDir(context.tmpDir());
|
stateHelper.setTmpDir(context.tmpDir());
|
||||||
|
|
||||||
const bxVersion = await buildx.getVersion();
|
const buildxVersion = await buildx.getVersion(standalone);
|
||||||
const inputs: context.Inputs = await context.getInputs();
|
await core.group(`Buildx version`, async () => {
|
||||||
const args: string[] = await context.getArgs(inputs, bxVersion);
|
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`);
|
core.startGroup(`Bake definition`);
|
||||||
await exec.exec('docker', [...args, '--print']);
|
await exec.exec(buildCmd.command, [...buildCmd.args, '--print']);
|
||||||
core.endGroup();
|
core.endGroup();
|
||||||
|
|
||||||
await exec
|
await exec
|
||||||
.getExecOutput('docker', args, {
|
.getExecOutput(buildCmd.command, buildCmd.args, {
|
||||||
ignoreReturnCode: true
|
ignoreReturnCode: true
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
|||||||
Reference in New Issue
Block a user