Search in sources :

Example 1 with BuildOptions

use of io.fabric8.maven.docker.access.BuildOptions in project docker-maven-plugin by fabric8io.

the class BuildService method buildImage.

/**
 * Build an image
 *
 * @param imageConfig the image configuration
 * @param params mojo params for the project
 * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
 * @param buildArgs
 * @throws DockerAccessException
 * @throws MojoExecutionException
 */
protected void buildImage(ImageConfiguration imageConfig, MojoParameters params, boolean noCache, Map<String, String> buildArgs) throws DockerAccessException, MojoExecutionException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String oldImageId = null;
    CleanupMode cleanupMode = buildConfig.cleanupMode();
    if (cleanupMode.isRemove()) {
        oldImageId = queryService.getImageId(imageName);
    }
    long time = System.currentTimeMillis();
    if (buildConfig.getDockerArchive() != null) {
        docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params));
        log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
        return;
    }
    File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
    log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));
    Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
    // auto is now supported by docker, consider switching?
    BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
    if (oldImageId != null && !oldImageId.equals(newImageId)) {
        try {
            docker.removeImage(oldImageId, true);
            log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
        } catch (DockerAccessException exp) {
            if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
                log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(), (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
            } else {
                throw exp;
            }
        }
    }
}
Also used : BuildOptions(io.fabric8.maven.docker.access.BuildOptions) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) CleanupMode(io.fabric8.maven.docker.config.CleanupMode) File(java.io.File) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Example 2 with BuildOptions

use of io.fabric8.maven.docker.access.BuildOptions in project docker-maven-plugin by fabric8io.

the class UrlBuilderTest method buildImage.

@Test
public void buildImage() throws URISyntaxException {
    UrlBuilder builder = new UrlBuilder("", "1.0");
    assertEquals(new URI("/1.0/build?dockerfile=df&nocache=0&t=image1"), new URI(builder.buildImage("image1", new BuildOptions().dockerfile("df").noCache(false))));
    assertEquals(new URI("/1.0/build?dockerfile=df&forcerm=1&nocache=1&t=image1"), new URI(builder.buildImage("image1", new BuildOptions().forceRemove(true).noCache(true).dockerfile("df"))));
    HashMap<String, String> m = new HashMap<>();
    m.put("k1", "v1");
    m.put("k2", "v2");
    assertEquals("/1.0/build?buildargs=%7B%22k1%22%3A%22v1%22%2C%22k2%22%3A%22v2%22%7D&dockerfile=df&t=image1", builder.buildImage("image1", new BuildOptions().dockerfile("df").buildArgs(m)));
    HashMap<String, String> options = new HashMap<>();
    options.put("cpusetcpus", "1");
    options.put("memswap", "-1");
    assertEquals("/1.0/build?buildargs=%7B%22k1%22%3A%22v1%22%2C%22k2%22%3A%22v2%22%7D&cpusetcpus=1&memswap=-1&t=image1", builder.buildImage("image1", new BuildOptions(options).buildArgs(m)));
    options.put("dockerfile", "blub");
    assertEquals("/1.0/build?cpusetcpus=1&dockerfile=bla&memswap=-1&t=image1", builder.buildImage("image1", new BuildOptions(options).dockerfile("bla")));
    assertEquals("/1.0/build?cpusetcpus=1&dockerfile=holla&memswap=-1&t=image1", builder.buildImage("image1", new BuildOptions(options).dockerfile("bla").addOption("dockerfile", "holla")));
}
Also used : BuildOptions(io.fabric8.maven.docker.access.BuildOptions) HashMap(java.util.HashMap) UrlBuilder(io.fabric8.maven.docker.access.UrlBuilder) Test(org.junit.Test)

Aggregations

BuildOptions (io.fabric8.maven.docker.access.BuildOptions)2 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)1 UrlBuilder (io.fabric8.maven.docker.access.UrlBuilder)1 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 CleanupMode (io.fabric8.maven.docker.config.CleanupMode)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1