Skip to content

Commit

Permalink
Enhance builder inspection
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 Apr 22, 2021
1 parent abdb186 commit 0dcbadd
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 50 deletions.
42 changes: 28 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ jobs:
with:
version: ${{ matrix.buildx-version }}
-
name: Builder instance name
run: echo ${{ steps.buildx.outputs.name }}
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
name: Inspect builder
run: |
echo "Name: ${{ steps.buildx.outputs.name }}"
echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}"
echo "Status: ${{ steps.buildx.outputs.status }}"
echo "Flags: ${{ steps.buildx.outputs.flags }}"
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
-
name: Dump context
uses: crazy-max/ghaction-dump-context@v1
Expand All @@ -55,15 +57,25 @@ jobs:
id: buildx1
uses: ./
-
name: Builder 1 instance name
run: echo ${{ steps.buildx1.outputs.name }}
name: Inspect builder 1
run: |
echo "Name: ${{ steps.buildx1.outputs.name }}"
echo "Endpoint: ${{ steps.buildx1.outputs.endpoint }}"
echo "Status: ${{ steps.buildx1.outputs.status }}"
echo "Flags: ${{ steps.buildx1.outputs.flags }}"
echo "Platforms: ${{ steps.buildx1.outputs.platforms }}"
-
name: Set up Docker Buildx 2
id: buildx2
uses: ./
-
name: Builder 2 instance name
run: echo ${{ steps.buildx2.outputs.name }}
name: Inspect builder 2
run: |
echo "Name: ${{ steps.buildx2.outputs.name }}"
echo "Endpoint: ${{ steps.buildx2.outputs.endpoint }}"
echo "Status: ${{ steps.buildx2.outputs.status }}"
echo "Flags: ${{ steps.buildx2.outputs.flags }}"
echo "Platforms: ${{ steps.buildx2.outputs.platforms }}"
install:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -204,8 +216,10 @@ jobs:
with:
version: ${{ matrix.buildx-version }}
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
-
name: Builder instance name
run: echo ${{ steps.buildx.outputs.name }}
name: Inspect builder
run: |
echo "Name: ${{ steps.buildx.outputs.name }}"
echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}"
echo "Status: ${{ steps.buildx.outputs.status }}"
echo "Flags: ${{ steps.buildx.outputs.flags }}"
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jobs:
uses: docker/bake-action@v1
with:
targets: validate
-
name: Set up Docker Buildx
uses: ./
-
name: Test
uses: docker/bake-action@v1
Expand Down
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ jobs:
id: buildx
uses: docker/setup-buildx-action@v1
-
name: Builder instance name
run: echo ${{ steps.buildx.outputs.name }}
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
name: Inspect builder
run: |
echo "Name: ${{ steps.buildx.outputs.name }}"
echo "Status: ${{ steps.buildx.outputs.status }}"
echo "Endpoint: ${{ steps.buildx.outputs.endpoint }}"
echo "Platforms: ${{ steps.buildx.outputs.platforms }}"
```

### With QEMU
Expand Down Expand Up @@ -129,7 +130,7 @@ Following inputs can be used as `step.with` keys
| `install` | Bool | Sets up `docker build` command as an alias to `docker buildx` (default `false`) |
| `use` | Bool | Switch to this builder instance (default `true`) |
| `endpoint` | String | [Optional address for docker socket](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#description) or context from `docker context ls` |
| `config` | String | [Optional config file path](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#config) |
| `config` | String | [BuildKit config fileh](https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#config) |

> `CSV` type must be a newline-delimited string
> ```yaml
Expand All @@ -147,8 +148,12 @@ Following outputs are available
| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `name` | String | Builder instance name |
| `platforms` | String | Available platforms (comma separated) |
| `name` | String | Builder name |
| `driver` | String | Builder driver |
| `endpoint` | String | Builder node endpoint |
| `status` | String | Builder node status |
| `flags` | String | Builder node flags (if applicable) |
| `platforms` | String | Builder node platforms available (comma separated) |
### environment variables
Expand Down
12 changes: 7 additions & 5 deletions __tests__/buildx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ describe('parseVersion', () => {
});
});

describe('platforms', () => {
describe('inspect', () => {
async function isDaemonRunning() {
return await docker.isDaemonRunning();
}
(isDaemonRunning() ? it : it.skip)(
'valid',
async () => {
const platforms = buildx.platforms();
console.log(`platforms: ${platforms}`);
expect(platforms).not.toBeUndefined();
expect(platforms).not.toEqual('');
const builder = await buildx.inspect('');
console.log('builder', builder);
expect(builder).not.toBeUndefined();
expect(builder.name).not.toEqual('');
expect(builder.driver).not.toEqual('');
expect(builder.node_platforms).not.toEqual('');
},
100000
);
Expand Down
14 changes: 11 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ inputs:
description: 'Optional address for docker socket or context from `docker context ls`'
required: false
config:
description: 'Optional config file path'
description: 'BuildKit config file'
required: false

outputs:
name:
description: 'Builder instance name'
description: 'Builder name'
driver:
description: 'Builder driver'
endpoint:
description: 'Builder node endpoint'
status:
description: 'Builder node status'
flags:
description: 'Builder node flags (if applicable)'
platforms:
description: 'Available platforms (comma separated)'
description: 'Builder node platforms available (comma separated)'

runs:
using: 'node12'
Expand Down
61 changes: 50 additions & 11 deletions dist/index.js

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

54 changes: 49 additions & 5 deletions src/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';

export type Builder = {
name?: string;
driver?: string;
node_name?: string;
node_endpoint?: string;
node_status?: string;
node_flags?: string;
node_platforms?: string;
};

export async function getVersion(): Promise<string> {
return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => {
if (res.stderr != '' && !res.success) {
Expand All @@ -34,16 +44,50 @@ export async function isAvailable(): Promise<Boolean> {
});
}

export async function platforms(): Promise<String | undefined> {
return await exec.exec(`docker`, ['buildx', 'inspect'], true).then(res => {
export async function inspect(name: string): Promise<Builder> {
return await exec.exec(`docker`, ['buildx', 'inspect', name], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
for (const line of res.stdout.trim().split(`\n`)) {
if (line.startsWith('Platforms')) {
return line.replace('Platforms: ', '').replace(/\s/g, '').trim();
const builder: Builder = {};
itlines: for (const line of res.stdout.trim().split(`\n`)) {
const [key, ...rest] = line.split(':');
const value = rest.map(v => v.trim()).join(':');
if (key.length == 0 || value.length == 0) {
continue;
}
switch (key) {
case 'Name': {
if (builder.name == undefined) {
builder.name = value;
} else {
builder.node_name = value;
}
break;
}
case 'Driver': {
builder.driver = value;
break;
}
case 'Endpoint': {
builder.node_endpoint = value;
break;
}
case 'Status': {
builder.node_status = value;
break;
}
case 'Flags': {
builder.node_flags = value;
break;
}
case 'Platforms': {
builder.node_platforms = value.replace(/\s/g, '');
break itlines;
}
}
}
return builder;
});
}

Expand Down
12 changes: 8 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ async function run(): Promise<void> {
core.endGroup();
}

core.startGroup(`Extracting available platforms`);
const platforms = await buildx.platforms();
core.info(`${platforms}`);
core.setOutput('platforms', platforms);
core.startGroup(`Inspect builder`);
const builder = await buildx.inspect(builderName);
core.info(JSON.stringify(builder, undefined, 2));
core.setOutput('driver', builder.driver);
core.setOutput('endpoint', builder.node_endpoint);
core.setOutput('status', builder.node_status);
core.setOutput('flags', builder.node_flags);
core.setOutput('platforms', builder.node_platforms);
core.endGroup();
} catch (error) {
core.setFailed(error.message);
Expand Down

0 comments on commit 0dcbadd

Please sign in to comment.