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

platform support #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ inputs:
description: 'docker-builder: prune docker system after build'
required: false
default: 'false'
platforms:
description: 'docker-builder: platforms to build for'
required: false

runs:
using: 'node12'
Expand Down
40 changes: 33 additions & 7 deletions src/commands/docker/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { exec, getArg, isDryRun, readJson } from '../../util';
import { readDockerConfig } from '../../utils/config';
import { build, publish } from '../../utils/docker';
import { init } from '../../utils/docker/buildx';
import { dockerDf, dockerPrune, dockerTag } from '../../utils/docker/common';
import {
docker,
dockerDf,
dockerPrune,
dockerTag,
} from '../../utils/docker/common';
import log from '../../utils/logger';
import * as renovate from '../../utils/renovate';
import { Config, ConfigFile } from '../../utils/types';
Expand Down Expand Up @@ -125,6 +130,7 @@ async function buildAndPush(
versioning,
majorMinor,
prune,
platforms,
}: Config,
versions: string[]
): Promise<void> {
Expand Down Expand Up @@ -198,16 +204,35 @@ async function buildAndPush(
cacheTags,
buildArgs: [...(buildArgs ?? []), `${buildArg}=${version}`],
dryRun,
platforms,
});

if (!buildOnly) {
await publish({ image, imagePrefix, tag, dryRun });
const source = tag;
const MultiPlatform: boolean =
!is.nullOrUndefined(platforms) && platforms.length > 1;

for (const tag of tags) {
log(`Publish ${source} as ${tag}`);
await dockerTag({ image, imagePrefix, src: source, tgt: tag });
if (!buildOnly) {
if (MultiPlatform) {
const source = tag;
for (const tag of tags) {
log(`Publish ${source} as ${tag}`);
await docker(
'buildx',
'imagetools',
'create',
'-t',
`${imagePrefix}/${image}:${tag}`,
`${imagePrefix}/${image}:${source}`
);
}
} else {
await publish({ image, imagePrefix, tag, dryRun });
const source = tag;

for (const tag of tags) {
log(`Publish ${source} as ${tag}`);
await dockerTag({ image, imagePrefix, src: source, tgt: tag });
await publish({ image, imagePrefix, tag, dryRun });
}
}
}

Expand Down Expand Up @@ -283,6 +308,7 @@ export async function run(): Promise<void> {
buildOnly: getInput('build-only') == 'true',
majorMinor: getArg('major-minor') !== 'false',
prune: getArg('prune') === 'true',
platforms: getArg('platforms', { multi: true }),
};

if (dryRun) {
Expand Down
22 changes: 16 additions & 6 deletions src/utils/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export type BuildOptions = {
tag?: string;
dryRun?: boolean;
buildArgs?: string[];
platforms?: string[];
};

const errors = [
Expand All @@ -144,18 +145,27 @@ export async function build({
tag = 'latest',
dryRun,
buildArgs,
platforms,
}: BuildOptions): Promise<void> {
const args = [
'buildx',
'build',
'--load',
`--tag=${imagePrefix}/${image}:${tag}`,
];
const MultiPlatform: boolean =
!is.nullOrUndefined(platforms) && platforms.length > 1;

const args = ['buildx', 'build', `--tag=${imagePrefix}/${image}:${tag}`];

if (!MultiPlatform) {
args.push('--load');
} else if (!dryRun) {
args.push('--push');
}

if (is.nonEmptyArray(buildArgs)) {
args.push(...buildArgs.map((b) => `--build-arg=${b}`));
}

if (is.array(platforms)) {
args.push(...platforms.map((p) => `--platform=${p}`));
}

if (is.string(cache)) {
const cacheImage = `${imagePrefix}/${cache}:${image.replace(/\//g, '-')}`;
args.push(`--cache-from=${cacheImage}-${tag}`);
Expand Down
5 changes: 3 additions & 2 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type ConfigFile = {

export type Config = {
buildArg: string;
buildArgs?: string[];
buildArgs: string[];
buildOnly: boolean;
tagSuffix?: string;
depName: string;
Expand All @@ -46,10 +46,11 @@ export type Config = {
lastOnly: boolean;
dryRun: boolean;
prune: boolean;
platforms?: string[];
} & ConfigFile;

export type BinaryBuilderConfig = {
buildArgs?: string[];
buildArgs: string[];
depName: string;
ignoredVersions: string[];
lastOnly: boolean;
Expand Down
98 changes: 98 additions & 0 deletions test/commands/docker/__snapshots__/builder.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Array [
"dryRun": undefined,
"image": "yarn",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "1.22.4",
},
],
Expand All @@ -39,6 +40,7 @@ Array [
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "12-slim",
},
],
Expand Down Expand Up @@ -66,6 +68,34 @@ Array [
]
`;

exports[`multiplatform build-only: build 1`] = `
Array [
Array [
Object {
"buildArgs": Array [
"YARN_VERSION=1.22.4",
],
"cache": "docker-build-cache",
"cacheTags": Array [
"latest",
"1",
"1.22",
],
"dryRun": undefined,
"image": "yarn",
"imagePrefix": "renovate",
"platforms": Array [
"linux/amd64",
"linux/arm64",
],
"tag": "1.22.4",
},
],
]
`;

exports[`multiplatform build-only: publish 1`] = `Array []`;

exports[`updates image yarn (dry-run): build 1`] = `
Array [
Array [
Expand All @@ -82,6 +112,7 @@ Array [
"dryRun": true,
"image": "yarn",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "1.22.4",
},
],
Expand Down Expand Up @@ -139,6 +170,7 @@ Array [
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"platforms": undefined,
"tag": "10",
},
],
Expand All @@ -154,6 +186,7 @@ Array [
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"platforms": undefined,
"tag": "13",
},
],
Expand Down Expand Up @@ -181,6 +214,64 @@ Array [
]
`;

exports[`works dummyx: build 1`] = `
Array [
Array [
Object {
"buildArgs": Array [
"DUMMY_VERSION=10",
],
"cache": undefined,
"cacheTags": Array [
"latest",
],
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"platforms": undefined,
"tag": "10",
},
],
Array [
Object {
"buildArgs": Array [
"DUMMY_VERSION=13",
],
"cache": undefined,
"cacheTags": Array [
"latest",
],
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"platforms": undefined,
"tag": "13",
},
],
]
`;

exports[`works dummyx: publish 1`] = `
Array [
Array [
Object {
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"tag": "10",
},
],
Array [
Object {
"dryRun": undefined,
"image": "dummy",
"imagePrefix": "renovate",
"tag": "13",
},
],
]
`;

exports[`works gradle: build 1`] = `
Array [
Array [
Expand All @@ -197,6 +288,7 @@ Array [
"dryRun": undefined,
"image": "gradle",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "3.5.5",
},
],
Expand All @@ -213,6 +305,7 @@ Array [
"dryRun": undefined,
"image": "gradle",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "4.5",
},
],
Expand All @@ -229,6 +322,7 @@ Array [
"dryRun": undefined,
"image": "gradle",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "6.0",
},
],
Expand Down Expand Up @@ -321,6 +415,7 @@ Array [
"dryRun": undefined,
"image": "pnpm",
"imagePrefix": "ghcr.io/renovatebot",
"platforms": Array [],
"tag": "5.0.0-slim",
},
],
Expand Down Expand Up @@ -380,6 +475,7 @@ Array [
"dryRun": undefined,
"image": "ubuntu",
"imagePrefix": "renovate",
"platforms": undefined,
"tag": "bionic",
},
],
Expand All @@ -397,6 +493,7 @@ Array [
"dryRun": undefined,
"image": "ubuntu",
"imagePrefix": "renovate",
"platforms": undefined,
"tag": "focal",
},
],
Expand Down Expand Up @@ -480,6 +577,7 @@ Array [
"dryRun": undefined,
"image": "yarn",
"imagePrefix": "renovate",
"platforms": Array [],
"tag": "1.22.4",
},
],
Expand Down
16 changes: 16 additions & 0 deletions test/commands/docker/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,20 @@ describe(getName(__filename), () => {
expect((e as Error).message).toEqual('missing-config');
}
});

it('multiplatform build-only', async () => {
datasources.getPkgReleases.mockResolvedValueOnce({
releases: [{ version }, { version: '2.0.0-rc.24' }],
});

args = {
...args,
platforms: ['linux/amd64', 'linux/arm64'],
};

await run();

expect(docker.build.mock.calls).toMatchSnapshot('build');
expect(docker.publish.mock.calls).toMatchSnapshot('publish');
});
});
Loading