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

ref(docker): Use docker buildx (BuildKit) to publish docker images #556

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -904,17 +904,16 @@ targets:

### Docker (`docker`)

Pulls an existing source image tagged with the revision SHA, and then pushed it
to a new target tagged with the released version. No release
assets are required for this target except for the source image at the provided
source image location so it would be a good idea to add a status check that
ensures the source image exists, otherwise `craft publish` will fail at the
`docker pull` step, causing an interrupted publish. This is an issue for other,
non-idempotent targets, not for the Docker target.
Copies an existing source image tagged with the revision SHA to a new target
tagged with the released version. No release assets are required for this target
except for the source image at the provided source image location so it would be
a good idea to add a status check that ensures the source image exists, otherwise
`craft publish` will fail at the copy step, causing an interrupted publish.
This is an issue for other, non-idempotent targets, not for the Docker target.

**Environment**

`docker` executable (or something equivalent) must be installed on the system.
`docker` executable (or something equivalent) with BuildKit must be installed on the system.

| Name | Description |
| ----------------- | ------------------------------------------ |
Expand Down
34 changes: 9 additions & 25 deletions src/targets/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,14 @@ 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> {
this.logger.debug('Pulling source image...');
const sourceImage = renderTemplateSafe(this.dockerConfig.sourceTemplate, {
...this.dockerConfig,
revision,
});
return spawnProcess(
DOCKER_BIN,
['pull', sourceImage],
{},
{ enableInDryRunMode: true }
);
}

/**
* Pushes the locally tagged source image to Docker Hub
* 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
*/
public async push(sourceRevision: string, version: string): Promise<any> {
async copy(sourceRevision: string, version: string): Promise<any> {
const sourceImage = renderTemplateSafe(this.dockerConfig.sourceTemplate, {
...this.dockerConfig,
revision: sourceRevision,
Expand All @@ -114,11 +99,11 @@ export class DockerTarget extends BaseTarget {
...this.dockerConfig,
version,
});
this.logger.debug('Tagging target image...');
await spawnProcess(DOCKER_BIN, ['tag', sourceImage, targetImage]);

this.logger.debug(`Copying image from ${sourceImage} to ${targetImage}...`);
return spawnProcess(
DOCKER_BIN,
['push', targetImage],
['buildx', 'imagetools', 'create', '--tag', targetImage, sourceImage],
{},
{ showStdout: true }
);
Expand All @@ -132,8 +117,7 @@ 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);
await this.copy(revision, version);

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