Skip to content

Commit

Permalink
nodes metadata JSON ouput
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 18, 2022
1 parent f385c9e commit 2e7d20a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ jobs:
uses: actions/checkout@v3
-
name: Set up Docker Buildx
id: buildx
uses: ./
with:
version: ${{ matrix.buildx-version }}
-
name: Nodes output
run: |
cat << EOF
${{ steps.buildx.outputs.nodes }}
EOF
multi:
runs-on: ubuntu-latest
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,11 @@ Following inputs can be used as `step.with` keys
Following outputs are available
| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `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) |
| Name | Type | Description |
|-------------|--------|------------------------|
| `name` | String | Builder name |
| `driver` | String | Builder driver |
| `nodes` | JSON | Builder nodes metadata |
### environment variables
Expand Down
2 changes: 1 addition & 1 deletion __tests__/buildx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('inspect', () => {
expect(builder).not.toBeUndefined();
expect(builder.name).not.toEqual('');
expect(builder.driver).not.toEqual('');
expect(builder.node_platforms).not.toEqual('');
expect(builder.nodes).not.toEqual({});
}, 100000);
});

Expand Down
10 changes: 6 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ outputs:
description: 'Builder name'
driver:
description: 'Builder driver'
nodes:
description: 'Builder nodes metadata'
endpoint:
description: 'Builder node endpoint'
description: 'Builder node endpoint (deprecated, use nodes output instead)'
status:
description: 'Builder node status'
description: 'Builder node status (deprecated, use nodes output instead)'
flags:
description: 'Builder node flags (if applicable)'
description: 'Builder node flags (deprecated, use nodes output instead)'
platforms:
description: 'Builder node platforms available (comma separated)'
description: 'Builder node platforms available (deprecated, use nodes output instead)'

runs:
using: 'node16'
Expand Down
54 changes: 34 additions & 20 deletions src/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ 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;
nodes: Node[];
};

export type Node = {
name?: string;
endpoint?: string;
status?: string;
'buildkitd-flags'?: string;
platforms?: string;
};

export async function getConfigInline(s: string): Promise<string> {
Expand Down Expand Up @@ -98,44 +102,54 @@ export async function inspect(name: string, standalone?: boolean): Promise<Build
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
const builder: Builder = {};
itlines: for (const line of res.stdout.trim().split(`\n`)) {
const builder: Builder = {
nodes: []
};
let node: Node = {};
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': {
switch (key.toLowerCase()) {
case 'name': {
if (builder.name == undefined) {
builder.name = value;
} else {
builder.node_name = value;
if (Object.keys(node).length > 0) {
builder.nodes.push(node);
node = {};
}
node.name = value;
}
break;
}
case 'Driver': {
case 'driver': {
builder.driver = value;
break;
}
case 'Endpoint': {
builder.node_endpoint = value;
case 'endpoint': {
node.endpoint = value;
break;
}
case 'Status': {
builder.node_status = value;
case 'status': {
node.status = value;
break;
}
case 'Flags': {
builder.node_flags = value;
case 'flags': {
node['buildkitd-flags'] = value;
break;
}
case 'Platforms': {
builder.node_platforms = value.replace(/\s/g, '');
break itlines;
case 'platforms': {
node.platforms = value.replace(/\s/g, '');
break;
}
}
}
if (Object.keys(node).length > 0) {
builder.nodes.push(node);
}
return builder;
});
}
Expand Down
21 changes: 13 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,26 @@ async function run(): Promise<void> {

core.startGroup(`Inspect builder`);
const builder = await buildx.inspect(builderName, standalone);
const firstNode = builder.nodes[0];
core.info(JSON.stringify(builder, undefined, 2));
context.setOutput('driver', builder.driver);
context.setOutput('endpoint', builder.node_endpoint);
context.setOutput('status', builder.node_status);
context.setOutput('flags', builder.node_flags);
context.setOutput('platforms', builder.node_platforms);
context.setOutput('nodes', JSON.stringify(builder.nodes, undefined, 2));
context.setOutput('endpoint', firstNode.endpoint); // TODO: deprecated, to be removed in a later version
context.setOutput('status', firstNode.status); // TODO: deprecated, to be removed in a later version
context.setOutput('flags', firstNode['buildkitd-flags']); // TODO: deprecated, to be removed in a later version
context.setOutput('platforms', firstNode.platforms); // TODO: deprecated, to be removed in a later version
core.endGroup();

if (!standalone && inputs.driver == 'docker-container') {
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
if (!standalone && builder.driver == 'docker-container') {
stateHelper.setContainerName(`buildx_buildkit_${firstNode.name}`);
core.startGroup(`BuildKit version`);
core.info(await buildx.getBuildKitVersion(`buildx_buildkit_${builder.node_name}`));
for (const node of builder.nodes) {
const bkvers = await buildx.getBuildKitVersion(`buildx_buildkit_${node.name}`);
core.info(`${node.name}: ${bkvers}`);
}
core.endGroup();
}
if (core.isDebug() || builder.node_flags?.includes('--debug')) {
if (core.isDebug() || firstNode['buildkitd-flags']?.includes('--debug')) {
stateHelper.setDebug('true');
}
} catch (error) {
Expand Down

0 comments on commit 2e7d20a

Please sign in to comment.