Search in sources :

Example 1 with JibContainerBuilder

use of com.google.cloud.tools.jib.api.JibContainerBuilder in project docker-maven-plugin by fabric8io.

the class JibBuildService method build.

public void build(String jibImageFormat, ImageConfiguration imageConfig, RegistryService.RegistryConfig registryConfig) throws MojoExecutionException {
    try {
        log.info("[[B]]JIB[[B]] image build started");
        if (imageConfig.getBuildConfiguration().isDockerFileMode()) {
            throw new MojoExecutionException("Dockerfile mode is not supported with JIB build strategy");
        }
        prependRegistry(imageConfig, mojoParameters.getProject().getProperties().getProperty(PUSH_REGISTRY));
        BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), mojoParameters);
        final Credential pullRegistryCredential = getRegistryCredentials(registryConfig, false, imageConfig, log);
        final JibContainerBuilder containerBuilder = containerFromImageConfiguration(jibImageFormat, imageConfig, pullRegistryCredential);
        File dockerTarArchive = getAssemblyTarArchive(imageConfig, serviceHub, mojoParameters, log);
        for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
            // TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared
            // the call to DockerAssemblyManager.getInstance().createDockerTarArchive should not be necessary,
            // files should be added using the AssemblyFileEntry list. DockerAssemblyManager, should provide
            // a common way to achieve this so that both the tar builder and any other builder could get a hold of
            // archive customizers, file entries, etc.
            AssemblyFiles assemblyFiles = serviceHub.getDockerAssemblyManager().getAssemblyFiles(imageConfig.getName(), assemblyConfiguration, mojoParameters, log);
            final Map<File, AssemblyFiles.Entry> files = assemblyFiles.getUpdatedEntriesAndRefresh().stream().collect(Collectors.toMap(AssemblyFiles.Entry::getDestFile, Function.identity(), (oldV, newV) -> newV));
            JibServiceUtil.copyToContainer(containerBuilder, buildDirs.getOutputDirectory(), buildDirs.getOutputDirectory().getAbsolutePath(), files);
        }
        JibServiceUtil.buildContainer(containerBuilder, TarImage.at(dockerTarArchive.toPath()).named(imageConfig.getName()), log);
        log.info(" %s successfully built", dockerTarArchive.getAbsolutePath());
    } catch (Exception ex) {
        throw new MojoExecutionException("Error when building JIB image", ex);
    }
}
Also used : Arrays(java.util.Arrays) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) ImageName(io.fabric8.maven.docker.util.ImageName) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Logger(io.fabric8.maven.docker.util.Logger) Function(java.util.function.Function) Credential(com.google.cloud.tools.jib.api.Credential) JibServiceUtil(io.fabric8.maven.docker.util.JibServiceUtil) BuildDirs(io.fabric8.maven.docker.assembly.BuildDirs) Map(java.util.Map) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) AuthConfigFactory(io.fabric8.maven.docker.util.AuthConfigFactory) Collection(java.util.Collection) JibServiceUtil.containerFromImageConfiguration(io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration) TarImage(com.google.cloud.tools.jib.api.TarImage) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) ArchiveCompression(io.fabric8.maven.docker.config.ArchiveCompression) JibServiceUtil.getBaseImage(io.fabric8.maven.docker.util.JibServiceUtil.getBaseImage) EnvUtil(io.fabric8.maven.docker.util.EnvUtil) AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) Credential(com.google.cloud.tools.jib.api.Credential) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) BuildDirs(io.fabric8.maven.docker.assembly.BuildDirs) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 2 with JibContainerBuilder

use of com.google.cloud.tools.jib.api.JibContainerBuilder in project docker-maven-plugin by fabric8io.

the class JibServiceUtilTest method testContainerFromImageConfiguration.

@Test
public void testContainerFromImageConfiguration(@Mocked JibContainerBuilder containerBuilder) throws Exception {
    // Given
    ImageConfiguration imageConfiguration = getSampleImageConfiguration();
    // When
    JibContainerBuilder jibContainerBuilder = containerFromImageConfiguration(ImageFormat.Docker.name(), imageConfiguration, null);
    // Then
    // @formatter:off
    new Verifications() {

        {
            jibContainerBuilder.addLabel("foo", "bar");
            times = 1;
            jibContainerBuilder.setEntrypoint(Arrays.asList("java", "-jar", "foo.jar"));
            times = 1;
            jibContainerBuilder.setExposedPorts(new HashSet<>(Collections.singletonList(Port.tcp(8080))));
            times = 1;
            jibContainerBuilder.setUser("root");
            times = 1;
            jibContainerBuilder.setWorkingDirectory(AbsoluteUnixPath.get("/home/foo"));
            times = 1;
            jibContainerBuilder.setVolumes(new HashSet<>(Collections.singletonList(AbsoluteUnixPath.get("/mnt/volume1"))));
            times = 1;
            jibContainerBuilder.setFormat(ImageFormat.Docker);
            times = 1;
        }
    };
// @formatter:on
}
Also used : BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) JibServiceUtil.containerFromImageConfiguration(io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Verifications(mockit.Verifications) Test(org.junit.Test)

Aggregations

JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)2 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)2 JibServiceUtil.containerFromImageConfiguration (io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration)2 Credential (com.google.cloud.tools.jib.api.Credential)1 TarImage (com.google.cloud.tools.jib.api.TarImage)1 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)1 AssemblyFiles (io.fabric8.maven.docker.assembly.AssemblyFiles)1 BuildDirs (io.fabric8.maven.docker.assembly.BuildDirs)1 ArchiveCompression (io.fabric8.maven.docker.config.ArchiveCompression)1 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)1 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 AuthConfigFactory (io.fabric8.maven.docker.util.AuthConfigFactory)1 EnvUtil (io.fabric8.maven.docker.util.EnvUtil)1 ImageName (io.fabric8.maven.docker.util.ImageName)1 JibServiceUtil (io.fabric8.maven.docker.util.JibServiceUtil)1 JibServiceUtil.getBaseImage (io.fabric8.maven.docker.util.JibServiceUtil.getBaseImage)1 Logger (io.fabric8.maven.docker.util.Logger)1 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)1 File (java.io.File)1 Arrays (java.util.Arrays)1