Search in sources :

Example 1 with BuildOptions

use of org.eclipse.jkube.kit.build.service.docker.access.BuildOptions in project jkube by eclipse.

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 maven build context
 * @throws DockerAccessException docker access exception
 * @throws IOException in case of any I/O exception
 */
protected void buildImage(ImageConfiguration imageConfig, JKubeConfiguration params, boolean noCache, Map<String, String> buildArgs) throws IOException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildConfiguration 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.getSourceDirectory(), params.getProject().getBaseDirectory() != null ? params.getProject().getBaseDirectory().toString() : null));
        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).cacheFrom(buildConfig.getCacheFrom()).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    if (newImageId == null) {
        throw new IllegalStateException("Failure in building image, unable to find image built with name " + imageName);
    }
    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 : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) BuildOptions(org.eclipse.jkube.kit.build.service.docker.access.BuildOptions) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) CleanupMode(org.eclipse.jkube.kit.config.image.build.CleanupMode) File(java.io.File)

Aggregations

File (java.io.File)1 BuildOptions (org.eclipse.jkube.kit.build.service.docker.access.BuildOptions)1 DockerAccessException (org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException)1 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)1 CleanupMode (org.eclipse.jkube.kit.config.image.build.CleanupMode)1