Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use context for inputs #62

Merged
merged 1 commit into from Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -13,6 +13,7 @@ ___
* [Usage](#usage)
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
* [Contributing](#contributing)

## Usage
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/context.ts
@@ -0,0 +1,19 @@
import * as core from '@actions/core';
import {issueCommand} from '@actions/core/lib/command';

export interface Inputs {
image: string;
platforms: string;
}

export function getInputs(): Inputs {
return {
image: core.getInput('image') || 'tonistiigi/binfmt:latest',
platforms: core.getInput('platforms') || 'all'
};
}

// FIXME: Temp fix https://github.com/actions/toolkit/issues/777
export function setOutput(name: string, value: unknown): void {
issueCommand('set-output', {name}, value);
}
78 changes: 38 additions & 40 deletions src/main.ts
@@ -1,6 +1,6 @@
import * as context from './context';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import {issueCommand} from '@actions/core/lib/command';

interface Platforms {
supported: string[];
Expand All @@ -9,49 +9,47 @@ interface Platforms {

async function run(): Promise<void> {
try {
core.startGroup(`Docker info`);
await exec.exec('docker', ['version']);
await exec.exec('docker', ['info']);
core.endGroup();

const image: string = core.getInput('image') || 'tonistiigi/binfmt:latest';
const platforms: string = core.getInput('platforms') || 'all';

core.startGroup(`Pulling binfmt Docker image`);
await exec.exec('docker', ['pull', image]);
core.endGroup();

core.startGroup(`Image info`);
await exec.exec('docker', ['image', 'inspect', image]);
core.endGroup();

core.startGroup(`Installing QEMU static binaries`);
await exec.exec('docker', ['run', '--rm', '--privileged', image, '--install', platforms]);
core.endGroup();

core.startGroup(`Extracting available platforms`);
await exec
.getExecOutput('docker', ['run', '--rm', '--privileged', image], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
const platforms: Platforms = JSON.parse(res.stdout.trim());
core.info(`${platforms.supported.join(',')}`);
setOutput('platforms', platforms.supported.join(','));
const input: context.Inputs = context.getInputs();

await core.group(`Docker info`, async () => {
await exec.exec('docker', ['version'], {
failOnStdErr: false
});
await exec.exec('docker', ['info'], {
failOnStdErr: false
});
core.endGroup();
});

await core.group(`Pulling binfmt Docker image`, async () => {
await exec.exec('docker', ['pull', input.image]);
});

await core.group(`Image info`, async () => {
await exec.exec('docker', ['image', 'inspect', input.image]);
});

await core.group(`Installing QEMU static binaries`, async () => {
await exec.exec('docker', ['run', '--rm', '--privileged', input.image, '--install', input.platforms]);
});

await core.group(`Extracting available platforms`, async () => {
await exec
.getExecOutput('docker', ['run', '--rm', '--privileged', input.image], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
const platforms: Platforms = JSON.parse(res.stdout.trim());
core.info(`${platforms.supported.join(',')}`);
context.setOutput('platforms', platforms.supported.join(','));
});
});
} catch (error) {
core.setFailed(error.message);
}
}

// FIXME: Temp fix https://github.com/actions/toolkit/issues/777
function setOutput(name: string, value: unknown): void {
issueCommand('set-output', {name}, value);
}

run();