Merge pull request #14 from crazy-max/array-driver-opt

driver-opt as array of inputs (renamed driver-opts)
This commit is contained in:
Tõnis Tiigi 2020-09-03 09:00:55 -07:00 committed by GitHub
commit 5636be6c3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 242 additions and 58 deletions

View File

@ -107,9 +107,11 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
driver-opt: driver-opts:
- image=moby/buildkit:latest - image=moby/buildkit:latest
- image=moby/buildkit:master - |
image=moby/buildkit:master
network=host
steps: steps:
- -
name: Checkout name: Checkout
@ -119,7 +121,7 @@ jobs:
uses: ./ uses: ./
with: with:
driver: docker-container driver: docker-container
driver-opt: ${{ matrix.driver-opt }} driver-opts: ${{ matrix.driver-opts }}
docker-driver: docker-driver:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -130,11 +130,21 @@ Following inputs can be used as `step.with` keys
|--------------------|---------|-----------------------------------| |--------------------|---------|-----------------------------------|
| `version` | String | [Buildx](https://github.com/docker/buildx) version. (e.g. `v0.3.0`, `latest`) | | `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` | String | Sets the [builder driver](https://github.com/docker/buildx#--driver-driver) to be used (default `docker-container`) |
| `driver-opt` | String | Passes additional [driver-specific options](https://github.com/docker/buildx#--driver-opt-options) | | `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 | | `buildkitd-flags` | String | [Flags for buildkitd](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md) daemon |
| `install` | Bool | Sets up `docker build` command as an alias to `docker buildx` (default `false`) | | `install` | Bool | Sets up `docker build` command as an alias to `docker buildx` (default `false`) |
| `use` | Bool | Switch to this builder instance (default `true`) | | `use` | Bool | Switch to this builder instance (default `true`) |
> `CSV` type must be a newline-delimited string
> ```yaml
> driver-opts: image=moby/buildkit:master
> ```
> ```yaml
> driver-opts: |
> image=moby/buildkit:master
> network=host
> ```
### outputs ### outputs
Following outputs are available Following outputs are available

74
__tests__/context.test.ts Normal file
View File

@ -0,0 +1,74 @@
import * as context from '../src/context';
describe('getInputList', () => {
it('handles single line correctly', async () => {
await setInput('foo', 'bar');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar']);
});
it('handles multiple lines correctly', async () => {
setInput('foo', 'bar\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles comma correctly', async () => {
setInput('foo', 'bar,baz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines correctly', async () => {
setInput('foo', 'bar\r\nbaz');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz']);
});
it('handles different new lines and comma correctly', async () => {
setInput('foo', 'bar\r\nbaz,bat');
const res = await context.getInputList('foo');
console.log(res);
expect(res).toEqual(['bar', 'baz', 'bat']);
});
it('handles multiple lines and ignoring comma correctly', async () => {
setInput('driver-opts', 'image=moby/buildkit:master\nnetwork=host');
const res = await context.getInputList('driver-opts', true);
console.log(res);
expect(res).toEqual(['image=moby/buildkit:master', 'network=host']);
});
it('handles different new lines and ignoring comma correctly', async () => {
setInput('driver-opts', 'image=moby/buildkit:master\r\nnetwork=host');
const res = await context.getInputList('driver-opts', true);
console.log(res);
expect(res).toEqual(['image=moby/buildkit:master', 'network=host']);
});
});
describe('asyncForEach', () => {
it('executes async tasks sequentially', async () => {
const testValues = [1, 2, 3, 4, 5];
const results: number[] = [];
await context.asyncForEach(testValues, async value => {
results.push(value);
});
expect(results).toEqual(testValues);
});
});
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
}
function setInput(name: string, value: string): void {
process.env[getInputName(name)] = value;
}

View File

@ -14,8 +14,8 @@ inputs:
description: 'Sets the builder driver to be used' description: 'Sets the builder driver to be used'
default: 'docker-container' default: 'docker-container'
required: false required: false
driver-opt: driver-opts:
description: 'Passes additional driver-specific options. Eg. image=moby/buildkit:master' description: 'List of additional driver-specific options. Eg. image=moby/buildkit:master'
required: false required: false
buildkitd-flags: buildkitd-flags:
description: 'Flags for buildkitd daemon' description: 'Flags for buildkitd daemon'

115
dist/index.js generated vendored
View File

@ -501,6 +501,7 @@ const exec = __importStar(__webpack_require__(514));
const os = __importStar(__webpack_require__(87)); const os = __importStar(__webpack_require__(87));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const buildx = __importStar(__webpack_require__(295)); const buildx = __importStar(__webpack_require__(295));
const context = __importStar(__webpack_require__(842));
const mexec = __importStar(__webpack_require__(757)); const mexec = __importStar(__webpack_require__(757));
const stateHelper = __importStar(__webpack_require__(647)); const stateHelper = __importStar(__webpack_require__(647));
function run() { function run() {
@ -510,38 +511,33 @@ function run() {
core.setFailed('Only supported on linux platform'); core.setFailed('Only supported on linux platform');
return; return;
} }
const bxVersion = core.getInput('version'); const inputs = yield context.getInputs();
const bxDriver = core.getInput('driver') || 'docker-container';
const bxDriverOpt = core.getInput('driver-opt');
const bxBuildkitdFlags = core.getInput('buildkitd-flags');
const bxInstall = /true/i.test(core.getInput('install'));
const bxUse = /true/i.test(core.getInput('use'));
const dockerConfigHome = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); const dockerConfigHome = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
if (!(yield buildx.isAvailable()) || bxVersion) { if (!(yield buildx.isAvailable()) || inputs.version) {
yield buildx.install(bxVersion || 'latest', dockerConfigHome); yield buildx.install(inputs.version || 'latest', dockerConfigHome);
} }
core.info('📣 Buildx info'); core.info('📣 Buildx info');
yield exec.exec('docker', ['buildx', 'version']); yield exec.exec('docker', ['buildx', 'version']);
const builderName = bxDriver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(yield buildx.countBuilders()) + 1}`; const builderName = inputs.driver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(yield buildx.countBuilders()) + 1}`;
core.setOutput('name', builderName); core.setOutput('name', builderName);
stateHelper.setBuilderName(builderName); stateHelper.setBuilderName(builderName);
if (bxDriver != 'docker') { if (inputs.driver !== 'docker') {
core.info('🔨 Creating a new builder instance...'); core.info('🔨 Creating a new builder instance...');
let createArgs = ['buildx', 'create', '--name', builderName, '--driver', bxDriver]; let createArgs = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
if (bxDriverOpt) { yield context.asyncForEach(inputs.driverOpts, (driverOpt) => __awaiter(this, void 0, void 0, function* () {
createArgs.push('--driver-opt', bxDriverOpt); createArgs.push('--driver-opt', driverOpt);
}));
if (inputs.buildkitdFlags) {
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
} }
if (bxBuildkitdFlags) { if (inputs.use) {
createArgs.push('--buildkitd-flags', bxBuildkitdFlags);
}
if (bxUse) {
createArgs.push('--use'); createArgs.push('--use');
} }
yield exec.exec('docker', createArgs); yield exec.exec('docker', createArgs);
core.info('🏃 Booting builder...'); core.info('🏃 Booting builder...');
yield exec.exec('docker', ['buildx', 'inspect', '--bootstrap']); yield exec.exec('docker', ['buildx', 'inspect', '--bootstrap']);
} }
if (bxInstall) { if (inputs.install) {
core.info('🤝 Setting buildx as default builder...'); core.info('🤝 Setting buildx as default builder...');
yield exec.exec('docker', ['buildx', 'install']); yield exec.exec('docker', ['buildx', 'install']);
} }
@ -1942,15 +1938,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.install = exports.platforms = exports.countBuilders = exports.isAvailable = void 0; exports.install = exports.platforms = exports.countBuilders = exports.isAvailable = void 0;
const fs = __importStar(__webpack_require__(747)); const fs = __importStar(__webpack_require__(747));
const os = __importStar(__webpack_require__(87));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const semver = __importStar(__webpack_require__(383)); const semver = __importStar(__webpack_require__(383));
const util = __importStar(__webpack_require__(669)); const util = __importStar(__webpack_require__(669));
const context = __importStar(__webpack_require__(842));
const exec = __importStar(__webpack_require__(757)); const exec = __importStar(__webpack_require__(757));
const github = __importStar(__webpack_require__(928)); const github = __importStar(__webpack_require__(928));
const core = __importStar(__webpack_require__(186)); const core = __importStar(__webpack_require__(186));
const tc = __importStar(__webpack_require__(784)); const tc = __importStar(__webpack_require__(784));
const osPlat = os.platform();
function isAvailable() { function isAvailable() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
return yield exec.exec(`docker`, ['buildx'], true).then(res => { return yield exec.exec(`docker`, ['buildx'], true).then(res => {
@ -2010,7 +2005,7 @@ function install(inputVersion, dockerConfigHome) {
if (!fs.existsSync(pluginsDir)) { if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, { recursive: true }); fs.mkdirSync(pluginsDir, { recursive: true });
} }
const filename = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const filename = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const pluginPath = path.join(pluginsDir, filename); const pluginPath = path.join(pluginsDir, filename);
core.debug(`Plugin path is ${pluginPath}`); core.debug(`Plugin path is ${pluginPath}`);
fs.copyFileSync(path.join(toolPath, filename), pluginPath); fs.copyFileSync(path.join(toolPath, filename), pluginPath);
@ -2023,10 +2018,10 @@ exports.install = install;
function download(version) { function download(version) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
version = semver.clean(version) || ''; version = semver.clean(version) || '';
const platform = osPlat == 'win32' ? 'windows' : osPlat; const platform = context.osPlat == 'win32' ? 'windows' : context.osPlat;
const ext = osPlat == 'win32' ? '.exe' : ''; const ext = context.osPlat == 'win32' ? '.exe' : '';
const filename = util.format('buildx-v%s.%s-amd64%s', version, platform, ext); const filename = util.format('buildx-v%s.%s-amd64%s', version, platform, ext);
const targetFile = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const targetFile = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, filename); const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, filename);
let downloadPath; let downloadPath;
try { try {
@ -6489,6 +6484,78 @@ module.exports = require("url");
/***/ }), /***/ }),
/***/ 842:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.asyncForEach = exports.getInputList = exports.getInputs = exports.osPlat = void 0;
const os = __importStar(__webpack_require__(87));
const core = __importStar(__webpack_require__(186));
exports.osPlat = os.platform();
function getInputs() {
return __awaiter(this, void 0, void 0, function* () {
return {
version: core.getInput('version'),
driver: core.getInput('driver') || 'docker-container',
driverOpts: yield getInputList('driver-opts', true),
buildkitdFlags: core.getInput('buildkitd-flags'),
install: /true/i.test(core.getInput('install')),
use: /true/i.test(core.getInput('use'))
};
});
}
exports.getInputs = getInputs;
function getInputList(name, ignoreComma) {
return __awaiter(this, void 0, void 0, function* () {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',') : line).map(pat => pat.trim()), []);
});
}
exports.getInputList = getInputList;
exports.asyncForEach = (array, callback) => __awaiter(void 0, void 0, void 0, function* () {
for (let index = 0; index < array.length; index++) {
yield callback(array[index], index, array);
}
});
//# sourceMappingURL=context.js.map
/***/ }),
/***/ 848: /***/ 848:
/***/ (function(module, __unusedexports, __webpack_require__) { /***/ (function(module, __unusedexports, __webpack_require__) {

View File

@ -1,15 +1,13 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver'; import * as semver from 'semver';
import * as util from 'util'; import * as util from 'util';
import * as context from './context';
import * as exec from './exec'; import * as exec from './exec';
import * as github from './github'; import * as github from './github';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
const osPlat: string = os.platform();
export async function isAvailable(): Promise<Boolean> { export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => { return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) { if (res.stderr != '' && !res.success) {
@ -65,7 +63,7 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
fs.mkdirSync(pluginsDir, {recursive: true}); fs.mkdirSync(pluginsDir, {recursive: true});
} }
const filename: string = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const filename: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const pluginPath: string = path.join(pluginsDir, filename); const pluginPath: string = path.join(pluginsDir, filename);
core.debug(`Plugin path is ${pluginPath}`); core.debug(`Plugin path is ${pluginPath}`);
fs.copyFileSync(path.join(toolPath, filename), pluginPath); fs.copyFileSync(path.join(toolPath, filename), pluginPath);
@ -78,10 +76,10 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
async function download(version: string): Promise<string> { async function download(version: string): Promise<string> {
version = semver.clean(version) || ''; version = semver.clean(version) || '';
const platform: string = osPlat == 'win32' ? 'windows' : osPlat; const platform: string = context.osPlat == 'win32' ? 'windows' : context.osPlat;
const ext: string = osPlat == 'win32' ? '.exe' : ''; const ext: string = context.osPlat == 'win32' ? '.exe' : '';
const filename: string = util.format('buildx-v%s.%s-amd64%s', version, platform, ext); const filename: string = util.format('buildx-v%s.%s-amd64%s', version, platform, ext);
const targetFile: string = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, filename); const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, filename);
let downloadPath: string; let downloadPath: string;

40
src/context.ts Normal file
View File

@ -0,0 +1,40 @@
import * as os from 'os';
import * as core from '@actions/core';
export const osPlat: string = os.platform();
export interface Inputs {
version: string;
driver: string;
driverOpts: string[];
buildkitdFlags: string;
install: boolean;
use: boolean;
}
export async function getInputs(): Promise<Inputs> {
return {
version: core.getInput('version'),
driver: core.getInput('driver') || 'docker-container',
driverOpts: await getInputList('driver-opts', true),
buildkitdFlags: core.getInput('buildkitd-flags'),
install: /true/i.test(core.getInput('install')),
use: /true/i.test(core.getInput('use'))
};
}
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.reduce<string[]>((acc, line) => acc.concat(!ignoreComma ? line.split(',') : line).map(pat => pat.trim()), []);
}
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};

View File

@ -3,6 +3,7 @@ import * as exec from '@actions/exec';
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as buildx from './buildx'; import * as buildx from './buildx';
import * as context from './context';
import * as mexec from './exec'; import * as mexec from './exec';
import * as stateHelper from './state-helper'; import * as stateHelper from './state-helper';
@ -13,48 +14,40 @@ async function run(): Promise<void> {
return; return;
} }
const bxVersion: string = core.getInput('version'); const inputs: context.Inputs = await context.getInputs();
const bxDriver: string = core.getInput('driver') || 'docker-container';
const bxDriverOpt: string = core.getInput('driver-opt');
const bxBuildkitdFlags: string = core.getInput('buildkitd-flags');
const bxInstall: boolean = /true/i.test(core.getInput('install'));
const bxUse: boolean = /true/i.test(core.getInput('use'));
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
if (!(await buildx.isAvailable()) || bxVersion) { if (!(await buildx.isAvailable()) || inputs.version) {
await buildx.install(bxVersion || 'latest', dockerConfigHome); await buildx.install(inputs.version || 'latest', dockerConfigHome);
} }
core.info('📣 Buildx info'); core.info('📣 Buildx info');
await exec.exec('docker', ['buildx', 'version']); await exec.exec('docker', ['buildx', 'version']);
const builderName: string = const builderName: string =
bxDriver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(await buildx.countBuilders()) + 1}`; inputs.driver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(await buildx.countBuilders()) + 1}`;
core.setOutput('name', builderName); core.setOutput('name', builderName);
stateHelper.setBuilderName(builderName); stateHelper.setBuilderName(builderName);
if (bxDriver != 'docker') { if (inputs.driver !== 'docker') {
core.info('🔨 Creating a new builder instance...'); core.info('🔨 Creating a new builder instance...');
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', bxDriver]; let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
if (bxDriverOpt) { await context.asyncForEach(inputs.driverOpts, async driverOpt => {
createArgs.push('--driver-opt', bxDriverOpt); createArgs.push('--driver-opt', driverOpt);
});
if (inputs.buildkitdFlags) {
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
} }
if (bxBuildkitdFlags) { if (inputs.use) {
createArgs.push('--buildkitd-flags', bxBuildkitdFlags);
}
if (bxUse) {
createArgs.push('--use'); createArgs.push('--use');
} }
await exec.exec('docker', createArgs); await exec.exec('docker', createArgs);
core.info('🏃 Booting builder...'); core.info('🏃 Booting builder...');
await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']); await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']);
} }
if (bxInstall) { if (inputs.install) {
core.info('🤝 Setting buildx as default builder...'); core.info('🤝 Setting buildx as default builder...');
await exec.exec('docker', ['buildx', 'install']); await exec.exec('docker', ['buildx', 'install']);
} }