Skip to content

Commit

Permalink
Support downloading binaries from docker images
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
vvoland committed Oct 28, 2024
1 parent 7bb5657 commit 2f969b0
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 14 deletions.
107 changes: 100 additions & 7 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ describe('getInputs', () => {
['set-host', 'false'],
]),
{
version: 'v24.0.8',
channel: '',
source: {
type: 'archive',
version: 'v24.0.8',
channel: 'stable'
},
context: '',
daemonConfig: '',
setHost: false
Expand All @@ -38,8 +41,11 @@ describe('getInputs', () => {
['set-host', 'false'],
]),
{
version: 'v24.0.0-rc.4',
channel: 'test',
source: {
type: 'archive',
version: 'v24.0.0-rc.4',
channel: 'test'
},
context: 'foo',
daemonConfig: `{"debug":true,"features":{"containerd-snapshotter":true}}`,
setHost: false
Expand All @@ -51,13 +57,100 @@ describe('getInputs', () => {
['set-host', 'true'],
]),
{
version: 'latest',
channel: '',
source: {
type: 'archive',
version: 'latest',
channel: 'stable',
},
context: '',
daemonConfig: '',
setHost: true
} as context.Inputs
]
],
[
3,
new Map<string, string>([
['version', 'type=image,tag=master'],
['context', 'foo'],
['daemon-config', `{"debug":true,"features":{"containerd-snapshotter":true}}`],
['set-host', 'false'],
]),
{
source: {
type: 'image',
tag: 'master',
},
context: 'foo',
daemonConfig: `{"debug":true,"features":{"containerd-snapshotter":true}}`,
setHost: false
} as context.Inputs
],
[
4,
new Map<string, string>([
['version', 'type=image'],
['set-host', 'false'],
]),
{
source: {
type: 'image',
tag: 'latest',
},
context: '',
daemonConfig: '',
setHost: false
} as context.Inputs
],
[
5,
new Map<string, string>([
['version', 'type=archive'],
['set-host', 'false'],
]),
{
source: {
type: 'archive',
version: 'latest',
channel: 'stable',
},
setHost: false,
context: '',
daemonConfig: '',
} as context.Inputs
],
[
6,
new Map<string, string>([
['version', 'version=v27.2.0,channel=test'],
['set-host', 'false'],
]),
{
source: {
type: 'archive',
version: 'v27.2.0',
channel: 'test',
},
setHost: false,
context: '',
daemonConfig: '',
} as context.Inputs
],
[
7,
new Map<string, string>([
['version', 'type=image,tag=27.2.1'],
['set-host', 'false'],
]),
{
source: {
type: 'image',
tag: '27.2.1',
},
setHost: false,
context: '',
daemonConfig: '',
} as context.Inputs
],
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: context.Inputs) => {
Expand Down
81 changes: 77 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,92 @@
import * as core from '@actions/core';
import {InstallSource} from '@docker/actions-toolkit/lib/docker/install';
import { parse } from 'csv-parse';

export interface Inputs {
version: string;
channel: string;
source: InstallSource;
daemonConfig?: string;
context: string;
setHost: boolean;
}

export function getInputs(): Inputs {
const rawVersion = core.getInput('version') || 'latest';
const source = parseSource(rawVersion);
const channel = core.getInput('channel');
if (channel && source.type === 'archive') {
source.channel = channel;
}

return {
version: core.getInput('version') || 'latest',
channel: core.getInput('channel'),
source: source,
daemonConfig: core.getInput('daemon-config'),
context: core.getInput('context'),
setHost: core.getBooleanInput('set-host')
};
}

function parseSource(input: string): InstallSource {
let [type, version, channel, tag] = ['archive', input, 'stable', 'latest'];

const fields = parse(version, {
relaxColumnCount: true,
skipEmptyLines: true
})[0];
for (const field of fields) {
const parts = field
.toString()
.split(/(?<=^[^=]+?)=/)
.map(item => item.trim());

switch (parts[0]) {
case 'type':
type = parts[1];
break;
case 'version':
version = parts[1];
break;
case 'channel':
channel = parts[1];
break;
case 'tag':
tag = parts[1];
break;
default:
throw new Error(`Invalid field: ${parts[0]}`);
}
}

if (!type) {
throw new Error(`Invalid type: ${type}`);
}
if (!channel) {
throw new Error(`Invalid channel: ${channel}`);
}
if (!version) {
throw new Error(`Invalid version: ${version}`);
}
if (!tag) {
throw new Error(`Invalid tag: ${tag}`);
}

let src: InstallSource;
switch (type) {
case 'archive':
src = {
type: 'archive',
version: version,
channel: channel
};
break;
case 'image':
src = {
type: 'image',
tag: tag
};
break;
default:
throw new Error(`Invalid version: ${input}`);
}

return src;
}
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ actionsToolkit.run(

const install = new Install({
runDir: runDir,
version: input.version,
channel: input.channel || 'stable',
source: input.source,
contextName: input.context || 'setup-docker-action',
daemonConfig: input.daemonConfig
});
let toolDir;
if (!(await Docker.isAvailable()) || input.version) {
if (!(await Docker.isAvailable()) || input.source) {
await core.group(`Download docker`, async () => {
toolDir = await install.download();
});
Expand Down

0 comments on commit 2f969b0

Please sign in to comment.