Skip to content

Commit

Permalink
Fix docker push errors not failing build
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkRx committed Jul 11, 2023
1 parent e3bceb5 commit 624e35a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.bmuschko.gradle.docker.tasks.image

import com.bmuschko.gradle.docker.AbstractGroovyDslFunctionalTest
import org.gradle.testkit.runner.BuildResult

class DockerPushImageFunctionalTest extends AbstractGroovyDslFunctionalTest {

def "fails on push error"() {
def image = createUniqueImageId()

buildFile << """
import com.bmuschko.gradle.docker.tasks.image.Dockerfile
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerPushImage
task dockerfile(type: Dockerfile) {
from '$TEST_IMAGE_WITH_TAG'
label(['maintainer': '[email protected]'])
}
task buildImage(type: DockerBuildImage) {
dependsOn dockerfile
inputDir = file("build/docker")
images.add("${image}")
}
task pushImage(type: DockerPushImage) {
dependsOn buildImage
images.add("${image}")
}
"""

when:
BuildResult result = build('pushImage')

then:
def e = thrown(Exception)
e.message.contains("Could not push image")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import com.bmuschko.gradle.docker.tasks.RegistryCredentialsAware;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.PushImageCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.PushResponseItem;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.Input;

import javax.annotation.Nullable;

public class DockerPushImage extends AbstractDockerRemoteApiTask implements RegistryCredentialsAware {

/**
Expand Down Expand Up @@ -80,14 +83,30 @@ public void registryCredentials(Action<? super DockerRegistryCredentials> action
}

private ResultCallback.Adapter<PushResponseItem> createCallback(final Action nextHandler) {
// Workaround to manually handle error logic - see https://github.com/docker-java/docker-java/issues/2140
return new ResultCallback.Adapter<PushResponseItem>() {
@Nullable
private PushResponseItem latestItem = null;

@Override
public void onNext(PushResponseItem item) {
this.latestItem = item;

if (nextHandler != null) {
nextHandler.execute(item);
}
}

@Override
protected void throwFirstError() {
super.throwFirstError();

if (latestItem == null) {
throw new DockerClientException("Could not push image");
} else if (latestItem.isErrorIndicated()) {
throw new DockerClientException("Could not push image: " + latestItem.getError());
}
}
};
}
}

0 comments on commit 624e35a

Please sign in to comment.