Skip to content

Commit

Permalink
driver-opt as array of inputs (renamed driver-opts)
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Sep 3, 2020
1 parent f96a630 commit d479e0f
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
strategy:
fail-fast: false
matrix:
driver-opt:
driver-opts:
- image=moby/buildkit:latest
- image=moby/buildkit:master
steps:
Expand All @@ -119,7 +119,7 @@ jobs:
uses: ./
with:
driver: docker-container
driver-opt: ${{ matrix.driver-opt }}
driver-opts: ${{ matrix.driver-opt }}

docker-driver:
runs-on: ubuntu-latest
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`) |
| `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 |
| `install` | Bool | Sets up `docker build` command as an alias to `docker buildx` (default `false`) |
| `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

Following outputs are available
Expand Down
74 changes: 74 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ inputs:
description: 'Sets the builder driver to be used'
default: 'docker-container'
required: false
driver-opt:
description: 'Passes additional driver-specific options. Eg. image=moby/buildkit:master'
driver-opts:
description: 'List of additional driver-specific options. Eg. image=moby/buildkit:master'
required: false
buildkitd-flags:
description: 'Flags for buildkitd daemon'
Expand Down
115 changes: 91 additions & 24 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions src/buildx.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as util from 'util';
import * as context from './context';
import * as exec from './exec';
import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';

const osPlat: string = os.platform();

export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) {
Expand Down Expand Up @@ -65,7 +63,7 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
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);
core.debug(`Plugin path is ${pluginPath}`);
fs.copyFileSync(path.join(toolPath, filename), pluginPath);
Expand All @@ -78,10 +76,10 @@ export async function install(inputVersion: string, dockerConfigHome: string): P

async function download(version: string): Promise<string> {
version = semver.clean(version) || '';
const platform: string = osPlat == 'win32' ? 'windows' : osPlat;
const ext: string = osPlat == 'win32' ? '.exe' : '';
const platform: string = context.osPlat == 'win32' ? 'windows' : context.osPlat;
const ext: string = context.osPlat == 'win32' ? '.exe' : '';
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);
let downloadPath: string;
Expand Down
40 changes: 40 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -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);
}
};
Loading

0 comments on commit d479e0f

Please sign in to comment.