Java Code Examples for com.spotify.docker.client.DockerClient#push()

The following examples show how to use com.spotify.docker.client.DockerClient#push() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PublishArtifactExecutor.java    From docker-registry-artifact-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse execute() {
    ArtifactPlan artifactPlan = publishArtifactRequest.getArtifactPlan();
    final ArtifactStoreConfig artifactStoreConfig = publishArtifactRequest.getArtifactStore().getArtifactStoreConfig();
    try {
        final DockerClient docker = clientFactory.docker(artifactStoreConfig);
        Map<String, String> environmentVariables = publishArtifactRequest.getEnvironmentVariables() == null ? new HashMap<>() : publishArtifactRequest.getEnvironmentVariables();
        environmentVariables.putAll(System.getenv());
        final DockerImage image = artifactPlan.getArtifactPlanConfig().imageToPush(publishArtifactRequest.getAgentWorkingDir(), environmentVariables);

        LOG.info(format("Pushing docker image `%s` to docker registry `%s`.", image, artifactStoreConfig.getRegistryUrl()));
        consoleLogger.info(format("Pushing docker image `%s` to docker registry `%s`.", image, artifactStoreConfig.getRegistryUrl()));

        docker.push(image.toString(), progressHandler);
        docker.close();

        publishArtifactResponse.addMetadata("image", image.toString());
        publishArtifactResponse.addMetadata("digest", progressHandler.getDigest());
        consoleLogger.info(format("Image `%s` successfully pushed to docker registry `%s`.", image, artifactStoreConfig.getRegistryUrl()));
        return DefaultGoPluginApiResponse.success(publishArtifactResponse.toJSON());
    } catch (Exception e) {
        consoleLogger.error(String.format("Failed to publish %s: %s", artifactPlan, e));
        LOG.error(String.format("Failed to publish %s: %s", artifactPlan, e.getMessage()), e);
        return DefaultGoPluginApiResponse.error(String.format("Failed to publish %s: %s", artifactPlan, e.getMessage()));
    }
}
 
Example 2
Source File: PushPullIT.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testPushHubPublicImageWithAuth() throws Exception {
  // Push an image to a public repo on Docker Hub and check it succeeds
  final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
  final RegistryAuth registryAuth = RegistryAuth.builder()
      .username(HUB_AUTH_USERNAME)
      .password(HUB_AUTH_PASSWORD)
      .build();
  final DockerClient client = DefaultDockerClient
      .fromEnv()
      .registryAuthSupplier(new FixedRegistryAuthSupplier(
          registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth))))
      .build();

  client.build(Paths.get(dockerDirectory), HUB_PUBLIC_IMAGE);
  client.push(HUB_PUBLIC_IMAGE);
}
 
Example 3
Source File: PushPullIT.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testPushHubPrivateImageWithAuth() throws Exception {
  // Push an image to a private repo on Docker Hub and check it succeeds
  final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
  final RegistryAuth registryAuth = RegistryAuth.builder()
      .username(HUB_AUTH_USERNAME)
      .password(HUB_AUTH_PASSWORD)
      .build();
  final DockerClient client = DefaultDockerClient
      .fromEnv()
      .registryAuthSupplier(new FixedRegistryAuthSupplier(
          registryAuth, RegistryConfigs.create(singletonMap(HUB_NAME, registryAuth))))
      .build();

  client.build(Paths.get(dockerDirectory), HUB_PRIVATE_IMAGE);
  client.push(HUB_PRIVATE_IMAGE);
}
 
Example 4
Source File: Utils.java    From docker-maven-plugin with Apache License 2.0 6 votes vote down vote up
public static void pushImageTag(DockerClient docker, String imageName,
                              List<String> imageTags, Log log, boolean skipPush)
    throws MojoExecutionException, DockerException, IOException, InterruptedException {

  if (skipPush) {
    log.info("Skipping docker push");
    return;
  }
  // tags should not be empty if you have specified the option to push tags
  if (imageTags.isEmpty()) {
    throw new MojoExecutionException("You have used option \"pushImageTag\" but have"
                                     + " not specified an \"imageTag\" in your"
                                     + " docker-maven-client's plugin configuration");
  }
  final CompositeImageName compositeImageName = CompositeImageName.create(imageName, imageTags);
  for (final String imageTag : compositeImageName.getImageTags()) {
    final String imageNameWithTag = compositeImageName.getName() + ":" + imageTag;
    log.info("Pushing " + imageNameWithTag);
    docker.push(imageNameWithTag, new AnsiProgressHandler());
  }
}
 
Example 5
Source File: PushMojo.java    From dockerfile-maven with Apache License 2.0 5 votes vote down vote up
@Override
protected void execute(DockerClient dockerClient)
    throws MojoExecutionException, MojoFailureException {
  final Log log = getLog();

  if (skipPush) {
    log.info("Skipping execution because 'dockerfile.push.skip' is set");
    return;
  }

  if (repository == null) {
    repository = readMetadata(Metadata.REPOSITORY);
  }

  // Do this hoop jumping so that the override order is correct
  if (tag == null) {
    tag = readMetadata(Metadata.TAG);
  }
  if (tag == null) {
    tag = "latest";
  }

  if (repository == null) {
    throw new MojoExecutionException(
        "Can't push image; image repository not known "
        + "(specify dockerfile.repository parameter, or run the tag goal before)");
  }

  try {
    dockerClient
        .push(formatImageName(repository, tag), LoggingProgressHandler.forLog(log, verbose));
  } catch (DockerException | InterruptedException e) {
    throw new MojoExecutionException("Could not push image", e);
  }
}
 
Example 6
Source File: PushPullIT.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testPushHubPublicImageWithAuthFromConfig() throws Exception {
  // Push an image to a public repo on Docker Hub and check it succeeds
  final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
  final DockerClient client = DefaultDockerClient
      .fromEnv()
      .registryAuthSupplier(new ConfigFileRegistryAuthSupplier(
          new DockerConfigReader(),
          Paths.get(Resources.getResource("dockerConfig/dxia4Config.json").toURI())))
      .build();

  client.build(Paths.get(dockerDirectory), HUB_PUBLIC_IMAGE);
  client.push(HUB_PUBLIC_IMAGE);
}
 
Example 7
Source File: Utils.java    From docker-maven-plugin with Apache License 2.0 4 votes vote down vote up
public static void pushImage(final DockerClient docker, final String imageName,
                             final List<String> imageTags, final Log log,
                             final DockerBuildInformation buildInfo,
                             final int retryPushCount, final int retryPushTimeout,
                             final boolean skipPush)
        throws MojoExecutionException, DockerException, InterruptedException {

  if (skipPush) {
    log.info("Skipping docker push");
    return;
  }

  int attempt = 0;
  do {
    final AnsiProgressHandler ansiProgressHandler = new AnsiProgressHandler();
    final DigestExtractingProgressHandler handler = new DigestExtractingProgressHandler(
            ansiProgressHandler);

    try {
      log.info("Pushing " + imageName);
      docker.push(imageName, handler);

      if (imageTags != null) {
        final String imageNameNoTag = getImageNameWithNoTag(imageName);
        for (final String imageTag : imageTags) {
          final String imageNameAndTag = imageNameNoTag + ":" + imageTag;
          log.info("Pushing " + imageNameAndTag);
          docker.push(imageNameAndTag, new AnsiProgressHandler());
        }
      }

      // A concurrent push raises a generic DockerException and not
      // the more logical ImagePushFailedException. Hence the rather
      // wide catch clause.
    } catch (DockerException e) {
      if (attempt < retryPushCount) {
        log.warn(String.format(PUSH_FAIL_WARN_TEMPLATE, imageName, retryPushTimeout / 1000,
            attempt + 1, retryPushCount));
        sleep(retryPushTimeout);
        continue;
      } else {
        throw e;
      }
    }
    if (buildInfo != null) {
      final String imageNameWithoutTag = parseImageName(imageName)[0];
      buildInfo.setDigest(imageNameWithoutTag + "@" + handler.digest());
    }
    break;
  } while (attempt++ <= retryPushCount);
}