Skip to content

Commit

Permalink
ref(docker): Support multi-arch images through docker buildkit
Browse files Browse the repository at this point in the history
  • Loading branch information
David Herberth authored and Dav1dde committed Sep 20, 2024
1 parent 6f7fa1c commit 2d1a890
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions src/targets/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class DockerTarget extends BaseTarget {
* Pushes the the source image into local
* @param revision Image tag, usually the git revision
*/
public async pull(revision: string): Promise<any> {
async pull(revision: string): Promise<any> {
this.logger.debug('Pulling source image...');
const sourceImage = renderTemplateSafe(this.dockerConfig.sourceTemplate, {
...this.dockerConfig,
Expand All @@ -105,7 +105,7 @@ export class DockerTarget extends BaseTarget {
* @param sourceRevision The tag/revision for the source image
* @param version The release version for the target image
*/
public async push(sourceRevision: string, version: string): Promise<any> {
async push(sourceRevision: string, version: string): Promise<any> {
const sourceImage = renderTemplateSafe(this.dockerConfig.sourceTemplate, {
...this.dockerConfig,
revision: sourceRevision,
Expand All @@ -124,6 +124,38 @@ export class DockerTarget extends BaseTarget {
);
}


async hasBuildKit(): Promise<boolean> {
return spawnProcess(DOCKER_BIN, ['buildx', 'version']).then(() => true).catch(_ => false);
}

/**
* Copies an existing local or remote docker image to a new destination.
*
* Requires BuildKit / `docker buildx` to be installed.
*
* @param sourceRevision The tag/revision for the source image
* @param version The release version for the target image
*/
async copy(sourceRevision: string, version: string): Promise<any> {
const sourceImage = renderTemplateSafe(this.dockerConfig.sourceTemplate, {
...this.dockerConfig,
revision: sourceRevision,
});
const targetImage = renderTemplateSafe(this.dockerConfig.targetTemplate, {
...this.dockerConfig,
version,
});

this.logger.debug(`Copying image from ${sourceImage} to ${targetImage}...`);
return spawnProcess(
DOCKER_BIN,
['buildx', 'imagetools', 'create', '--tag', targetImage, sourceImage],
{},
{ showStdout: true }
);
}

/**
* Pushes a source image to Docker Hub
*
Expand All @@ -132,8 +164,14 @@ export class DockerTarget extends BaseTarget {
*/
public async publish(version: string, revision: string): Promise<any> {
await this.login();
await this.pull(revision);
await this.push(revision, version);

if (this.hasBuildKit()) {
await this.copy(revision, version);
} else {
// Fall back to slow/old pull and push method.
await this.pull(revision);
await this.push(revision, version);
}

this.logger.info('Docker release complete');
}
Expand Down

0 comments on commit 2d1a890

Please sign in to comment.