View Javadoc
1   package io.micronaut.build;
2   
3   import com.google.cloud.tools.jib.api.*;
4   import com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory;
5   import com.google.cloud.tools.jib.maven.MavenProjectProperties;
6   import io.micronaut.build.services.ApplicationConfigurationService;
7   import io.micronaut.build.services.DockerService;
8   import io.micronaut.build.services.JibConfigurationService;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.plugin.MojoFailureException;
11  import org.apache.maven.plugins.annotations.Mojo;
12  import org.apache.maven.project.MavenProject;
13  
14  import javax.inject.Inject;
15  import java.util.Optional;
16  
17  import static io.micronaut.build.DockerMojo.DOCKER_PACKAGING;
18  import static io.micronaut.build.DockerNativeMojo.DOCKER_NATIVE_PACKAGING;
19  
20  /**
21   * <p>Implementation of the <code>deploy</code> lifecycle for pushing Docker images</p>
22   * <p><strong>WARNING</strong>: this goal is not intended to be executed directly. Instead, Execute the <code>deploy</code>
23   * phase specifying the packaging type, eg:</p>
24   *
25   * <pre>mvn deploy -Dpackaging=docker-native</pre>
26   *
27   * @author Álvaro Sánchez-Mariscal
28   * @since 1.1
29   */
30  @Mojo(name = "docker-push")
31  public class DockerPushMojo extends AbstractDockerMojo {
32  
33      @Inject
34      public DockerPushMojo(MavenProject mavenProject, JibConfigurationService jibConfigurationService,
35                               ApplicationConfigurationService applicationConfigurationService, DockerService dockerService) {
36          super(mavenProject, jibConfigurationService, applicationConfigurationService, dockerService);
37      }
38  
39      @Override
40      public void execute() throws MojoExecutionException, MojoFailureException {
41          String packaging = mavenProject.getPackaging();
42          if (DOCKER_PACKAGING.equals(packaging) || DOCKER_NATIVE_PACKAGING.equals(packaging)) {
43              Optional<String> toImage = jibConfigurationService.getToImage();
44              if (toImage.isPresent()) {
45                  getLog().info("Pushing image: " + toImage.get());
46                  try {
47                      ImageReference imageReference = ImageReference.parse(toImage.get());
48                      DockerDaemonImage dockerDaemonImage = DockerDaemonImage.named(toImage.get());
49                      RegistryImage targetImage = RegistryImage.named(imageReference);
50  
51                      CredentialRetrieverFactory credentialRetrieverFactory = CredentialRetrieverFactory
52                              .forImage(imageReference, logEvent -> getLog().info(logEvent.getMessage()));
53                      targetImage.addCredentialRetriever(credentialRetrieverFactory.dockerConfig());
54                      targetImage.addCredentialRetriever(credentialRetrieverFactory.wellKnownCredentialHelpers());
55  
56                      jibConfigurationService.getCredentials().ifPresent(c -> targetImage.addCredential(c.getUsername(), c.getPassword()));
57                      jibConfigurationService.getCredHelper().ifPresent(credHelper -> targetImage.addCredentialRetriever(credentialRetrieverFactory.dockerCredentialHelper(credHelper)));
58  
59                      Containerizer containerizer = Containerizer.to(targetImage);
60                      Jib.from(dockerDaemonImage).containerize(containerizer);
61                  } catch (Exception e) {
62                      throw new MojoExecutionException(e.getMessage(), e);
63                  }
64              } else {
65                  throw new MojoFailureException("The plugin " + MavenProjectProperties.PLUGIN_KEY + " is misconfigured. Missing <to> tag");
66              }
67          } else {
68              throw new MojoFailureException("The <packaging> must be set to either [" + DOCKER_PACKAGING + "] or [" + DOCKER_NATIVE_PACKAGING + "]");
69          }
70      }
71  }