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

Simplified and unified argument collection #430

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 16 additions & 3 deletions __tests__/context.test.ts
Expand Up @@ -188,9 +188,9 @@ describe('getArgs', () => {
[
'buildx',
'build',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
'--tag', 'name/app:7.4',
'--tag', 'name/app:latest',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
'https://github.com/docker/build-push-action.git#refs/heads/test-jest'
]
],
Expand Down Expand Up @@ -481,12 +481,12 @@ nproc=3`],
[
'buildx',
'build',
'--ulimit', 'nofile=1024:1024',
'--ulimit', 'nproc=3',
'--cgroup-parent', 'foo',
'--file', './test/Dockerfile',
'--iidfile', '/tmp/.docker-build-push-jest/iidfile',
'--shm-size', '2g',
'--ulimit', 'nofile=1024:1024',
'--ulimit', 'nproc=3',
'--metadata-file', '/tmp/.docker-build-push-jest/metadata-file',
'.'
]
Expand Down Expand Up @@ -662,6 +662,19 @@ describe('asyncForEach', () => {
});
});

describe('flagMap', () => {
it('should prepend array elements with the provided flag', async () => {
const testValues = ['a', 'b', 'c'];
const results: string[][] = context.flagMap(testValues, '--catpants');

expect(results).toEqual([
['--catpants', 'a'],
['--catpants', 'b'],
['--catpants', 'c']
]);
});
});

describe('setOutput', () => {
beforeEach(() => {
process.stdout.write = jest.fn();
Expand Down
38 changes: 7 additions & 31 deletions dist/index.js

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

46 changes: 16 additions & 30 deletions src/context.ts
Expand Up @@ -94,27 +94,24 @@ export async function getInputs(defaultContext: string): Promise<Inputs> {
}

export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['buildx'];
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion));
args.push.apply(args, await getCommonArgs(inputs, buildxVersion));
args.push(inputs.context);
return args;
return ['buildx', ...(await getBuildArgs(inputs, defaultContext, buildxVersion)), ...(await getCommonArgs(inputs, buildxVersion)), inputs.context];
}

async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['build'];
const args: Array<string> = ['build'].concat(
...flagMap(inputs.buildArgs, '--build-arg'),
...flagMap(inputs.cacheFrom, '--cache-from'),
...flagMap(inputs.cacheTo, '--cache-to'),
...flagMap(inputs.labels, '--label'),
...flagMap(inputs.outputs, '--output'),
...flagMap(inputs.ssh, '--ssh'),
...flagMap(inputs.tags, '--tag'),
...flagMap(inputs.ulimit, '--ulimit')
);

if (inputs.allow.length > 0) {
args.push('--allow', inputs.allow.join(','));
}
await asyncForEach(inputs.buildArgs, async buildArg => {
args.push('--build-arg', buildArg);
});
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
args.push('--cache-from', cacheFrom);
});
await asyncForEach(inputs.cacheTo, async cacheTo => {
args.push('--cache-to', cacheTo);
});
if (inputs.cgroupParent) {
args.push('--cgroup-parent', inputs.cgroupParent);
}
Expand All @@ -124,12 +121,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
args.push('--iidfile', await buildx.getImageIDFile());
}
await asyncForEach(inputs.labels, async label => {
args.push('--label', label);
});
await asyncForEach(inputs.outputs, async output => {
args.push('--output', output);
});
if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(','));
}
Expand All @@ -153,18 +144,9 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio
if (inputs.shmSize) {
args.push('--shm-size', inputs.shmSize);
}
await asyncForEach(inputs.ssh, async ssh => {
args.push('--ssh', ssh);
});
await asyncForEach(inputs.tags, async tag => {
args.push('--tag', tag);
});
if (inputs.target) {
args.push('--target', inputs.target);
}
await asyncForEach(inputs.ulimit, async ulimit => {
args.push('--ulimit', ulimit);
});
return args;
}

Expand Down Expand Up @@ -227,6 +209,10 @@ export const asyncForEach = async (array, callback) => {
}
};

export function flagMap(array: string[], flag: string): string[][] {
return array.map(value => [flag, value]);
}

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