Skip to content

Commit

Permalink
display QEMU version installed on the runner
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Jan 5, 2024
1 parent 5306bad commit 56748a4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
24 changes: 24 additions & 0 deletions __tests__/qemu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {describe, expect, jest, it} from '@jest/globals';
import * as io from '@actions/io';
import {Exec} from '@docker/actions-toolkit/lib/exec';

import * as qemu from '../src/qemu';

describe('isInstalled', () => {
it('bin', async () => {
const ioWhichSpy = jest.spyOn(io, 'which');
await qemu.isInstalled();
expect(ioWhichSpy).toHaveBeenCalledTimes(1);
expect(ioWhichSpy).toHaveBeenCalledWith(qemu.bin(), true);
});
});

describe('printVersion', () => {
it('call qemu --version', async () => {
const execSpy = jest.spyOn(Exec, 'exec');
await qemu.printVersion().catch(() => {
// noop
});
expect(execSpy).toHaveBeenCalledWith(qemu.bin(), ['--version']);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "Apache-2.0",
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/io": "^1.1.3",
"@docker/actions-toolkit": "^0.16.1"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as context from './context';
import * as qemu from './qemu';
import * as core from '@actions/core';
import * as actionsToolkit from '@docker/actions-toolkit';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
Expand All @@ -19,6 +20,14 @@ actionsToolkit.run(
await Docker.printInfo();
});

await core.group('QEMU version', async () => {
if (await qemu.isInstalled()) {
await qemu.printVersion();
} else {
core.warning('QEMU is not installed');
}
});

await core.group(`Pulling binfmt Docker image`, async () => {
await Exec.exec('docker', ['pull', input.image]);
});
Expand Down
39 changes: 39 additions & 0 deletions src/qemu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os from 'os';
import * as core from '@actions/core';
import * as io from '@actions/io';
import {Exec} from '@docker/actions-toolkit/lib/exec';

export async function isInstalled(): Promise<boolean> {
return await io
.which(bin(), true)
.then(res => {
core.debug(`qemu.isInstalled ok: ${res}`);
return true;
})
.catch(error => {
core.debug(`qemu.isInstalled error: ${error}`);
return false;
});
}

export async function printVersion(): Promise<void> {
await Exec.exec(bin(), ['--version']);
}

export function bin(): string {
return `qemu-system-${arch()}`;
}

function arch(): string {
switch (os.arch()) {
case 'x64': {
return 'x86_64';
}
case 'arm64': {
return 'aarch64';
}
default: {
return os.arch();
}
}
}

0 comments on commit 56748a4

Please sign in to comment.