1
0
Fork 0
mirror of https://github.com/docker/setup-buildx-action.git synced 2024-12-22 15:02:23 +00:00

Check buildx version

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-09-03 21:02:36 +02:00
parent 4285b9db58
commit 0be6f6575b
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
6 changed files with 83 additions and 25 deletions

View file

@ -22,7 +22,7 @@ jobs:
matrix:
buildx-version:
- latest
- v0.4.1
- v0.2.2
- ""
steps:
-
@ -150,7 +150,7 @@ jobs:
matrix:
buildx-version:
- latest
- v0.4.1
- v0.2.2
- ""
qemu-platforms:
- all

View file

@ -131,7 +131,7 @@ Following inputs can be used as `step.with` keys
| `version` | String | [Buildx](https://github.com/docker/buildx) version. (e.g. `v0.3.0`, `latest`) |
| `driver` | String | Sets the [builder driver](https://github.com/docker/buildx#--driver-driver) to be used (default `docker-container`) |
| `driver-opts` | CSV | List of additional [driver-specific options](https://github.com/docker/buildx#--driver-opt-options) |
| `buildkitd-flags` | String | [Flags for buildkitd](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) daemon (default `--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host`) |
| `buildkitd-flags` | String | [Flags for buildkitd](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) daemon (since [buildx v0.3.0](https://github.com/docker/buildx/releases/tag/v0.3.0)) |
| `install` | Bool | Sets up `docker build` command as an alias to `docker buildx` (default `false`) |
| `use` | Bool | Switch to this builder instance (default `true`) |

View file

@ -3,26 +3,34 @@ 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';
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
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);
});
describe('buildx', () => {
describe('parseVersion', () => {
test.each([
['github.com/docker/buildx 0.4.1+azure bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.1 bda4882a65349ca359216b135896bddc1d92461c', '0.4.1'],
['github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2', '0.4.2']
])('given %p', async (stdout, expected) => {
expect(await buildx.parseVersion(stdout)).toEqual(expected);
});
});
describe('platforms', () => {
async function isDaemonRunning() {
return await docker.isDaemonRunning();
}
it('is available', async () => {
expect(await buildx.isAvailable()).toBe(true);
}, 100000);
it('count builders', async () => {
const countBuilders = await buildx.countBuilders();
console.log(`countBuilders: ${countBuilders}`);
expect(countBuilders).toBeGreaterThan(0);
}, 100000);
(isDaemonRunning() ? it : it.skip)(
'platforms',
'valid',
async () => {
const platforms = buildx.platforms();
console.log(`platforms: ${platforms}`);
@ -31,13 +39,23 @@ describe('buildx', () => {
},
100000
);
});
describe('countBuilders', () => {
it('valid', async () => {
const countBuilders = await buildx.countBuilders();
console.log(`countBuilders: ${countBuilders}`);
expect(countBuilders).toBeGreaterThan(0);
}, 100000);
});
describe('install', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
it('acquires v0.2.2 version of buildx', async () => {
const buildxBin = await buildx.install('v0.2.2', tmpDir);
console.log(buildxBin);
expect(fs.existsSync(buildxBin)).toBe(true);
}, 100000);
it('acquires latest version of buildx', async () => {
const buildxBin = await buildx.install('latest', tmpDir);
console.log(buildxBin);

30
dist/index.js generated vendored
View file

@ -500,6 +500,7 @@ const core = __importStar(__webpack_require__(186));
const exec = __importStar(__webpack_require__(514));
const os = __importStar(__webpack_require__(87));
const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(383));
const buildx = __importStar(__webpack_require__(295));
const context = __importStar(__webpack_require__(842));
const mexec = __importStar(__webpack_require__(757));
@ -516,8 +517,8 @@ function run() {
if (!(yield buildx.isAvailable()) || inputs.version) {
yield buildx.install(inputs.version || 'latest', dockerConfigHome);
}
core.info('📣 Buildx info');
yield exec.exec('docker', ['buildx', 'version']);
const buildxVersion = yield buildx.getVersion();
core.info(`📣 Buildx version: ${buildxVersion}`);
const builderName = inputs.driver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(yield buildx.countBuilders()) + 1}`;
core.setOutput('name', builderName);
stateHelper.setBuilderName(builderName);
@ -527,7 +528,7 @@ function run() {
yield context.asyncForEach(inputs.driverOpts, (driverOpt) => __awaiter(this, void 0, void 0, function* () {
createArgs.push('--driver-opt', driverOpt);
}));
if (inputs.buildkitdFlags) {
if (inputs.buildkitdFlags && semver.satisfies(buildxVersion, '>=0.3.0')) {
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
}
if (inputs.use) {
@ -1936,7 +1937,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.install = exports.platforms = exports.countBuilders = exports.isAvailable = void 0;
exports.install = exports.platforms = exports.countBuilders = exports.isAvailable = exports.parseVersion = exports.getVersion = void 0;
const fs = __importStar(__webpack_require__(747));
const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(383));
@ -1946,6 +1947,27 @@ const exec = __importStar(__webpack_require__(757));
const github = __importStar(__webpack_require__(928));
const core = __importStar(__webpack_require__(186));
const tc = __importStar(__webpack_require__(784));
function getVersion() {
return __awaiter(this, void 0, void 0, function* () {
return yield exec.exec(`docker`, ['buildx', 'version'], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
return parseVersion(res.stdout);
});
});
}
exports.getVersion = getVersion;
function parseVersion(stdout) {
return __awaiter(this, void 0, void 0, function* () {
const matches = /\sv?([0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse Buildx version`);
}
return semver.clean(matches[1]);
});
}
exports.parseVersion = parseVersion;
function isAvailable() {
return __awaiter(this, void 0, void 0, function* () {
return yield exec.exec(`docker`, ['buildx'], true).then(res => {

View file

@ -8,6 +8,23 @@ import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
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);
});
}
export async function parseVersion(stdout: string): Promise<string> {
const matches = /\sv?([0-9.]+)/.exec(stdout);
if (!matches) {
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 != '' && !res.success) {

View file

@ -2,6 +2,7 @@ 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';
@ -21,8 +22,8 @@ async function run(): Promise<void> {
await buildx.install(inputs.version || 'latest', dockerConfigHome);
}
core.info('📣 Buildx info');
await exec.exec('docker', ['buildx', 'version']);
const buildxVersion = await buildx.getVersion();
core.info(`📣 Buildx version: ${buildxVersion}`);
const builderName: string =
inputs.driver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(await buildx.countBuilders()) + 1}`;
@ -35,7 +36,7 @@ async function run(): Promise<void> {
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
createArgs.push('--driver-opt', driverOpt);
});
if (inputs.buildkitdFlags) {
if (inputs.buildkitdFlags && semver.satisfies(buildxVersion, '>=0.3.0')) {
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
}
if (inputs.use) {